Archive for category shell-scripting

Accept input from console – Basic Shell Script Guide 2

Posted by on Tuesday, 20 April, 2010

Having the ability to get an input command from the input console is a very great tool to have in shell scripting,

So how do we go about doing it?

Here’s the sample that i have created for your reference:

Hint: to use “$#” & $1, $2 …

1. Create the shell script:
#!/bin/sh
n_input=”$#”;
counter=0;

echo “The number of input you give is $n_input”;

while [ "$counter" -le "$n_input" ]
#Note the above must have a space after the [ and before the ].
# -le means less than or equal to
do
echo “$counter”;
$counter++;
done
#We need to denote the done in when using while loop.

2. Next is how we call our script and put in the parameters:

bash# sh myscript.sh arg1 arg2 arg3

Note: In Shell Scripting, we use this hash symbol # , to denote a comment of the entire line. However if the hash symbol is followed by a ! symbol, it means that we are saying to the system that this line is not a comment but rather a special command, which in this case declaration to the system that we are using a shell scrpt. Same applies for the symbol $ followed by # ($#) , it denotes the no of input arguments to the shell script.

-That’s All !

Hope this is usefull for all of you.

Author: Angsari Toni

Basic Shell scripting guide part 1

Posted by on Friday, 5 March, 2010

1. Do the declarations to notify the system that we are going to use a shell script for our code, thus start off with this:
#!/bin/sh

2. Always practise to set your directory paths eg.
insertlogs=”/usr/logs/insertlogs/”;
directorypath=”/usr/www/cronjobs/applications/”;

3. create a function to host each code:
A) Function Creation:
start_time()
{
   echo “== Program is starting at `date` ==” >> “$insertlogs”;
}
end_time()
{
 echo “== Program finish at `date` ==” >> “$insertlogs”;
}
B) Function Calling
start_time;
sleep 10;
end_time;