Archive for the tag 'bash'

SBDavid

Bash Command line Arguments

Bash Command line Arguments

These are variables that contain the arguments to a script when it is run. These variables are accessed using $1, $2, … $n, where $1 is the first command-line argument, $2 the second, etc. Arguments are delimited by spaces. $0 is the name of the script. The variable $# will display the number of command-line arguments supplied; this number is limited to 9 arguments in the older shells, and is practically unlimited in the modern ones.

Example:

Consider a script that will take two command-line arguments and display them.

#!/bin/sh
echo “The first variable is $1″
echo “The second variable is $2

Debugging on part(s) of the bash script

Using the set Bash built-in you can run in normal mode those portions of the script of
which you are sure they are without fault, and display debugging information only for troublesome zones.

Say we are not sure what the `uptime` command will do in a script, then we could enclose it in the script like this:

set -x # activate debugging from here
uptime
set +x # stop debugging from here

Debugging on part(s) of the bash script

Using the set Bash built-in you can run in normal mode those portions of the script of which you are sure they are without fault, and display debugging information only for troublesome zones. Say we are not sure what the w command will do in the example commented-script1.sh, then we could enclose it in the script like this.

set -x # activate debugging from here
w
set +x # stop debugging from here

You can switch debugging mode on and off as many times as you want within the same script.