

Bash Shell Shortcuts

Special Shell Variables If you like this 10 seconds guide, don't forget to read -

Common environment variables

PATH

HOME

MAIL

IFS

$ echo $IFS | od -bc

PS1

PS2

$

>

$ ls |

USER

TERM

SHELL

Thisguide is not a detailed study but a quick reference to the BASH syntax. So lets begin...- Sets the search path for any executable command. Similar to the PATH variable in MSDOS.- Home directory of the user.- Contains the path to the location where mail addressed to the user is stored. - 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:and- Primary and secondary prompts in bash. PS1 is set toby default and PS2 is set to. To see the secondary prompt, just run the command :... and press enter.- User login name.- indicates the terminal type being used. This should be set correctly for editors like Vim to work correctly.- Determines the type of shell that the user sees on logging in.

To see what are the values held by the above environment variables, just do an echo of the name of the variable preceded with a $ .

$ echo $USER ravi

USER

Some bash shell scripting rules

The first line in your script must be #!/bin/bash

... that is a # (Hash) followed by a ! (bang) 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.

... that is a (Hash) followed by a (bang) 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. 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



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

'if' Statement

if condition_is_true then //execute commands else //execute commands fi

if condition_is_true then //execute commands elif another_condition_is_true then //execute commands else //execute commands fi

if grep "aboutlinux" thisfile.html then echo "Found the word in the file" else echo "Sorry no luck!" fi

if's companion - test

test

-eq - Equal to

- Equal to -lt - Less than

- Less than -gt - Greater than

- Greater than -ge - Greater than or Equal to

- Greater than or Equal to -le - Less than or Equal to

-f file - True if file exists and is a regular file.

- True if file exists and is a regular file. -r file - True if file exists and is readable.

- True if file exists and is readable. -w file - True if file exists and is writable.

- True if file exists and is writable. -x file - True if file exists and is executable.

- True if file exists and is executable. -d file - True if file exists and is a directory.

- True if file exists and is a directory. -s file - True if file exists and has a size greater than zero.

-n str - True if string str is not a null string.

- True if string str is not a null string. -z str - True if string str is a null string.

- True if string str is a null string. str1 == str2 - True if both strings are equal.

- True if both strings are equal. str - True if string str is assigned a value and is not null.

- True if string str is assigned a value and is not null. str1 != str2 - True if both strings are unequal.

- True if both strings are unequal. -s file - True if file exists and has a size greater than zero.

-a - Performs the AND function

- Performs the AND function -o - Performs the OR function

test $d -eq 25 && echo $d

test $s -lt 50 && do_something

if [ $d -eq 25 ] then echo $d fi

if [ $str1 == $str2 ] then //do something fi if [ -n "$str1" -a -n "$str2" ] then echo 'Both $str1 and $str2 are not null' fi

If you are using square brackets [ ] instead of test , then care should be taken to insert a space after the [ and before the ] . 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 expression in pattern1) //execute commands ;; pattern2) //execute commands ;; ... esac

... echo "Enter your option : " read i; case $i in 1) ls -l ;; 2) ps -aux ;; 3) date ;; 4) who ;; 5) exit esac

For example, if I do the following:... I get the value (My login name) which is stored in the environment variableThe 'if' statement evaluates a condition which accompanies its command line.syntax:'if' condition also permits multi-way branching. That is you can evaluate more conditions if the previous condition fails.Example :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:Test also permits the checking of more than one expression in the same line.... which means, if the value in the variable d is equal to 25, print the value. Otherwise don't print anything.In the above example, I have used square brackets instead of the keyword test - which is another way of doing the same thing.... above, I have checked if both strings are not null then execute the echo command.Case statement is the second conditional offered by the shell.Syntax: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:

The last case option need not have ;; but you can provide them if you want.

case `date |cut -d" " -f1` in Mon) commands ;; Tue) commands ;; Wed) commands ;; ... esac

... 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

Looping Statements

while loop

while

while condition_is_true do //execute commands done

while [ $num -gt 100 ] do sleep 5 done

while : do //execute some commands done

break

continue

until loop

until

while

as long as

until false do //execute commands done

... until [ -r myfile ] do sleep 5 done

for loop

for

for variable in list do //execute commands done

... for x in 1 2 3 4 5 do echo "The value of x is $x"; done

for var in $PATH $MAIL $HOME do echo $var done

... for file in *.java do javac $file done

Here is another example:Case can also match more than one pattern with each option.You can also use shell wild-cards for matching patterns.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.loop syntax -Example: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 areand- This keyword causes control to break out of the loop.- This keyword will suspend the execution of all statements following it and switches control to the top of the loop for the next iteration.complementsconstruct in the sense that the loop body here is executed repeatedlythe condition remains false.Syntax:Example:The above code is executed repeatedly until the file myfile can be read.loop syntax :Example:Here the list contains 5 numbers 1 to 5. Here is another example:Suppose you have a directory full of java files and you want to compile those. You can write a script like this:

You can use wildcard expressions in your scripts.

Read Regular Expressions Tutorial to know more.

Special symbols used in BASH scripting

$* - This denotes all the parameters passed to the script at the time of its execution. Which includes $1 , $2 and so on.

- This denotes all the parameters passed to the script at the time of its execution. Which includes , and so on. $0 - Name of the shell script being executed.

- Name of the shell script being executed. $# - Number of arguments specified in the command line.

- Number of arguments specified in the command line. $? - Exit status of the last command.

$ ./my_script.sh linux is a robust OS

$* - will contain the values 'linux','is','a','robust','OS'.

- will contain the values 'linux','is','a','robust','OS'. $0 - will contain the value my_script.sh - the name of the script being executed.

- will contain the value - the name of the script being executed. $# - contains the value 5 - the total number of parameters.

- 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.

- 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'

- contains the value 'linux' $2 - contains the value 'is'

The set and shift statements

set

$ set `date` $ echo $1 $ echo $* $ echo $# $ echo $2

shift

$ set `date` $ echo $1 $2 $3 $ shift $ echo $1 $2 $3 $ shift $ echo $1 $2 $3

$ echo $$ 2667

$ ps -f |grep bash

Make your BASH shell script interactive

read statement

read

#!/bin/sh echo "Enter your name : " read name echo "Hello $name , Have a nice day."

Exit status of the last command

true

false

$?

Resources for more detailed study of the BASH 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 :... 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:... and so on.- Lets you associate values with these positional parameters .For example, try this:- transfers the contents of a positional parameter to its immediate lower numbered one. This goes on as many times it is called.Example :To see the process Id of the current shell, try this:Validate that it is the same value by executing the following command:Make your shell script interactive.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.An example -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 beif it executes successfully, andif it fails. This can be checked in the script using thepositional parameter.Linux Shell Scripting Tutorial @ Cyberciti.biz Introduction to BASH Programming @ Tldp.org BASH FAQ @ Greg's Wiki BASH Pitfalls @ Greg's Wiki Unix shell scripting resource @ Shelldorado I hope you enjoyed reading this10 seconds guide.