Home » Tech » Linux » Unix/Linux/OSX scripting
          

Unix/Linux/OSX scripting

{stolen from here…}

Unix Shell Stuff
 


 

$# - Contains the number of arguments passed to the 
      program in the form of positional variables.

$* - Contains all the positional parameters passed to 
      the program.

$1-$9 - Positional parameters.

$0 - Contains the program name.

$$ - Contains the process ID of the current process.

$? - Contains the exit status of the last command executed. 

expr - Used to evaluate arguments as mathematical 
        expressions.  ex: CNT=`expr $CNT + 1` 

; - Used to separate commands on command line. 

\ - Used to continue the command on the next line.

\c - Used to suppress newline.
      ex: echo "Enter Name:\c" ; read name

read - Assigns the value entered into the given variable.
         ex: echo "Enter Name:"  
             read name

` ` - A command within grave accent marks returns 
       the output to the variable ex: DATE=`date`.

set - Set the values of the command line argument 
        variables ($1-$9) to it's arguments.

--   - No more options follow.

if expressions:
  if
  then
       Commands
   else
       Commands
  fi

   :     Null Character (always true)

   ;     Separates commands on one line 

   #!    As the first characters in the first line of shell script,
           tells OS that what follows, is the path to the shell to execute this script with.

   exec  Does not start an new process for the command being execed.
            Over lays the current process instead of starting new one.  Does not return control to shell.

   break Exits "for", "while" and "until" loop.

 


 

Testing Character Data:
 

 
   =   Equals, string comparisons ex: if [ "$str1" = "str2" ]; then    (equal)

   !=  Not equal, string comparisons ex: if [ "$str1" != "str2" ]; then    (not equal)

   -n string - True if the length of string is greater than 0 (is not null)

   -z string - True if string is null (has a length of 0)

   test - Test strings ex: test "$str" = "abc"

 


 

Testing Numeric Data:
 

     -eq -  Equals                      ex:  if [ $1 -eq 0 ]; then
     -ne -  Not equal                   ex:  if [ $1 -ne 0 ]; then
     -gt -  Greater than                ex:  if [ $1 -gt 0 ]; then
     -ge -  Greater than of equal to    ex:  if [ $1 -ge 0 ]; then
     -lt -  Less than                   ex:  if [ $1 -lt 0 ]; then
     -le -  Less than or equal to       ex:  if [ $1 -le 0 ]; then


Testing For Files:

     True if
        -r    Read permission           ex:  if [ -r $FILE ]; then
        -w    Write permission          ex:  if [ -w $FILE ]; then
        -x    Execute permission        ex:  if [ -x $FILE ]; then
        -f    Regular file              ex:  if [ -f $FILE ]; then
        -d    Directory                 ex:  if [ -d $FILE ]; then
        -c    Character special file    ex:  if [ -c $FILE ]; then
        -b    Block special file        ex:  if [ -b $FILE ]; then
        -s    File not zero             ex:  if [ -s $FILE ]; then
        -t    Terminal device           ex:  if [ -t $FILE ]; then
        -o    True if either (OR)       ex:  if [ ! -r $FILE  -o ! -f $FILE ]; then
        -a    True if both (AND)        ex:  if [ -r $FILE -a -f $FILE ]; then