UNIX Scripting: Difference between revisions

From HaFrWiki42
Jump to navigation Jump to search
Created page with "{{TOCright}} A summary of useful tips and tricks as can be found on different (web)-pages such as: * Russel Quong <ref>[http://www.quong.com/shellin20/shellin20.html Russel Qu..."
 
Line 94: Line 94:
done
done
</pre>
</pre>
== Tests ==
UNIX tests have the format:
<pre>
if [ 0 ]      # zero
then
  echo "0 is true."
else          # Or else ...
  echo "0 is false."
fi   
</pre>
Suppose you want to check if the first parameter (a filename) exists then:
<pre>
if [ -e $1 ]
then
  echo "File $1 exists"
else
  echo "File $1 does not exist"
fi
</pre>
=== Tests overview ===
{| class="wikitableharm" width="950"
! width="200" | Test
! width="750" | Description
|-
| -a || File exists. {{FormFCT|9|blue|Deprecated}}, its use is discouraged.
|-
| -b || File is a block device <ref>''Block special files'' or ''block devices'' provide buffered access to the hardware, such that the hardware characteristics of the device are not visible.</ref>.
|-
|-
| -c || File is a character device <ref>''Character special files'' or ''character devices'' provide unbuffered, direct access to the hardware device.</ref>
|}


== See also ==
== See also ==

Revision as of 15:27, 3 August 2015

A summary of useful tips and tricks as can be found on different (web)-pages such as:

  • Russel Quong [1], A guide for writing scripts.
  • Mendel Cooper [2], Advanced Bash Scripting Guide. An in-depth exploration of the art of shell scripting.

Debugging Scripts

  1. Use echo statements.
  2. Run bash -n script to check for syntax errors.
  3. Use the command set -v to get a verbose dump of each line the shell reads.
    Use set +v to turn off verbose mode.
  4. Use the command set -x to see what each command expands to.
    Again, set +x turns this mode off.

Command line arguments

The command line parameters to a script are stored in the nearly identical variables $* and $.
The following table summarizes the variables you would use for command line processing.
For the example values, assume you wrote a bash script /usr/bin/args.sh and ran it as shown below.

Variable Meaning Example:
echoArgs -t two "let's go"
$* Command line args -t two let's go
$@ Command line args -t two "let's go"
$# Number of args 3
$0 Name of script /usr/bin/args.sh
$1 First arg in $* -t
$2 Second arg in $* two
$3 Third arg in $* let's go
$4 Fourth arg in $* (empty)
echoArgs () { 
    echo $#
    for i in "$@"; do
        echo "($i)";
    done;
    for i in $*; do
        echo "(($i))";
    done
}

$ echoArgs -t two "let's go"
  3
  (-t)
  (two)
  (let's go)
  ((-t))
  ((two))
  ((let's))
  ((go))

Command Line Options

I prefer the usage of case statements in stead of the getops.

Flag Description
-o OUT Sends output to file OUT
-n Shows what you would do but do not do it
-v Gives more output, each -v increases verboseness
-l Same as -verbose
-version Shows the version and quit


nflag=0
vlevel=0
OUT=
while [ $# -gt 0 ] 
do
  case "$1" in 
    -o ) OUT=$2 ; shift 2 ;;
    -n ) nflag=1 ; shift ;;
    -l | -v ) vlevel=$(( vlevel+1 )) ; shift ;;
    -ver* ) echo "Version $version"  ; exit 1 ;;
    * ) echo "Saw non flag $arg" ; break ;;
  esac
done

Tests

UNIX tests have the format:

if [ 0 ]      # zero
then
  echo "0 is true."
else          # Or else ...
  echo "0 is false."
fi    

Suppose you want to check if the first parameter (a filename) exists then:

if [ -e $1 ] 
then
   echo "File $1 exists"
else
   echo "File $1 does not exist"
fi

Tests overview

Test Description
-a File exists. Deprecated, its use is discouraged.
-b File is a block device [3].
-c File is a character device [4]


See also

top

Reference

top

  1. Russel Quong, A guide to writing shell scripts for C/C++/Java and unix programmers.
  2. Mendel Cooper Advanced Bash-Scripting Guide. Very extensive Guide with a lot of examples.
  3. Block special files or block devices provide buffered access to the hardware, such that the hardware characteristics of the device are not visible.
  4. Character special files or character devices provide unbuffered, direct access to the hardware device.