Untitled Document
set Command

Some UNIX commands like date produce single-line output . We also pass command output through filters like grep , cut , head and tails etc. to produce a single line . We have seen the cut command to extract a field from the emp.csv file . Lets see some examples of set command which does this task , but in more tricky and shortcut way .

1. Basic example -

Run the below set command on your terminal .

   set 113 116 119

The above command assigns the value 113 to the positional parameter $1 , 116 to $2 and 119 to $3 . It also sets the other parameters $# and $* We can verify this by echoing each parameter in turn :

   echo "\$1 is $1 , \$2 is $2 , \$3 is $3 "
   output -  $1 is 113 , $2 is 116 , $3 is 119 
   echo "The $# arguments are $*"
   Output - The 3 arguments are 113 116 119 

2. Extracting filed values using set command -

We will now use command substitution to extract indiviual fields from the date output and without using cut .

    set `date`
    echo $*
    Sun Mar 19 10:38:15 IST 2016
    echo " The date today is $2 $3 $6 "
    The date today is Mar 19 , 2016

The day of the week is available in $1.

set parses its arguments on the delimiters specified in the environment variabel . IFS, which , by default , is whitespace . We can change the value of this variable to make set work on a different delimiters . This means that you can easily extract any field in aline from /etc/passwd without using cut !

3. set -- Helps command substitution -

We often need to use set with command substitution . There is a small problem though , especially when the output of the command begins with a -

    set `ls -l progs`
    Output - rwx-rwr-- :  bad options (s)

Since the permission begins with a - ( for regular files ) , set interprets it as an option and finds it to be a "bad" one .set creates another problem when its argument evaluate to a null string .

4. Example of Null string -

    set `grep PPP /etc/passwd`

If the string PPP can't be located in the file , set will operate with no arguments and puzzle the user by displaying all variables on the terminal ( its default output ) ! The solution to both these problems lies in the use of -- ( two hyphens ) immediately after set .

    set -- `grep PPP /etc/passwd`                Null output is no problem 
    set `ls -l progs`                            First - now taken care of 

set now understands that the arguments following -- are not to be treated as options . The two hyphens also direct set to suppress its default behaviour if its argument evaluate to anull string.

5. Debugging Shell Scripts with set -x -

Apart from assigning values to positional parameters , set serves as a useful debugging tool with its -x option . When used inside a script ( or even at the $ prompt ) , it echoes each statement on the terminla , preceded by a + as it is executed . Modify any previous script to turn on the set option by placing the following statement at the begining of the script .

    set -x 

set +x turns off set -x , and you can place the latter statement at the ned of the script . This is what you will see when you invoke the script for sum of two numbers . with set -x

       #!/bin/ksh
       set -x 
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program To Print sum of two numbers
       
       #We have hardcode variables x and y 
       
       + x=10
       + y=15
       + sum=`expr x + y`
       + echo " Sum of $x and $y is : $sum "
       

This is an ideal tool to use if you have trouble finding out why scripts don't work in the manner expected . Note how the shell prints each statement as it is being executed , affixing a + to each .

Untitled Document