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.
L
For example: you can type the simple command'whoami'
$ whoami
anu007tiger
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
anu007tiger
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. Forinstance:
$ echo 'whoami' > tiger.sh
$ echo 'ls' > tiger.sh
$ less tiger.sh
ls
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
ls
whoami
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
anu007tiger
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
/home/anu007tiger
anu007tiger
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:
$ MYVAR=1
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
1+2
If you want to do math you have to use the 'let' command. For instance:
$ let "myvar = myvar + 2"
$ echo $myvar
3
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
2
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"
else
echo "can't find anu.sh"
fi
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:
x=0
echo $x
while [ $x -lt 11 ]
do
let "x += 1"
echo $x
done
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:
And there you have it :)x=1
theURL='http://www.somesite.com/images/picture'
while [ $x -le 10 ]
do
theGo=$theURL$x".jpg"
wget $theGo
let "x += 1"
done
At this point you should understand the power of shell scripts for all sorts of tasks.
Enjoy SCRIPTING ....
0 comments:
Post a Comment