Friday, November 6, 2009

Shell scripting 101

I had to automate the build system today. The task was simple however my shell scripting knowledge is somewhat limited. I've done itsy-bitsy work on shell scripting but nothing extensive. Today in the process of automating the build I got to learn a few things, might be trivial but they were learning for me.

  • To check if the file already exists on the filesystem
    [ -f filename ] && echo"file exists"


  • To check if the directory exists on the filesystem
    [ -f dirname ] && echo"dir exists"


  • To copy the output of the command executed in the script to a variable.
    result=`pwd`


  • export command is used to set a environment variable.


  • Use = for comparisons not ==.
    if [ $name = "shiv" ];
    then
    echo "its me"
    fi


  • Use option "o" with unzip command to overwrite existing files without prompting.
    unzip -o myArchive.zip

  • Use option "r" to travel recurcively with zip command
    zip -r zipfilename sourcedir *


Following link points to a quick shell scripting reference.
http://www.opengroup.org/onlinepubs/007908799/xcu/shellix.html

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails