Bash Programming Basics

Debugging

Start up the shell with the -x option, which will run the entire script in debug mode. Traces of each command plus its arguments are printed to standard output after the commands have been expanded but before they are executed.
#!/bin/bash -x        #on first line

Variable

a=5
export b='whatever'
c="$a $b"
d='$a $e'                                    # no substitution
local e=56

Array

list=(/tmp/m*)
echo ${#list[@]}
echo ${list[0]} 
echo ${list[@]}
echo ${list[*]} # same as @

Conditionals

Empty or unset variable
if [[ ${b:-z} == z ]]; then 
    echo "variable is empty or unset"; 
fi
String
if [ "a" = "b" ]; then
  echo yes;
else                                         #elif
  echo no
fi
multiple condition
if [ "a" = "b" -a "c" == "c" ]; then       # string = or ==
  echo "and condition true";
else                                         #elif
  echo no
fi

if [ "a" = "b" -o "c" == "c" ]; then       # string = or ==
  echo "or condition true";
else                                         #elif
  echo no
fi

if [[ $a == "b" && $b == "c" ]]; then
   echo yes;
fi

[[ -f $NATION_FILE ]] || fail "No nation file";
Numbers
if [ 5 -eq $? ]; then                         # numbers
  echo yes
fi
commands
if grep a * >/dev/null; then 
  echo yes; 
fi

Loop

For loop
for each in $(ls); do
  echo $each
done
While loop
COUNTER=0
while [  $COUNTER -lt 10 ]; do
    echo The counter is $COUNTER
    COUNTER=$((COUNTER+1)) 
done
Util
COUNTER=20
until [  $COUNTER -lt 10 ]; do
     echo COUNTER $COUNTER
     let COUNTER-=1
done

Read Each Line in a Variable

files=$(ls -1)
echo "$files"      # one line
echo $files | while read -r line; do echo $line; done    #one line
echo "$files" | while read -r line; do echo $line; done  # per line
echo "$files"|xargs  echo      #one line
echo "$files"|xargs -L1 echo   # per line

Functions

myecho()
{
  echo $*  
  echo $@
}

func_parm()
{
  [ -z $1 ] || echo parm missing && exit 1
  echo parm
}

Signal Handling

handle_int()
{
  echo "Control C"
}

trap handle_int SIGINT 

Manipulating Strings

http://tldp.org/LDP/abs/html/string-manipulation.html
${#String}
${string:position}
${string:position:length}
${string#substring}
${string##substring}

Redirection

   1>filename
      # Redirect stdout to file "filename."
   1>>filename
      # Redirect and append stdout to file "filename."
   2>filename
      # Redirect stderr to file "filename."
   2>>filename
      # Redirect and append stderr to file "filename."
   &>filename
      # Redirect both stdout and stderr to file "filename."
      # This operator is now functional, as of Bash 4, final release.

   M>N
     # "M" is a file descriptor, which defaults to 1, if not explicitly set.
     # "N" is a filename.
     # File descriptor "M" is redirect to file "N."
   M>&N
     # "M" is a file descriptor, which defaults to 1, if not set.
     # "N" is another file descriptor.

Operators

Numbers
arg1 OP arg2
       OP  is 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,  respec‐
       tively.  Arg1 and arg2 may be positive or negative integers.  
String
string
-n string
       True if the length of string is non-zero.

string1 == string2
string1 = string2
       True if the strings are equal.  = should be used with the test command for POSIX conformance.

string1 != string2
       True if the strings are not equal.

string1 < string2
       True if string1 sorts before string2 lexicographically.

string1 > string2
       True if string1 sorts after string2 lexicographically.

No comments: