UNIX Scripting: Difference between revisions

From HaFrWiki42
Jump to navigation Jump to search
Line 116: Line 116:


=== Tests overview ===
=== Tests overview ===
{| class="wikitableharm" width="950"
{| class="wikitableharmcenter" width="950"
! width="200" | Test
! width="070" | Test
! width="750" | Description
! width="750" style="text-align:left;" | Description
|-  
|-  
| -a || File exists. {{FormFCT|9|blue|Deprecated}}, its use is discouraged.
| -a  
| style="text-align:left;" | 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>.
| -b  
| style="text-align:left;" | 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>
| -c  
| style="text-align:left;" | File is a character device <ref>''Character special files'' or ''character devices'' provide unbuffered, direct access to the hardware device.</ref>.
|-
| -d
| style="text-align:left;" | File is a directory.
|-
| -e
| style="text-align:left;" | File exists. The ''file'' maybe a file or a directory. Use other operators to find out.
|-
| -f
| style="text-align:left;" | File is a regular file (not a directory or device file).
|-
| -g
| style="text-align:left;" | File exists and its set-group-id bit is set. The ''file'' maybe a file or a directory <ref></ref>
|-
| -h
| style="text-align:left;" | File is a symbolic link.
|-
| -i
| style="text-align:left;" | {{FormFCT|9|blue|Not used}}
|-
| -j
| style="text-align:left;" | {{FormFCT|9|blue|Not used}}
|-
| -k
| style="text-align:left;" | File exists and its sticky bit is set <ref>The sticky bit is a user ownership access right flag that can be assigned to files and directories on Unix-like systems.</ref>.
|}
|}


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

Revision as of 18:04, 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].
-d File is a directory.
-e File exists. The file maybe a file or a directory. Use other operators to find out.
-f File is a regular file (not a directory or device file).
-g File exists and its set-group-id bit is set. The file maybe a file or a directory Cite error: Invalid <ref> tag; refs with no name must have content
-h File is a symbolic link.
-i Not used
-j Not used
-k File exists and its sticky bit is set [5].

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.
  5. The sticky bit is a user ownership access right flag that can be assigned to files and directories on Unix-like systems.