DBA Unix Commands

Passing arguments to a shell script

posted Aug 5, 2011, 7:48 AM by Sachchida Ojha

Any shell script you run has access to (inherits) the environment variables accessible to its parent shell. In addition, any arguments you type after the script name on the shell command line are passed to the script as a series of variables.

The following parameters are recognized:


$*
Returns a single string (``$1, $2 ... $n'') comprising all of the positional parameters separated by the internal field separator character (defined by the IFS environment variable).

$@
Returns a sequence of strings (``$1'', ``$2'', ... ``$n'') wherein each positional parameter remains separate from the others.

$1, $2 ... $n
Refers to a numbered argument to the script, where n is the position of the argument on the command line. In the Korn shell you can refer directly to arguments where n is greater than 9 using braces. For example, to refer to the 57th positional parameter, use the notation ${57}. In the other shells, to refer to parameters with numbers greater than 9, use the shift command; this shifts the parameter list to the left. $1 is lost, while $2 becomes $1, $3 becomes $2, and so on. The inaccessible tenth parameter becomes $9 and can then be referred to.

$0
Refers to the name of the script itself.

$#
Refers to the number of arguments specified on a command line.

For example, create the following shell script called mytest:

   echo There are $# arguments to $0: $*
echo first argument: $1
echo second argument: $2
echo third argument: $3
echo here they are again: $@
When the file is executed, you will see something like the following:
   $ mytest foo bar quux
There are 3 arguments to mytest: foo bar quux
first argument: foo
second argument: bar
third argument: quux
here they are again: foo bar quux
$# is expanded to the number of arguments to the script, while $* and $@ contain the entire argument list. Individual parameters are accessed via $0, which contains the name of the script, and variables $1 to $3 which contain the arguments to the script (from left to right along the command line).

Although the output from $@ and $* appears to be the same, it may be handled differently, as $@ lists the positional parameters separately rather than concatenating them into a single string. Add the following to the end of mytest:

   function how_many {
print "$# arguments were supplied."
}
how_many "$*"
how_many "$@"
The following appears when you run mytest:
   $ mytest foo bar quux
There are 3 arguments to mytest: foo bar quux
first argument: foo
second argument: bar
third argument: quux
here they are again: foo bar quux
1 arguments were supplied.
3 arguments were supplied.

Set backspace as erase key

posted Sep 10, 2010, 7:53 AM by Sachchida Ojha

set backspace as erase key: $stty erase <ctrlV backspace key> 

How to refresh the .profile for one user?

posted Sep 10, 2010, 7:51 AM by Sachchida Ojha

$. ./.profile (Dot space dot slash dot profile) OR the best way is logout and login again. It will load with latest changes done by you.

Numeric tests

posted Sep 10, 2010, 7:46 AM by Sachchida Ojha

Numeric relationals. The arguments must be entirely numeric (possibly negative), or the special expression `-l STRING', which evaluates to the length of STRING.

`ARG1 -eq ARG2'
`ARG1 -ne ARG2'
`ARG1 -lt ARG2'
`ARG1 -le ARG2'
`ARG1 -gt ARG2'
`ARG1 -ge ARG2'
These arithmetic binary operators return true if ARG1 is equal, not-equal, less-than, less-than-or-equal, greater-than, or greater-than-or-equal than ARG2, respectively.

String tests

posted Sep 10, 2010, 7:42 AM by Sachchida Ojha

These options test string characteristics. Strings are not quoted for `test', though you may need to quote them to protect characters with special meaning to the shell, e.g., spaces.
 
`-z String'
    True if the length of String is zero.

`-n String'
`String'
     True if the length of String is nonzero.

`String1 = String2'
     True if the strings are equal.

`String1 != String2'
     True if the strings are not equal.

File Age

posted Sep 10, 2010, 7:40 AM by Sachchida Ojha

These options test the file modification date.

 `file1 -nt file2' : True if file1 is newer than file2.

 `file1 -ot file2':  True if file1 is older than file2.

File type tests

posted Sep 10, 2010, 7:28 AM by Sachchida Ojha   [ updated Feb 12, 2012, 8:52 PM by Sachchida Ojha ]


These options test for particular types of files. All cases will only return True (0) if the file exists.

`-b file':True if file is a Block special device.
`-c file':True if file is a Character special device.
`-d file':True if file is a Directory.
`-e file':True if file Exists.
`-f file':True if file is a regular File.
`-g file':True if file has its set-group-id bit set.
`-G file':True if file is owned by the current effective group id.
`-k file':True if file has its "sticky" bit set.
`-h file':
`-L file':True if file is a symbolic Link.
`-O file':True if file is owned by the current effective user id.
`-p file':True if file is a named Pipe.
`-r file':True if file is readable.
`-S file':True if file is a Socket.
`-s file':True if file has a Size greater than zero.
`-t [FD]':True if FD is opened on a terminal.  If FD is omitted, it defaults to 1 (standard output).
`-u file':True if file has its set-user-id bit set.
`-w file':True if file is writable.
`-x file':True if file is executable.
`file1 -ef file2':True if file1 and file2 have the same device and inode numbers,i.e., if they are hard links to each other.

1-7 of 7