www.Tutorialsforu.info

Free Tutorials Cave

  • Increase font size
  • Default font size
  • Decrease font size
Your Ad Here



Five Shell Programming Tips

E-mail Print
Article Index
Five Shell Programming Tips
Page 2
All Pages

Five Shell Programming Tips

 

 I recently read this article Good Shell Coding Practices on handling command line arguments in scripts. I am going to cover some tips for general shell programming I have learned over the years.

  1. grep -q: Do you use the exit status of grep? If so, you the -q switch instead of sending the output to /dev/null

    #!/bin/bash
    if grep 'Synergy' messages >/dev/null
    then
    echo 'Synergy has logged to messages'
    else
    echo 'Synergy has NOT logged to messages'
    fiif grep -q 'Synergy' messages
    then
    echo 'Synergy has logged to messages'
    else
    echo 'Synergy has NOT logged to messages'
    fi

    Why use -q instead of >/dev/null? Well its ALOT faster. I have not looked at the source but I believe -q implies -m 1 as they are the same speed when used together. (See below) The option -m 1 tells grep to stop after the first match. When using -q you only care if the file contains your search term and do not care how many times or what those lines look like. Thus it would make sense that -q implies -m 1.

    [root@mojito ~]# time grep 'Synergy' messages >/dev/nullreal 0m0.360s
    user 0m0.266s
    sys 0m0.072s
    [root@mojito ~]# time grep -q 'Synergy' messages
    real 0m0.002s
    user 0m0.001s
    sys 0m0.001s
    [root@mojito ~]# time grep -q -m 1 'Synergy' messages
    real 0m0.002s
    user 0m0.000s
    sys 0m0.002s
  2. grep -c: Instead of using grep to match lines in a file or input and then piping that to wc -l, just use grep -c. Its faster and simpler:

    [root@mojito ~]# time grep 'Synergy' messages | wc -l
    495948real 0m0.519s
    user 0m0.363s
    sys 0m0.118s
    [root@mojito ~]# time grep -c 'Synergy' messages
    495948
    real 0m0.245s
    user 0m0.167s
    sys 0m0.052s
  3. VAL=$( funcName ): Most people new to UNIX/Linux don’t use functions in their scripts. More expirence brings more functions. However, too many people have those functions set environment variables instead of returning the values directly. You cannot use the return command to return values (except exit status) but you can use echo. For example, here is two simple functions using the environment variable method and the echo method:

    [root@mojito ~]# cat returnVals.sh
    #!/bin/bash
    function badFunc()
    {
    export badFuncVar=VALUE
    }
    function goodFunc()
    {
    echo VALUE
    }
    badFunc
    var1=$badFuncVar
    var2=$( goodFunc )
    var3=` goodFunc `
    echo "var1=$var1 , var2=$var2 , var3=$var3"
    [root@mojito ~]# ./returnVals.sh

    var1=VALUE , var2=VALUE , var3=VALUE
    As you can see setting the environment variables takes two steps and requires you to remember the name of the variables! Too much work for me.

    Note: “ and $() are the same thing. I personally use $() to call functions and “ to call executables.


 

Subscribe By Email

Enter your email address:

Delivered by FeedBurner

Translate

Donate

Development & maintainance needs time & money.
With your donation you can help us to keep this project alive
Donate:
  Monthly Monthly
Currency
Amount