bash coding.
Know how to code bash flow of control statements : The test-command can be a actual command or even sequence of commands that will be run and the final return code tested. You may also use the test command which can either be invoked with the command test or be invoked with the open and closed square brackets, []. If using the brackets, remember to keep white-space on both sides of both open and closed brackets.
if
if test-cmd then action fi |
if test-cmd then action else action 2 fi |
if test-cmd then action elif test-cmd2 then action 2 fi |
if test-cmd then action elif test-cmd2 then action 2 else action 3 fi |
if test-cmd then action elif test-cmd2 then action 2 elif test-cmd2 then action 3 fi |
if test-cmd then action elif test-cmd2 then action 2 elif test-cmd2 then action 3 else action 4 fi |
while test-cmd do action1 done |
until test-cmd do action1 done |
read -p "y or n : " ans while [ $ans != "n" ] do read -p "y or n : " ans done |
read -p "y or n : " ans until [ $ans = "n" ] do read -p "y or n : " ans done |
#prompt user read -p "y or n : " ans # create an endless loop while [ 1 == 1 ] do if [ "$ans" = "y" ] then # break out of loop break; elif [ "$ans" = "n" ] then # Reprompt user and continue read -p "You sure ? y or n : " ans continue; fi # Repromt user with problem, loops back to top. read -p "Answer not valid, use y or n : " ans done |
case "$var" in template-1 ) action[s] ;; template-2 ) action[s] ;; ... esac |
case "$ans" in 123 ) echo "Exactly 123" ;; 123* ) echo "Starts with 123" ;; 456|654 ) echo "Either 456 or 654" ;; [0-9][0-9][0-9] ) echo "Some other three digit number" echo "values of 123, 456, 654, or starting with 123" echo "would have been caught by a previous case" ;; ??? ) echo "A three character string. # This should catch 3 any three character ans, except for # digits because we've already tested for those. ;; [0-9][0-9][0-9]* ) echo "A longer string that starts with three numbers." # Remember this is file-name wild-card, NOT regexp. # * is any characters of any number. # This will match any values starting with starting with # three digits except 123 because they would have been # matched above. ;; * ) echo "Any string that has not been previously matched" ;; esac |
function filecount () { if [ $# -lt 1 ] then echo "no file list" # set function arguments to list of current directory. set `ls` # create an endless loop while [ $# -gt 0 ] do wc $1 shift read -p "Continue ? y or n : " ans if [ "$ans" = "n" ] then # break out of loop break; elif [ "$ans" = "y" ] then # go back to top and continue. # If list is zero, the while loop will exit normally continue; fi # If invalid response, terminate program echo "Answer not valid. " exit 1 done return 0 } |
# for each file in current directory for fn in `ls` do # if the file is regular and readable if test -f $fn && test -r $fn then # do a word count on it. wc $fn fi done |
#!/bin/bash declare -a randArray # generate 10 random numbers. for (( ndx=1; ndx < 10; ndx++ )) { # $RANDOM is a shell defined variable that returns a # pseudo-random number. randArray[$ndx]=$RANDOM } echo ${randArray[*]} |
# Use select to display the contents of $HOME/Backup # Use command substitution to get contents of Backup # add quit to that list # # Put the selected string in the variable fn select fn in $(ls ~/Backup) quit do # show which list string was chosen echo $fn # if choice was quit, then break out of loop. if [ "$fn" == quit ] then break fi # trim the final extension off of the retrieved filename # if it has an 11 digit extension. rfn=$(echo "$fn" | sed "s/\.[0-9]\{11\}$//") # show results echo $rfn done |
function ask { read -p "up or down : " ans if echo $ans | grep '\(up\|down\)' > /dev/null 2>&1 then echo good answer else echo "What's $ans" fi }