Basics of Creating and Utilizing BASH Scripts

Building a Functions Library:

Whether you use the include statement or just have an inventory of code snippets, you can be consistent
and avoid recreating the wheel by accumulating a library of shell functions, tasks and variables.
A Simple Script to Try:
First lets make a quick script to create some files that we can later manipulate, then we will rename
them. Since these are simple examples, lets cheat a bit here, using the command line instead of a formal script.
Create the original files:
count=0;while [ $count ­lt 10 ]; do touch file_$count;let count=${count}+1; done

We now have 10 files, named file_0 thru file_9
Lets rename them. We could do each separately using:
“mv file_0 new_name_0;mv file_1 new_name_1; … mv file_9 new_name_9”
but that seems like a lot of work and a lot of opportunity for error.
A simpler and more efficient  method would be to create a loop that would rename each file
automatically. This is very similar to the way we created the files. This time though, we will do each
step separately. Note the prompt changes when BASH expects more information before it can complete
the command string. This is called the second level prompt (the primary prompt is a $ for most users
and a # for the root user).
$ for x in `ls`
> do
> n=”`echo $x|cut ­d_ ­f2`”
> mv $x new_file_$n
> done
We now have 10 files, named new_file_0 thru new_file_9
If you now do an up­arrow, the previous command is displayed as:
for x in `ls`; do n=”`echo $x|cut ­d_ ­f2`”; mv $x new_file_$n; done
Whenever doing mass changes, it is prudent to create a test directory to use when building the script or command line, so you do not damage your real source files in the case of an error.

Whenever a command is executed, upon completions a numeric value set which indicates whether the command was successful or not. An exit value of 0 (zero) means success. Any non zero value indicates non­success, usually 1 (one) and usually failure. In the case of a command like grep, and exit code of 1 does not really mean the command failed, but just that it did not find anything that matched the query. An exit code can be forced with a ‘exit’ string such as this example place within an ‘if’ statement:
   …some code…
           echo ”    Operation Failed!”
           exit 1
If the ‘else’ line is matched, then the script exits with a status of 1
You can also test for success of a command by retrieving the exit code, which is represented by the
variable “$?” using something like this:
…some command…
if [ $? ­eq 0 ] ; then
…success, carry on
or, more you may prefer this, slightly more refined method:
…some command…
if [ $RET_VAL ­eq 0 ] ; then
…success, carry on
The thing to remember is that the variable “$?” will be overwritten with the next commands
completion unless you save it into a unique variable as in the second example.

You can store functions in one or more files outside your main script. In order to utilize these
functions, you must have an “include” statement, then you can just call them as you would any other
locally defined function. The include line does not actually have a syntax, it simply reads in additional
Here we are adding the file install_lib, which contains a series of functions.
. src/install_libs  (note the leading ‘dot space’ meaning ‘look in this file plus…’)
Although the BASH shell is included in most Unix and Linux systems, some of the system commands
or utilities are located in different directories. If you want your script to be truly portable, you must
take this into account. Below is one of many ways to do it.
We read a file if the command names, formatted one command per line.
It’s contents may look like this:
Then we search for them and save the output to an include file:
(the shortfall here is the ‘which’  command must be available, a more complex method is used in theFlac Jacket scripts)
while read CMD_VAR
CMD_NAME=”`echo $CMD_VAR|\` which tr\` [:lower:]  [:upper:]`”
echo “${CMD_NAME}=`which  ${CMD_VAR}`” >> command_include_file
done < command_list.txt


Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.