You are on page 1of 32

Shell

A shell (command interpreter) is an interface


between the user commands and the kernel
Multiple shells are available in Linux
$ cat /etc/shells
$ chsh
Only Bash (Bourne Again SHell) gives access
to 100% features of Linux
Wildcards
Wildcards are used to match muliple
files/folders
* matches zero or more characters
? matches any single character
[a-z] matches a range of characters
[^a-z] matches all except the range
Special characters
~ tilde character
represents current users home directory
ls /home/student/myfolder
ls ~/myfolder
` back ticks
echo Today's date is `date`
Variables
Local variables and environmental variables
Local: Variables created will remain in the
current shell
Environment: A copy of the variables created
will be passed to child shells
set (display all variables local+environment)
env (display only environment variables)
Create vars
$ A=10
$ B=20
$ export B
Bash startup scripts
Login shell : Shell that is started after a login
action
Non-login shell: Shell that is started without a
login action
Login shell
/etc/profile
/etc/profile.d/
~/.bash_profile
~/.bashrc
/etc/bashrc
Non-login shell
~/.bashrc
/etc/bashrc
/etc/profile.d/
Logout
~/.bash_logout
Simple bash script
$ vi script1.sh

#!/bin/bash
# This is a bash script comment.

echo hello world

$ bash script1.sh

$ chmod +x script1.sh
$ ./script1.sh
sha-bang character
Path to shell(interpreter)
comment
cont...
#!/bin/bash

echo My PID number is $$
echo -n Enter first number:
read A
printf Enter second number:
read B

echo The total is
echo $A+$B | bc -l

C=`expr $A + $B`
printf Total is %d $C


Internal variables
$BASH_VERSION
The version of Bash installed on the system
$FUNCNAME
Name of the current function
$GROUPS
Groups current user belongs to
$HOME
Home directory of the user, usually /home/username
$HOSTNAME
The system host name
$PATH
Path to binaries/commands
$PPID
The $PPID of a process is the process ID (pid) of its parent process.
$PS1
This is the main prompt, seen at the command-line.

arithmetic operators
+
plus
-
minus
*
multiplication
/
division
**
exponentiation

%
modulo, or mod (returns the remainder of an integer division operation)

cont...
#!/bin/bash

read -p Enter marks for eng, sci, math M1 M2 M3
if [ -z $M1 -o -z $M2 -o -z $M3 ]; then
echo enter 3 values
exit 1
fi

let TOTAL=$M1+M2+M3
let AVG=$TOTAL/3
If [ $AVG -gt 90 ]; then
echo Merit
elif [ $AVG -gt 80]; then
echo First class
else
echo Avg is $AVG
fi

exit 0 # exit status with $?
Integer operators
-eq
is equal to
-ne
is not equal to
-gt
is greater than
-ge
is greater than or equal to
-lt
is less than
-le
is less than or equal to


String operators
-n STRING
the length of STRING is nonzero

-z STRING
the length of STRING is zero

STRING1 = STRING2
the strings are equal

STRING1 != STRING2
the strings are not equal

cont...
#!/bin/bash

read -p Enter file name: FILENAME

if [ ! -f $FILENAME ]; then
echo enter filename
exit 1
Fi

case $FILENAME in
*.conf)
echo Seems like a configuration file ;;
*.log)
echo Seems like a log file;;
*)
echo Cant' guess the file type
ease
cont...
#!/bin/bash

for i in 1 2 3 4 5 6
do
echo $i
done

for i in {1..10}
do
echo $i
done

for i in $(seq 1 10)
do
echo $i
done
cont...
#!/bin/bash

A=10
while [ $A -lt 10 ]
do
Echo $A
Let A=$A+1
done
#!/bin/bash
FILES="/usr/sbin/accept
/usr/sbin/pwck
/usr/sbin/chroot
/sbin/ypbind"

for file in $FILES
do
if [ ! -e "$file" ]; then
echo "$file does not exist."; echo
continue
elif [ -d $file ]; then
echo Directory found
break
fi
ls -l $file | awk '{ print "file size: " $5 }'
done
exit 0

File test operators
-d FILE
FILE exists and is a directory
-e FILE
FILE exists
-f FILE
FILE exists and is a regular file
-L FILE
FILE exists and is a symbolic link
-r FILE
FILE exists and read permission is granted
-s FILE
FILE exists and has a size greater than zero
-w FILE
FILE exists and write permission is granted
-x FILE
FILE exists and execute permission is granted

cont...
#!/bin/bash

If [ $# -lt 2 ]; then
echo Usage: $0 filename1 filename2 ...
fi

echo First argument is $1

for i in $*
do
echo passed values are $i
done

#Shift, ${10}
cont...
#!/bin/bash

multiply () {
A=$1
B=$2
T=$A+$B
return $T

}

multiply
RET=$?

cont..
#!/bin/bash

multiply () {
A=$1
B=$2
T=$A+$B
echo $T

}

RET=`multiply`
Strings
${#string}
expr length $string
String Length

expr index $string $substring
Numerical position in $string of first character in $substring that matches.

${string:position:length}
Extracts $length characters of substring from $string at $position.

expr substr $string $position $length
Extracts $length characters from $string starting at $position.

${string#substring}
Deletes shortest match of $substring from front of $string.
${string##substring}
Deletes longest match of $substring from front of $string.
${string%substring}
Deletes shortest match of $substring from back of $string.

cont...
${string/substring/replacement}
Replace first match of $substring with $replacement. [40]
${string//substring/replacement}
Replace all matches of $substring with $replacement.

#!/bin/bash
VAR1=abcABC123ABCabc

echo ${#VAR1}
echo `expr length $VAR1`

echo `expr index "$VAR1" C12`

echo ${VAR1:7:3}
echo `expr substr $VAR1 1 2`

echo ${VAR1#a*C}
echo ${VAR1##a*C}

echo ${VAR1/abc/xyz}
echo ${VAR1//abc/xyz}
pushd/popd
#!/bin/bash

dir1=/home/ubuntu/Music
dir2=/home/ubuntu/Documents

pushd $dir1
echo "Now in directory `pwd`."
pushd $dir2
echo "Now in directory `pwd`."

echo "The top entry in the DIRSTACK array is $DIRSTACK."
echo The entire stack contains `dirs`

popd
echo "Now back in directory `pwd`."
popd
echo "Now back in original working directory `pwd`."

exit 0
tput
Clear screen
tput clear

All text will be in bold
tput bold

All text will be underlined
tput smul

Reset all tput changes
tput sgr0

array
declare -a indices
The variable indices will be treated as an array.

# assigning values to array elements
area[11]=23
area[30]=hello
# ... another way
area2=( zero one two three four )

# accessing array elements
echo ${area[11]}





tput/array
#!/bin/bash
Line[1]="You may have all the knowledge in the world"
Line[2]="but do you have the wisdom to understand it."
Line[3]="Superman"

echo
# Bold print.
tput bold
for index in 1 2
do
printf "%s\n" "${Line[index]}"
done

#..and underline
tput smul
printf "%s\n" "${Line[3]}"

tput sgr0 # reset tput settings

cont...
Debugging
No debugger available
bash -v
bash -x

You might also like