Sunteți pe pagina 1din 13

Bash

First let me clarify that this is not going to be a detailed study of shell
scripting, but as the name of the post indicates, it will be a quick
reference to the syntax used in scripting for the bash shell. So if you
are expecting the former, then you should buy yourself a good book
on shell scripting . So let's move on to the guide. Start your stop
watch now.
-- Start of The 10 secs Guide to Bash Scripting -- Common
environment variables PATH - Sets the search path for any
executable command. Similar to the PATH variable in MSDOS.
HOME - Home directory of the user. MAIL - Contains the path to the
location where mail addressed to the user is stored. IFS - Contains a
string of characters which are used as word seperators in the
command line. The string normally consists of the space, tab and the
newline characters. To see them you will have to do an octal dump as
follows: $ echo $IFS | od -bc PS1 and PS2 - Primary and secondary
prompts in bash. PS1 is set to $ by default and PS2 is set to '>' . To
see the secondary prompt, just run the command :
$ ls |
... and press enter.
USER - User login name. TERM - indicates the terminal type being
used. This should be set correctly for editors like vi to work correctly.
SHELL - Determines the type of shell that the user sees on logging in.
Note: To see what are the values held by the above environment
variables, just do an echo of the name of the variable preceeded with
a $. For example, if I do the following:
$ echo $USER ravi
... I get the value stored in the environment variable USER.
Some bash shell scripting rules 1) The first line in your script must be
#!/bin/bash
... that is a # (Hash) followed by a ! (ban) followed by the path of the
shell. This line lets the environment know the file is a shell script and
the location of the shell.
2) Before executing your script, you should make the script
executable. You do it by using the following command:
$ chmod ugo+x your_shell_script.sh
3) The name of your shell script must end with a .sh . This lets the
user know that the file is a shell script. This is not compulsary but is
the norm.
Conditional statements The 'if' Statement - evaluates a condition
which accompanies its command line. Those words marked in blue
are compulsory. But those marked in red are optional. syntax:
if condition_is_true then execute commands else execute commands
fi
if condition also permits multiway branching. That is you can evaluate
more conditions if the previous condition fails.
if condition_is_true then execute commands elif
another_condition_is_true then execute commands else execute
commands fi
Example :
if grep "linuxhelp" thisfile.html then echo "Found the word in the file"
else echo "Sorry no luck!" fi
if's companion - test test is an internal feature of the shell. test
evaluates the condition placed on its right, and returns either a true or
false exit status. For this purpose, test uses certain operators to
evaluate the condition. They are as follows:
Relational operators
-eq Equal to -lt Less than -gt Greater than -ge Greater than or equal to
-lt Less than -le Less than or equal to
File related tests
-f file True if file exists and is a regular file -r file True if file exists and
is readable -w file True if file exists and is writable -x file True if file
exists and is executable -d file True if file exists and is a directory -s
file True if file exists and has a size greater
than zero.
String tests
-n str True if string str is not a null string -z str True if string str is a null
string str1 == str2 True if both strings are equal str1 != str2 True if
both strings are unequal str True if string str is assigned a value
and is not null.
Test also permits the checking of more than one expression in the
same line.
-a Performs the AND function -o Performs the OR function
Example:
test $d -eq 25 ; echo $d
... which means, if the value in the variable d is equal to 25, print the
value.
test $s -lt 50; do_something
if [ $d -eq 25 ] then echo $d fi
In the above example, I have used square brackets instead of the
keyword test - which is another way of doing the same thing.
if [ $str1 == $str2 ] then
do something
fi
if [ -n "$str1" -a -n "$str2" ] then
echo 'Both $str1 and $str2 are not null'
fi
... above, I have checked if both strings are not null then execute the
echo command.
Things to remember while using test If you are using square brackets
[] instead of test, then care should be taken to insert a space after the
[ and before the ]. Note: test is confined to integer values only.
Decimal values are simply truncated. Do not use wildcards for testing
string equality - they are expanded by the shell to match the files in
your directory rather than the string.
Case statement Case statement is the second conditional offered by
the shell. Syntax:
case expression in pattern1) execute commands ;; pattern2) execute
commands ;; ... esac
The keywords here are in, case and esac. The ';;' is used as option
terminators. The construct also uses ')' to delimit the pattern from the
action.
Example:
... echo "Enter your option : " read i;
case $i in
1) ls -l ;; 2) ps -aux ;; 3) date ;; 4) who ;; 5) exit
esac
Note: The last case option need not have ;; but you can provide them
if you want.
Here is another example:
case date |cut -d" " -f1 in
Mon) commands ;; Tue) commands ;; Wed) commands ;;
..
esac
Case can also match more than one pattern with each option.You can
also use shell wild-cards for matching patterns.
... echo "Do you wish to continue? (y/n)" read $ans
case $ans in
Y|y) ;;
[Yy][Ee][Ss]) ;;
N|n) exit ;;
[Nn][Oo]) exit ;;
) echo "Invalid command"
esac
In the above case, if you enter YeS, YES,yEs and any of its
combinations, it will be matched.
This brings us to the end of conditional statements.
Looping Statements while loop Syntax :
while condition_is_true do
execute commands
done
Example:
while [ $num -gt 100 ] do
sleep 5
done
while : do
execute some commands
done
The above code implements a infinite loop. You could also write 'while
true' instead of 'while :' . Here I would like to introduce two keywords
with respect to looping conditionals. They are break and continue.
break - This keyword causes control to break out of the loop. continue
- This keyword will suspend the execution of all statements following it
and switches control to the top of the loop for the next iteration.
until loop Until complements while construct in the sense that the loop
body here is executed repeatedly as long as the condition remains
false. Syntax:
until false do
execute commands
done
Example:
... until [ -r myfile ] do sleep 5 done
The above code is executed repeatedly until the file myfile can be
read.
for loop Syntax :
for variable in list do execute commands done
Example:
... for x in 1 2 3 4 5 do
echo "The value of x is $x";
done
Here the list contains 5 numbers 1 to 5. Here is another example:
for var in $PATH $MAIL $HOME do
echo $var
done
Suppose you have a directory full of java files and you want to
compile those. You can write a script like this:
... for file in *.java do
javac $file
done
Note: You can use wildcard expressions in your scripts.
A few special symbols and their meanings w.r.t shell scripts
$* - This denotes all the parameters passed to the script
at the time of its execution. Which includes $1, $2 and so on.
$0 - Name of the shell script being executed. $# - Number of
arguments specified in the command line. $? - Exit status of the last
command.
The above symbols are known as positional parameters. Let me
explain the positional parameters with the aid of an example. Suppose
I have a shell script called my_script.sh . Now I execute this script in
the command line as follows :
$ ./my_script.sh linux is a robust OS
... as you can see above, I have passed 5 parameters to the script. In
this scenario, the values of the positional parameters are as follows:
$* - will contain the values 'linux','is','a','robust','OS'. $0 - will contain
the value my_script.sh - the name of the script being executed. $# -
contains the value 5 - the total number of parameters. $$ - contains
the process ID of the current shell. You can use this parameter while
giving unique names to any temporary files that you create at the time
of execution of the shell.
$1 - contains the value 'linux' $2 - contains the value 'is' ... and so on.
The set and shift statements set - Lets you associate values with
these positional parameters . For example, try this:
$ set date $ echo $1 $ echo $* $ echo $# $ echo $2
shift - transfers the contents of a positional parameter to its immediate
lower numbered one. This goes on as many times it is called.
Example :
$ set date $ echo $1 $2 $3 $ shift $ echo $1 $2 $3 $ shift $ echo $1
$2 $3
To see the process Id of the current shell, try this:
$ echo $$ 2667
Validate that it is the same value by executing the following command:
$ ps -f |grep bash
read statement Make your shell script interactive. read will let the user
enter values while the script is being executed. When a program
encounters the read statement, the program pauses at that point.
Input entered through the keyboard id read into the variables following
read, and the program execution continues. Eg:
#!/bin/sh echo "Enter your name : " read name echo "Hello $name ,
Have a nice day."
Exit status of the last command Every command returns a value after
execution. This value is called the exit status or return value of the
command. A command is said to be true if it executes successfully,
and false if it fails. This can be checked in the script using the $?
positional parameter.
Here I have given a concise introduction to the art of bash shell
scripting in Linux. But there is more to shell scripting than what I have
covered. For one, there are different kinds of shells, bash shell being
only one of them. And each shell has a small variation in its syntax.
Like the C shell for example, which uses a syntax close to the C
language for scripting. But what I have covered above applys to all the
shells.
-- End of The 10 secs Guide to Bash Scripting -- Now check how
much time you have taken to cover this much.
What? You say you have taken more than 10 secs to read the guide ?
Then please enter a comment explaining your reasons for taking so
much time and I will send you a free gift as compensation for the extra
time you spent here. But if you have taken less than 10 secs to read
through the guide, please enter a comment explaining how you
accomplished this extraordinary feat. And I will send you a free gift
too.
Don't forget to enter your email Id in your comment if you want to
recieve the free gift. Email Id should be entered in the format: 'emailid
[at] yourmailprovider.com' to save your email from getting harvested
by spam bots.
posted by Ravi @ 8:43 PM 26 comments 26 Comments:
At 10:50 PM, dt said...
I knew most of it, but thanx for going in such a great detail it makes
a great refererence. email: dejan1 [at] gmail.com
At 1:58 AM, Anonymous said...
Great reminder, thanks a lot. Now i'll not need to look for
reminders in "Advanced bash scripting" any more
At 8:51 AM, hater2win said...
Hello, I read the guide and it took me much longer than 10
seconds, but it was worth it. I know a little bit of shell scripting but
this was a good read. Worth every extra second.
roberto[dot]villarreal[at]gmail[dot]com
At 10:47 AM, Ravi said...
roberto, I have sent you the free gift. Please check your mail. Hope
you find it useful.
At 4:40 PM, ?BlackPanther{k} said...
ok it did take me more than 10 seconds, Although I knew most of
it, especially the part abou t Relational Operators was good to re-
read. As per usual, your articles are of a high standard and a
pleasure to read.. blackpanther.blogs [at] gmail.com
At 6:23 PM, Srinivasan said...
Excellent guide. Guess I have to save it and read it offline. And
hey tomorrow is my lab and I have to learn Bash Scripting for it.
Thanks a lot man. srinivasanr [AT] gmail [DOT] com
At 11:58 PM, Anonymous said...
Thanks for the great post! It helped explain a number of questions
that I've had for some time . . . especially "while : " djohngo [at]
gmail.com
At 12:10 AM, reagan said...
hi ravi, awesome post - I finished it in 11 seconds because I got
distracted part of the way through and had to beat HL2. rewilliams
[at] firstam.com
At 12:39 AM, Anonymous said...
Short and crisp.. Very nice.. Regards, Kamesh
At 12:37 PM, logrotate said...
Great post, great blog. I know this pretends to be a 10 sec guide
but maybe it would be worthy if was something like a 15 secs
guide and included a little more reference to functions usage (a
simple example should be enough), let statement and some 2 or 3
important tips to prepare the programmer to the most common
errors. Since it's such few things i took the iniciative to add them
here: !! WARNING !! - i consider myself a rookie on bash scripting.
I can't assure you that everything is absolutely correct however i
can tell you that i'm not inventing all these things.
cat FILE - shows FILE contents (but cat doesn't more than this)
grep pattern FILE - prints line matching a pattern on FILE (FILE
can be the output of another command) tee - Command write to
file and standard out | - Pipe (sends output from the left side as
input to the right one)
> - Redirect output 2> - Redirect Standard errors >> - Concatenate
output to file << - Here Is File for scripts Example:
bthorne@trinity:~$ cat > whatever.txt blah - here i used Ctrl+D
(makes stdin report EOF) to end file bthorne@trinity:~$ cat
whatever.txt blah bthorne@trinity:~$ cat >> whatever.txt bleh
bthorne@trinity:~$ cat whatever.txt blah bleh bthorne@trinity:~$
grep bleh whatever.txt bleh bthorne@trinity:~$ cat whatever.txt
|grep bleh bleh 2.On bash scripting, quotes are more important
than many other languages. See it for your self... One good
example is the difference between $* and "$*".
bthorne@trinity:~$ cat > quotes.sh #!/bin/sh echo 'for a in $*:' for a
in $* do echo $a done echo 'for b in "$*":' for b in "$*" do echo $b
done bthorne@trinity:~$ chmod +x quotes.sh bthorne@trinity:~$
./quotes.sh 1 2 3 for a in $*: 1 2 3 for b in "$*": 1 2 3
bthorne@trinity:~$
Notice on the difference between echo "$var" and echo '$var':
bthorne@trinity:~$ cat > echo.sh #!/bin/sh var="Yellow Ferrari"
echo "i wish i had an $var" echo 'what is the content of $var? i
wish i knew' bthorne@trinity:~$ chmod +x echo.sh
bthorne@trinity:~$ ./echo.sh i wish i had an Yellow Ferrari what is
the content of $var? i wish i knew bthorne@trinity:~$
functions usage syntax (by example):
bthorne@trinity:~$ cat > functions.sh #!/bin/sh function hello () { for
var in $* do echo "hello $var" done } hello "Mary" "Jane" "Popkins"
bthorne@trinity:~$ chmod +x functions.sh bthorne@trinity:~$
./functions.sh hello Mary hello Jane hello Popkins
bthorne@trinity:~$
let statement is used on arithmetic operations evaluation.
Example:
bthorne@trinity:~$ cat > let.sh #!/bin/sh x=2 y=3 let "z=$x+$y"
echo "z = $z" bthorne@trinity:~$ chmod +x let.sh
bthorne@trinity:~$ ./let.sh z = 5 bthorne@trinity:~$
Other useful tip is including files. See example and try to
understand:
bthorne@trinity:~$ cat > let.sh #!/bin/sh export x="2" export y="3"
let "z=$x+$y" echo "z = $z" bthorne@trinity:~$ cat > let2.sh
#!/bin/sh
let.sh let "w=$x*$y" echo "w = $w" bthorne@trinity:~$ chmod +x
let* bthorne@trinity:~$ ./let2.sh z = 5 w = 6 bthorne@trinity:~$
To know more about anything there are the manpages and help.
Try running these... (use 'q' to quit) man bash help set help test
Now you How to Know more
Some useful commands you should learn: cat, ls, grep, cut, watch,
find, tee, wget, ps, at, echo, read, du, df, sleep, less, sed, awk,
head, tail,...
If you copy paste the examples i can't assure it will work because i
noticed this blog f*cks indentation and i can't avoid it because the
html tag PRE was not accepted. Pay attention to that.
Please don't bother the author of the blog if something is incorrect
on my post. I doesn't even know of my existance. If for some
reason you want to contact me, feel free to do it on
blackthorne@ironik.org .

S-ar putea să vă placă și