Welcome to my site. Please CLICK HERE to give your opinions regarding this new look of "PCTipsbyAnu". Thanks for visiting.

Sunday, August 8, 2010

Browse » Home » , , , , » BASH Shell Scripting

BASH Shell Scripting

Shell scripts are small programs comprised of BASH shell commands. Alla shell script really is is a series of BASH commands saved as a textfile and made executable. 


For example: you can type the simple command'whoami'
$ whoami
This command simply echoes the user you are currently logged in as.You could put this command into a text file, and use it as a shellscript quite simply. Echo the command into a text file by using pico.Keep in mind that the conventional extension for shell scripts is'.sh'. To echo the command into a text file you can use the '<' orredirect operator. '>' moves whatever output results from a commandinto a file.

For instance:
$ whoami > tiger.txt
Will create a new file called 'tiger.txt' in your current directorycontaining the output of the whoami command (in this case 'jkeane').You can check the contents using less:
$ less tiger.txt
If you want to put the text 'whoami' into the file you can use the'echo' command. Echo will simply mirror back whatever output you typeafter echo. For instance:
$ echo 'hello world'
hello world
So if we want to create a text file with the command 'whoami' in it we can use:
$ echo 'whoami' > tiger.sh
And we will create a file called 'tiger.sh' containing one line withthe word 'whoami' in it. If you use the '>' command to move moreinformation into yell.sh the original contents will be overwritten. 
$ echo 'whoami' > tiger.sh
$ echo 'ls' > tiger.sh
$ less tiger.sh
If you want to append information to your file without overwritingthe original use the double greater than operator ('>>') and theinformation will be appended without deleting the contents. 

Forinstance, given the contents of our 'tiger.sh' as described above, wecan:
$ echo 'whoami' >> tiger.sh
$ less tiger.sh
And now we have a series of commands in our file. Now, to createa shell script all you have to do is make your file executable. You canuse the 'chmod' to do this:
$ echo 'whoami' > tiger.sh
$ chmod +x tiger.sh
$ ls *.sh -la
-rwxrwxr-x 1 anu007tiger anu007tiger 7 Apr 21 09:16 tiger.sh*
The only problem with this modification is that everyone can now execute your shell script. You're probably safer using:
$ chmod +700 tiger.sh
This makes your file readable(+4), writable(+2) and executable(+1)by the owner (you) and nobody else. Once your file is executable youcan run it. 
You have to use the './' modifier to execute the fileunless the current directory is in your PATH or directories that theshell will search to find executables. 
You can find your PATH variableby taking a look at your .bash_profile (which is found in your homedirectory) or by issuing:
$ echo $PATH
If you want to add your home directory (or any directory for thatmatter) to your $PATH variable you can edit your .bash_profile and adda new line containing 
'PATH $PATH:/home/anu007tiger' (substituting your ownhome directory for /home/anu007tiger) below the line 'PATH=$PATH:$HOME/bin'. 
Assuming you haven't added the directory containing your 'tiger.sh' fileto your $PATH variable you can run it like so:
$ ./tiger.sh
The './' tells BASH to execute the program in the current directory(ignoring your PATH directories) and you'll see the executing 'tiger.sh'simply executes the commands contained in the text file line by line. 
If you want to issue multiple commands on the same line you canseparate them with a semi-colon. 
For instance:
$ pwd ; whoami
If you want to use variables in a shell program you can use them bysimply craeting a variable name (containing letters, numbers andunderscores starting with a letter or underscore) and setting itsvalue. 
For instance:
sets the variable 'MYVAR' and assigns it a value of 1
Note howeverthat we can't do arithmatic directly in the BASH shell. So if youissue:
$ myvar=1
$ myvar2=2
$ echo $myvar+$myvar2
If you want to do math you have to use the 'let' command. 
For instance:
$ let "myvar = myvar + 2"
$ echo $myvar
If you do any extensive shell programming you're also going to wantto be able to use the output from one command in another. To do thisyou use the pipe (|) symbol. 
A pipe moves output from one command to adifferent program or command.
For instance, if you wanted to list thecontents of a directory, but view them using the less program (so youcould scroll up and down using the arrow keys) rather than having themspill onto the screen, you could use a pipe like so:
$ ls -la | less
The pipe moves the output from 'ls -la' to the less command. Similarly you can use pipes and bc to do math in a shell:
$ echo "$myvar + 1" | bc
Now lets actually string together some commands with some flowcontrol to make a real shell script! The first thing we're going to dois open up 'tiger.sh' in a text editor. Next we'll change the contentsto make a small shell script that searches the current directory forthe file 'anu.sh'. Copy the following into your tiger.sh file using vi,emacs, or pico:
if (ls -la | grep "anu.sh"); then
echo "found anu.sh"
echo "can't find anu.sh"

Then run yell.sh:

$ tiger.sh
can't find anu.sh
And you can see the output. What the shell script is doing is usingan if else statement. What we're doing is checking to see if thecommand 'ls -la | grep "anu.sh"' returns any output. 
If it does thenthe shell script will echo 'found anu.sh', otherwise the 'can't findfoo.sh' line will echo back. 
You'll notice that the if statement isfollowed by a semi-colon and a 'then' statement (similar to VisualBasic). The else is fairly obvious, and then the if statement must beclosed with 'fi'. If you have multiple else statements you can use'elseif'.
You can also perform do while loops in shell scripts. A simple dowhile loop can be used to print out numbers 0 through 10. Simply alteryour 'tiger.sh' script to include:
echo $x
while [ $x -lt 11 ]
let "x += 1"
echo $x

The syntax is rather picky so be sure to include all the necessaryspaces and breaks. The only thing that bears explaining in the aboveexample is the '-lt' part. This is the 'less than' operator, '-gt' isgreater than, '-le' is less than or equal and '-ge' is greater than orequal.

Using all of this you should be able to script up a pretty simpleshell script. 
For instance, if you know there are 10 sequentiallynumbered photos (say 'picture1.jpg' through 'picture10.jgp' on awebsite and you want to download them all you could use the followingshell script to grab all the pictures:

while [ $x -le 10 ]
wget $theGo
let "x += 1"
And there you have it :) 
At this point you should understand the power of shell scripts for all sorts of tasks.

Enjoy SCRIPTING ....
You can leave a response, or trackback from your own site.

About 'Anu': My name is 'Anu' also Known as 'ANU 007 TIGER' .I'm administrator of 'PC Tips by Anu' blog .This blog was opened for sharing contents about hacking n cracking.


Post a Comment

Back to Top