Starting from today, I will be publishing quick tricks to help people who want to start writing their own bash scripts 🙂
A new trick will be published here soon, so stay tuned!
Since the today’s trick is very simple, I will just publish the code to implement it. If you have any difficulties, please comment and I will be happy to help you addressing them.
1. Using the -f param on an if statement
if [[ -f /etc/network/interfaces ]]; then echo "file exists" else echo "file does not exists" fi
if [[ ! -f /etc/network/interfaces ]]; then echo "file does not exists" fi
2. Using the -f param with a ternary operator
[[ -f /etc/network/interfaces ]] && echo "file exists"
[[ ! -f /etc/network/interfaces ]] && echo "file does not exists"
[[ -f /etc/network/interfaces ]] && echo "file exists" || echo "file does not exists"
3. Using the /usr/bin/test binary with a ternary operator
test -f /etc/network/interfaces && echo "file exists"
! test -f /etc/network/interfaces && echo "file does not exists"
test -f /etc/network/interfaces && echo "file exists" || echo "file does not exists"
…
If you know a different way to do this, please feel free to share it on the comments 🙂
Also, if you have any suggestions for the next tricks, please leave a comment and I will be happy to write about them in the future.
If this helped you at all, please don’t forget to share it on your social media outlets!
See you in few days 🙂
Alejandro M says
Usually more useful than -f is -s, which also tests if the file has content
Alejandro M. says
also `[ … ] && foo` can be problematic as the statement can return false and break `set -e`.
it tends to be better to construct them as `[ … ] || exception` …. where the statement is true because the test was successful or because the exception handler was successful.
e.g.
[ -s “somefile” ] || what_if_somefile_aint_there
it can also be used for asserts, [ -s “$configfile” ] || die “$configfile: not found”
Tiago Hillebrandt says
Hi Alejandro,
About the -s, I think it will depend from what you want to do 🙂
For other people reading this, here are some arguments extracted from test command manual:
-e FILE
FILE exists
-f FILE
FILE exists and is a regular file
-s FILE
FILE exists and has a size greater than zero
For statement returning false, yes, I agree it is always important to make sure that command will not break the script. Besides your suggestion, other option would be to check the $? value, since it always contains the last command exit code. Will be writing about this in a near future 🙂
Thanks for sharing your tricks!
Andy says
With Bash, you might consider using [[ …]] instead of [ … ]. The former is more feature rich.
Tiago Hillebrandt says
Hi Andy,
Thanks for sharing your trick!
In that example, since [ acts like test command, can’t see a good reason to use double square brackets on it. If you know a good reason to use the double ones in this example, please share with us.
See you 🙂
Defsdoor says
Since 1993 (ksh93) there has been no reason to use test, [, expr etc…
You should use [[ and (( as the “norm” and really be asking when or why would you use test etc..
Tiago Hillebrandt says
Hey Defsdoor,
Thanks for sharing your trick 🙂
Nick the Geek says
Cool tip. I’m definitely interested in improving my bash skills and seeing how the ternary operators work in bash opens up a whole new set of possibilities for me so thanks for showing different methods.
When I clicked through from FB I thought this would be showing how to find a file in general regardless of directory tree. I can see how it would be useful if you know the file name but not the exact location to be able to run a quick script that will echo the location of the file if it exists in the file system in general or prints file not found.
Maybe you could do that for your next tip?
Tiago Hillebrandt says
Hey Nick,
That would be a good trick. Will be writing about find command on next post. For example, it is possible to find and run specific commands for all PHP files in a directory.
Thanks for commenting here 🙂
Virneto says
Adorei o conteudo! || Loved this content!
Vou ficar atento ao próximo conteúdo. || I’ll stay tunned!
Tiago Hillebrandt says
Hi Virneto,
Glad to hear you loved the content 🙂
Would be happy to see you back here when publish the new trick!
Thanks for your comment!