2. 2
A script is a list of system commands stored in a
file.
Steps to write a script :-
Use any editor like vi or vim
chmod permission your-script-name.
Examples:-
$ chmod +x <filename.sh>
$ chmod 755 <filename.sh>
Execute your script as:
$ bash filename.sh
$ bash fileneme.sh
$ ./filename.sh
3. 3
My first shell script
clear
echo “hello world“
$ ./first
$ chmod 755 first
$ ./first
Variables in Shell:
In Linux (Shell), there are two types of variable:
(1) System variables :
(2) User defined variables :
4. 4
$ echo $USERNAME
$ echo $HOME
User defined variables :
variable name=value
Examples:
$x = 10
echo Command:
echo command to display text
7. $ ps
PID TTY TIME CMD
$ ps -ef
UID PID PPID C STIME TTY TIME CMD
$ ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME
CMD
$ ps -efH
UID PID PPID C STIME TTY TIME CMD
7
8. Creating files:
$ touch test1
$ ls -il test1
Copying files:
cp source destination
cp test1 test2
Linking files:
There are two different types of file links in Linux:
a. A symbolic, or soft, link
b. A hard link
8
9. " Double
Quotes
Double Quotes" -
Anything enclose in
double quotes removed
meaning of that
characters (except and
$).
' Single quotes 'Single quotes' - Enclosed in
single quotes remains
unchanged.
` Back quote `Back quote` - To execute
command
9
10. Pipes:
who | wc –l
Reading from Files:
$ read message
$ echo $message
Read command to read lines from files
Command substitution:
Var=`date`
Var=$(date)
Background Processes:
ls -R /tmp &
1
0
11. while read ip name alias
do
if [ ! -z “$name” ]; then
# Use echo -en here to suppress ending the line;
# aliases may still be added
echo -en “IP is $ip - its name is $name”
if [ ! -z “$aliases” ]; then
echo “ Aliases: $aliases”
else
# Just echo a blank line
echo
fi
fi
done < /etc/hosts
1
1
12. Stopping processes
kill pid
disk space
$ df
$ df –h
Disk usages:
$ du
Commands:
$ cat file1
$ sort file1
$ cat file2
$ sort file2
$ sort -n file2
1
2
13. grep [options] pattern [file]
The grep command searches either the input
or the file you specify for lines that contain
characters that match the specified pattern.
The output from grep is the lines that contain
the matching pattern.
1
3
14. $ cat random.sh
#!/bin/bash
MIN=200
MAX=500
let “scope = $MAX - $MIN”
if [ “$scope” -le “0” ]; then
echo “Error - MAX is less than MIN!”
fi
for i in `seq 1 10`
do
let result=”$RANDOM % $scope + $MIN”
echo “A random number between $MIN and $MAX is $result”
Done
$ ./random.sh
1
4
15. $ cat hypotenuse.sh
#!/bin/sh
# calculate the length of the hypotenuse of a Pythagorean
triangle
# using hypotenuse^2 = adjacent^2 + opposite^2
echo -n “Enter the Adjacent length: “
read adjacent
echo -n “Enter the Opposite length: “
read opposite
osquared=$(($opposite ** 2)) # get o^2
asquared=$(($adjacent ** 2)) # get a^2
hsquared=$(($osquered + $asquared)) # h^2 = a^2 + o^2
hypotenuse=`echo “scale=3;sqrt ($hsquared)” | bc`
# bc does sqrt
echo “The Hypotenuse is $hypotenuse”
1
5
16. There are two types of environment variables in the
bash shell
Global variables
Local variables
1
6
17. An array is a variable that can hold multiple values.
To set multiple values for an environment variable, just list
them in parentheses, with each value
separated by a space:
$ mytest=(one two three four five)
$
Not much excitement there. If you try to display the array
as a normal environment variable,
you’ll be disappointed:
$ echo $mytest
one
$
Only the first value in the array appears. To reference an
individual array element, you must use
a numerical index value, which represents its place in the
array. The numeric value is enclosed in
square brackets:
$ echo ${mytest[2]}
three
$
1
7
18. $ date ; who
$ chmod u+x test1
$ ./test1
$ echo This is a test
This is a test
$ echo Let’s see if this’ll work
Lets see if thisll work
$
1
8
19. One of the most useful features of shell scripts is the lowly
back quote character, usually called the
backtick (`) in the Linux world.
You must surround the entire command line command with
backtick characters:
testing=`date`
$ cat test5
#!/bin/bash
# using the backtick character
testing=`date`
echo "The date and time are: " $testing
$
1
9
20. Output redirection
The most basic type of redirection is sending output from a
command to a file. The bash shell uses the greater-than
symbol for this:
command > outputfile
Input redirection
Input redirection is the opposite of output redirection
The input redirection symbol is the less-than symbol (<):
command < inputfile
2
0
21. $ expr 1 + 5
6
The bash shell includes the expr command to stay
compatible with the Bourne shell; however, it
also provides a much easier way of performing mathematical
equations
$ var1=$[1 + 5]
$ echo $var1
6
$ var2 = $[$var1 * 2]
$ echo $var2
12
$
2
1