No strings attached !!! Advance shell scripting

If you are *NIX user say system administrator, software developer , software test engineer or desktop user you must have wrote some form of shell script. A shell script is a “quick and dirty” method of prototyping a complex application. Getting even a limited subset  of the functionality to work in a shell script, even if slowly, is often a useful first stage in project development . This way, the structure of the application can be tested and played with, and the major pitfalls found before  proceeding to the final coding in C, C++, Java, or Perl.

Most of us use shell scripts for just basic command executions. We forgot to explore the power of shell scripts. Text processing is one such task which we find difficult in shell scripts. Surprisingly it is very easy if you are familiar with advance shell scripting techniques.

In this post we will explore such advance features of shell for text processing :
Brace expansion
the shorthand touch file_{one,two,three}.txt to create multiple files file_one.txt, file_two.txt, and file_three.txt. This is known as brace expansion and occurs before any other kind of modification to the command-line.
Tilde expansion
The special character ~ is replaced with the full path contained in the HOME environment variable or the home directory of the users login (if $HOME is null). ~+ is replaced with the current working directory and ~- is replaced with the most recent previous working directory. The last two are rarely used.
Parameter expansion
This refers to expanding anything that begins with a $. Note that $VAR and ${VAR } do exactly the same thing, except in the latter case, VAR can contain non-“whole word” characters that would normally confuse bash.
There are several parameter expansion tricks that you can use to do string manipulation. Most shell programmers never bother with these, probably because they are not well supported by other UNIX systems.

${VAR :-default }
This will result in $VAR unless VAR is unset or null, in which case it will result in default.
${VAR :=default }
Same as previous except that default is also assigned to VAR if it is empty.
${VAR :-default }
This will result in an empty string if VAR is unset or null; otherwise it will result in default. This is the opposite behavior of ${VAR :-default }.
${VAR : ?message }
This will result in $VAR unless VAR is unset or null, in which case an error message containing message is displayed.
${VAR : offset } or ${VAR :n :l }
This produces the nth character of $VAR and then the following l characters. If l is not present, then all characters to the right of the nth character are produced. This is useful for splitting up strings. Try:

echo ${TEXT:10:3}
echo ${TEXT:10}

${#VAR }
Gives the length of $VAR.
${!PRE *}
Gives a list of all variables whose names begin with PRE.
${VAR #pattern }
$VAR is returned with the glob expression pattern removed from the leading part of the string. For instance, ${TEXT#scr} in the above example will return ripting_for_phun.
${VAR ##pattern }
This is the same as the previous expansion except that if pattern contains wild cards, then it will try to match the maximum length of characters.

${VAR %pattern }
The same as ${VAR #pattern } except that characters are removed from the trailing part of the string.

${VAR %%pattern }
The same as ${VAR ##pattern } except that characters are removed from the trailing part of the string.

${VAR /search /replace }
$VAR is returned with the first occurrence of the string search replaced with replace.

${VAR /#search /replace }
Same as ${VAR /search /replace } except that the match is attempted from the leading part of $VAR.

${VAR /%search /replace }
Same as ${VAR /search /replace } except that the match is attempted at the trailing part of $VAR.

${VAR //search /replace }
Same as ${VAR /search /replace } except that all instances of search are replaced.

One thought on “No strings attached !!! Advance shell scripting

  1. seo tips April 25, 2013 at 12:33 AM Reply

    Undeniably believe that which you stated. Your favourite justification appeared to be on the web the simplest factor to have in mind of.
    I say to you, I definitely get annoyed whilst folks consider
    issues that they plainly do not recognize about. You managed to hit the nail upon the highest as well as defined out the entire thing without having side-effects , folks could take a signal.
    Will likely be back to get more. Thanks

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s