Tuesday, January 11, 2011

Positional parameters in a shell script



 Positional parameters in a shell script are nothing but the command line arguments passed to a shell script. The following are some of the positional parameters used:
$# -  Total number of arguments
$0 - Command or the script name
$1,$2, $3 - First, second and third args respectively.
$* - All the command line arguments starting from $1.


Let us see this with an example:
[root@gpr ~]# cat cmd
#!/usr/bin/ksh

echo "The total no of args are: $#"
echo "The script name is : $0"
echo "The first argument is : $1"
echo "The second argument is: $2"
echo "The total argument list is: $*"
Output:
[root@gpr ~]# ./cmd 1 2 3 4
The total no of args are: 4
The script name is : ./cmd
The first argument is : 1
The second argument is: 2
The total argument list is: 1 2 3 4
[root@gpr ~]#
As shown in the above output,  $# printed 4 which is the total number of arguments, $0 printed the script name.

Similarly, the command line arguments can be accessed using $1, $2 till $9. However, if the number of command line arguments is more than 9, the same notation cannot be used. Instead, it should be used like ${10}, ${11} and so on. Let us see this with an example:
[root@gpr ~]# cat cmd
#!/usr/bin/ksh

echo "The total no of args are: $#"
echo "The script name is : $0"
echo "The first argument is : $1"
echo "The second argument is: $2"
echo "The incorrect 10th arg is : $10"
echo "The correct 10th arg is : ${10}"
[root@gpr ~]#
Output:
[root@gpr ~]# ./cmd a b c d e f g h i j
The total no of args are: 10
The script name is : ./cmd
The first argument is : a
The second argument is: b
The incorrect 10th arg is : a0
The correct 10th arg is : j
[root@gpr ~]#
As shown above, the correct result appeared when we used ${10}. When $10 is used, the shell interprets it as $1 concatenated with 0, and hence you get the result as a0 ($1 is a). The same terminology will be used for all the arguments after the 9th.

6 comments:

  1. what is the meanig of these line [root@gpr ~]# cat cmd ?

    ReplyDelete
    Replies
    1. root@gpr is root user and your machine, ~ means the home directory, and cat cmd means to print the content of cmd file.

      Delete
  2. what is the meaning of [root@gpr ~]# cat cmd?

    ReplyDelete
    Replies
    1. [root@gpr ~]# is the user you are logged in as, and cat cmd is the command

      Delete
  3. I am getting error when I run my scrpit positional parameter not found. Where and what should I add as a positional parameter in the below script . Plz someone reply asap.

    #Check the logifle to be smaller than 10MB, otherwise do a rollover
    get-item C:\Program Files\log\namget_ps.log | ForEach-Object {
    if ($_.lenght -ge 10MB) {
    (get-date).ToString() + " " + "Performing a logfile rollover" | out-file -filepath 'C:\Program Files\log\namget_ps.log' -Append
    move-item -Path C:\Program Files\log\namget_ps.log -Destination C:\Program Files\log\namget_ps.log.old -Force
    (get-date).ToString() + " " + "Logfile rollover completed succesfully" | out-file -filepath 'C:\Program Files\log\namget_ps.log'

    ReplyDelete
  4. What is the difference between: echo $* and echo $@ ?
    Both are returning the list of arguments passed as argument.
    $ set a b c d e f
    $ echo $*
    a b c d e f
    $ echo $@
    a b c d e f
    $

    ReplyDelete