Tutorial by Examples

if [[ $file1 -ef $file2 ]]; then echo "$file1 and $file2 are the same file" fi “Same file” means that modifying one of the files in place affects the other. Two files can be the same even if they have different names, for example if they are hard links, or if they are symbolic links...
if [[ -r $filename ]]; then echo "$filename is a readable file" fi if [[ -w $filename ]]; then echo "$filename is a writable file" fi if [[ -x $filename ]]; then echo "$filename is an executable file" fi These tests take permissions and ownership into a...
Numerical comparisons use the -eq operators and friends if [[ $num1 -eq $num2 ]]; then echo "$num1 == $num2" fi if [[ $num1 -le $num2 ]]; then echo "$num1 <= $num2" fi There are six numeric operators: -eq equal -ne not equal -le less or equal -lt less than ...
String comparison uses the == operator between quoted strings. The != operator negates the comparison. if [[ "$string1" == "$string2" ]]; then echo "\$string1 and \$string2 are identical" fi if [[ "$string1" != "$string2" ]]; then echo &quot...
The -e conditional operator tests whether a file exists (including all file types: directories, etc.). if [[ -e $filename ]]; then echo "$filename exists" fi There are tests for specific file types as well. if [[ -f $filename ]]; then echo "$filename is a regular file&quot...
Exit status 0: success Exit status other than 0: failure To test on the exit status of a command: if command;then echo 'success' else echo 'failure' fi
You can do things like this: [[ $s = 'something' ]] && echo 'matched' || echo "didn't match" [[ $s == 'something' ]] && echo 'matched' || echo "didn't match" [[ $s != 'something' ]] && echo "didn't match" || echo "matched" [[ $s -eq...

Page 1 of 1