Checking the existence of a file and / or directory
To check if a file exists in bash , you use the -f operator. For directories, use -d . Usage example:
$ mkdir dir $ [ -d dir ] && echo exists! exists! $ rmdir dir $ [ -d dir ] && echo exists! $ touch file $ [ -f file ] || echo "doesn't exist..." $ rm file $ [ -f file ] || echo "doesn't exist..." doesn't exist...
For more information, just run man test .
Note in -e , this test statement checks if a file exists. Although this might seem like a good choice, it's best to use -f , which will return false if the file is not a regular file. /dev/null for example, this is a file, but not a regular file. In this case, an error returning true in this case is undesirable.
Variable Note
Do not forget to specify the variables, if you have a space or any other special character contained in the variable, it may have undesirable side effects. Therefore, when you check for files and directories, wrap the / dir file in double quotes. Something like [ -f "/path/to/some/${dir}/" ] will work until the following is done if there is a space in dir : [ -f /path/to/some/${dir}/ ] .
Fix syntax error
You have a syntax error in control statements. The bash if clause is structured as follows:
if ...; then ... fi
Or optionally with an else clause:
if ...; then ... else ... fi
You cannot omit the then clause. If you want to use only the else clause, you must cancel this condition. The result of the following code:
if [ ! -f "/usr/share/icons/$j/scalable" ]; then mkdir "/usr/share/icons/$j/scalable/" fi
Here we add an exclamation mark ( ! ) To reverse the evaluation of the expression. If the expression is true, then the same expression is preceded by ! will return false and vice versa.