Shell 1.1 Shell Scripts:: Script
Shell 1.1 Shell Scripts:: Script
Now you save the above content (Escape and : wq) and make this script
executable as follows:
$chmod 755 test.sh
Now you have your shell script ready to be executed as follows:
$./test.sh
This would produce following result:
/home/amrood
index.htm unix-basic_utilities.htm unix-directories.htm
test.sh unix-communication.htm unix-environment.htm
Note: To execute your any program available in current directory you would
execute using ./program_name
Meaning
BASH=/bin/bash
Ourshellname
BASH_VERSION=1.14.7(1)
Ourshellversionname
COLUMNS=80
No.ofcolumnsforourscreen
HOME=/home/vivek
Ourhomedirectory
LINES=25
No.ofcolumnsforourscreen
LOGNAME=students
studentsOurloggingname
OSTYPE=Linux
OurOstype
PATH=/usr/bin:/sbin:/bin:/usr/sbin Ourpathsettings
PS1=[\u@\h\W]\$
Ourpromptsettings
PWD=/home/students/Common
Ourcurrentworkingdirectory
SHELL=/bin/bash
Ourshellname
USERNAME=vivek
UsernamewhoiscurrentlylogintothisPC
Accessing Values:
To access the value stored in a variable, prefix its name with the dollar sign ( $):
For example, following script would access the value of defined variable NAME
and would print it on STDOUT:
vi test.sh
NAME="Zara Ali"
echo $NAME
./test.sh
This would produce following value:
Zara Ali
Arithmetic Operators.
Relational Operators.
Boolean Operators.
String Operators.
The Bourne shell didn't originally have any mechanism to perform simple
arithmetic but it uses external programs, either awk or the must simpler program
expr.
Here is simple example to add two numbers:
Vi test.sh
val=`expr 2 + 2`
echo "Total value : $val"
This would produce following result:
Total value : 4
There are following points to note down:
There must be spaces between operators and expressions for example 2+2 is
not correct, where as it should be written as 2 + 2.
Arithmetic Operators:
There are following arithmetic operators supported by Bourne Shell.
Assume variable a holds 10 and variable b holds 20 then:
Operator Description
Example
==
[ $a == $b ] would return
false.
!=
It is very important to note here that all the conditional expressions would be put
inside square braces with one spaces around them, for example [ $a == $b ] is
correct where as [$a==$b] is incorrect.
All the arithmetical calculations are done using long integers.
Relational Operators:
Bourne Shell supports following relational operators which are specific to numeric
values. These operators would not work for string values unless their value is
numeric.
For example, following operators would work to check a relation between 10 and
20 as well as in between "10" and "20" but not in between "ten" and "twenty".
Assume variable a holds 10 and variable b holds 20 then:
Operator Description
Example
-eq
-ne
-gt
-lt
[ $a -lt $b ] is true.
-ge
-le
It is very important to note here that all the conditional expressions would be put
inside square braces with one spaces around them, for example [ $a <= $b ] is
correct where as [$a <= $b] is incorrect.
Boolean Operators:
There are following boolean operators supported by Bourne Shell.
Example
-o
-a
String Operators:
if yes then
condition becomes
true.
[ $a = $b ] is not true.
!=
-z
-n
Checks if the given string operand size is nonzero. If it is non-zero length then it returns true.
[ -z $a ] is
not false.
str
Example
-b file
-c file
-d file
-f file
-r file
[ -r $file ] is true.
-w file
[ -w $file ] is true.
-x file
[ -x $file ] is true.
[ -b $file ] is false.
-s file
[ -s $file ] is true.
-e file
[ -e $file ] is true.
test statement is used to evaluate condition on its right and returns either true or
false exit status( see section 1.5), which then used by if ( see in next
assignments/notes) for making decisions is used statement. test is used in three
ways :
Example:
Name
Double
Quotes
Meaning
"Double Quotes" - Anything enclose in double quotes
removed meaning of that characters (except \ and $).
'
Single
quotes
Back quote
Example:
$ echo "Today is date"
Can't print message with today's date.
$ echo "Today is `date`".
It will print today's date as, Today is Tue Jan ....,Can you see that the `date`
statement uses back quote?
$ echo $?
It will print 0 to indicate command is successful.
Meaning
Examples
$ ls *
$ ls a*
$ ls ut*.c
$ ls ?
$ ls fo?
$ ls [abc]*
[...]
Note:
[..-..] A pair of characters separated by a minus sign denotes a range.
Example:
$ ls /bin/[a-c]*
Will show all files name beginning with letter a,b or c like
/bin/arch
/bin/awk
/bin/bsh /bin/chmod
/bin/cp
/bin/ash
/bin/basename /bin/cat /bin/chown
/bin/cpio
/bin/ash.static /bin/bash
/bin/chgrp /bin/consolechars /bin/csh
But
$ ls /bin/[!a-o]
$ ls /bin/[^a-o]
If the first character following the [ is a ! or a ^ ,then any character not enclosed is
matched i.e. do not show us file name that beginning with a,b,c,e...o, like
/bin/ps
/bin/rvi
/bin/sleep /bin/touch /bin/view
/bin/pwd
/bin/rview
/bin/sort /bin/true
/bin/wcomp
/bin/red
/bin/sayHello /bin/stty /bin/umount /bin/xconf
/bin/remadmin /bin/sed
/bin/su /bin/uname /bin/ypdomainname
/bin/rm
/bin/setserial /bin/sync /bin/userconf /bin/zcat
/bin/rmdir
/bin/sfxload /bin/tar /bin/usleep
/bin/rpm
/bin/sh
/bin/tcsh /bin/vi
1.8 Pipe
You can connect two commands together so that the output from one program
becomes the input of the next program. Two or more commands connected in this
way form a pipe.
To make a pipe, put a vertical bar (|) on the command line between two commands.
When a program takes its input from another program, performs some operation on
that input, and writes the result to the standard output, it is referred to as a filter.
Syntax:
command1 | command2
Examles:
Command using Pipes
$ ls | more
$ who | sort
$ ls -l | wc -l
$ ls > myfiles
Now if 'myfiles' file exist in your current directory it will be overwritten without
any type of warning.
(2) >> Redirector Symbol
Syntax:
Linux-command >> filename
To output Linux-commands result (output of command or shell script) to END of
file. Note that if file exist , it will be opened and new information/data will be
written to END of file, without losing previous information/data, And if file is not
exist, then new file is created. For e.g. To send output of date command to already
exist file give command
$ date >> myfiles
(3) < Redirector Symbol
Syntax:
Linux-command < filename
To take input to Linux-command from file instead of key-board. For e.g. To take
input for cat command give
$ cat < myfiles
You can also use above redirectors simultaneously as follows
Create text file sname as follows
$cat > sname
vivek
ashish
zebra
babu
Press CTRL + D to save.
Now issue following command.
$ sort < sname > sorted_names
$ cat sorted_names
ashish
babu
vivek
zebra
In above example sort ($ sort < sname > sorted_names) command takes input
from sname file and output of sort command (i.e. sorted names) is redirected to
sorted_names file.
Try one more example to clear your idea:
$ tr "[a-z]" "[A-Z]" < sname > cap_names
$ cat cap_names
VIVEK
ASHISH
ZEBRA
BABU
tr command is used to translate all lower case characters to upper-case letters. It
take input from sname file, and tr's output is redirected to cap_names file.
Future Point : Try following command and find out most important point:
$ sort > new_sorted_names < sname
$ cat new_sorted_names
$0
The name the script was invoked with. This may be a basename without
directory component, or a path name. This variable is not changed with
subsequent shift commands.
$1, The first, second, third, ... command line argument, respectively. The
$2, argument may contain whitespace if the argument was quoted, i.e. "two
$3, ... words".
$#
$@
$*
Example
o
o
The variable $0 is the script's name. The total number of arguments is stored in $#.
The variables $* return all the arguments.
Example
vi script.sh
echo "the $1 eats a $2 every time there is a $3"
echo "bye:-)"