[bash] Conditional operators in BASH
-afile- True if file exists.
-bfile- True if file exists and is a block special file.
-cfile- True if file exists and is a character special file.
-dfile- True if file exists and is a directory.
-efile- True if file exists.
-ffile- True if file exists and is a regular file.
-gfile- True if file exists and its set-group-id bit is set.
-hfile- True if file exists and is a symbolic link.
-kfile- True if file exists and its “sticky” bit is set.
-pfile- True if file exists and is a named pipe (FIFO).
-rfile- True if file exists and is readable.
-sfile- True if file exists and has a size greater than zero.
-tfd- True if file descriptor fd is open and refers to a terminal.
-ufile- True if file exists and its set-user-id bit is set.
-wfile- True if file exists and is writable.
-xfile- True if file exists and is executable.
-Ofile- True if file exists and is owned by the effective user id.
-Gfile- True if file exists and is owned by the effective group id.
-Lfile- True if file exists and is a symbolic link.
-Sfile- True if file exists and is a socket.
-Nfile- True if file exists and has been modified since it was last read.
- file1
-ntfile2 - True if file1 is newer (according to modification date) than file2, or if file1 exists and file2 does not.
- file1
-otfile2 - True if file1 is older than file2, or if file2 exists and file1 does not.
- file1
-effile2 - True if file1 and file2 refer to the same device and inode numbers.
-ooptname- True if shell option optname is enabled. The list of options appears in the description of the -o option to the
setbuiltin (see The Set Builtin). -zstring- True if the length of string is zero.
-nstring- string
- True if the length of string is non-zero.
- string1
==string2 - True if the strings are equal. ‘=’ may be used in place of ‘==’ for strict posix compliance.
- string1
!=string2 - True if the strings are not equal.
- string1
<string2 - True if string1 sorts before string2 lexicographically in the current locale.
- string1
>string2 - True if string1 sorts after string2 lexicographically in the current locale.
- arg1
OParg2 OPis one of ‘-eq’, ‘-ne’, ‘-lt’, ‘-le’, ‘-gt’, or ‘-ge’. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2, respectively. Arg1 and arg2 may be positive or negative integers.
Reference: Copied directly from here.
[bash] Test if a variable is an integer using BASH script
#!/bin/bash
echo "Enter an integer; I'll test if it's really a one"
read VARIABLE
while [ -z "$VARIABLE" ]; do # Trap empty strings (but there's really no need!)
echo "Please enter something instead of just hitting ENTER. Thank you!"
read VARIABLE
done
#
if [ "$VARIABLE" -eq "$VARIABLE" >& /dev/null ]; then # test if integer
echo "'$VARIABLE' is indeed an integer"
else
echo "'$VARIABLE' is not an integer"
fi
echo "Good bye!"
Save the above in a file, say testint.sh, make it executable and test in the command line. The if statement does an arithmetic comparison which complains a lot if $VARIABLE is not an integer (that’s why there is a redirection!).
[bash] Introductory BASH scripting for scientists
These are based on the type of work I am interested in, i.e. scientific programming: you won’t see much about character strings in this post since we are mostly interested in numbers!
These can all be directly typed in the BASH prompt. Semicolons basically indicate the start of a new statement: you may put them in separate lines; and in that case, do not put the semicolon between the statements typed in different lines. The dollar sign ($) at the beginning of the command line indicates the command prompt and it is not a part of the command!
1. The basic
Print the sum of two integers:
$ echo $((2+3))
The two pairs of parentheses interpret them as numbers. You can also use square brackets (just a single pair):
$ echo $[2+3].
For floating point numbers use bc.
Variable substitutions may be done as
$ num1=10; num2=20; echo $(($num1+$num2))
(no space between the equality sign and the variable/number)
2. Conditional block: arithmetic conditional operators are eq, lt, le, gt and ge:
$ i=10; j=20; if [ $i -lt $j ]; then echo "$i<$j? True"; fi
$ i=$RANDOM;j=$RANDOM; if [ $i -lt $j ]; then echo "i=$i is less than j=$j"; else echo "i=$i is NOT less than j=$j";fi
($RANDOM equals a random number at each invocation)
Notice the spaces between the square brackets and the expression within it.
3. Array:
Assignment:
$ arr=(10 11 12)
which is the same as
$ arr[0]=10; arr[1]=11; arr[2]=12
Following constructs show how you can actually access the array (link)
$ echo ${arr[2]} # The 3rd element of the array
$ echo ${arr[*]} # All of the items in the array
$ echo ${!arr[*]} # All of the indexes in the array
$ echo ${#arr[*]} # Number of items in the array
$ echo ${#arr[0]} # Length of item zero
4. For loop
$ for (( c=1; c<=5; c++ )); do echo "c = $c"; done # C-style
$ for c in 1 2 3 4 5; do echo "c = $c"; done # List
$ arr=(1 2 3 4 5); for c in ${arr[*]}; do echo "c = $c"; done # Explicit array
$ for c in {1..5}; do echo "c = $c"; done # 1 thru' 5 with unit intervals
$ for c in {0..10..2}; do echo "c = $c"; done # 0 thru' 10 with interval=2
5. While and until loops
$ c=0; while [ $c -lt 10 ]; do echo "c = $c"; let c++; done
You may replace “let c++” by “((c++))”.
$ c=20; until [ $c -lt 10 ]; do echo "c = $c"; let c--; done
6. User input
Use read command to read in user input, e.g.
$ echo "Enter two integers"; read num1 num2; echo "$num1+$num2=$((num1+num2))"
In the second echo statement, when num1 and num2 are not enclosed within two pairs of parentheses they are treated as character strings, whereas in the last part the double parentheses cause them to be treated as numbers.
N.B. For the above loops and blocks, a line break between do or then and the statement that immediately follows it is allowed.
Reference: link.

leave a comment