0% found this document useful (0 votes)
3 views19 pages

Shellscripts Executed in Class

The document contains a series of shell script commands and their outputs, demonstrating various operations such as arithmetic calculations, conditional statements, loops, and string handling. It includes examples of scripts for user input, age calculations, and file manipulations. Additionally, it highlights the use of control structures like if-else, while, and for loops in shell scripting.

Uploaded by

sahukarisudeer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views19 pages

Shellscripts Executed in Class

The document contains a series of shell script commands and their outputs, demonstrating various operations such as arithmetic calculations, conditional statements, loops, and string handling. It includes examples of scripts for user input, age calculations, and file manipulations. Additionally, it highlights the use of control structures like if-else, while, and for loops in shell scripting.

Uploaded by

sahukarisudeer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Surya Pavan Kumar@Surya ~

$ x=10 y=10; echo 10 + 10


10 + 10

Surya Pavan Kumar@Surya ~


$ x=10 y=10; 10 + 10
-bash: 10: command not found

Surya Pavan Kumar@Surya ~


$ x=10 y=10 10 + 10
-bash: 10: command not found

Surya Pavan Kumar@Surya ~


$ x=10 y=10

Surya Pavan Kumar@Surya ~


$ 10 + 10
-bash: 10: command not found

Surya Pavan Kumar@Surya ~


$ expr 10 +10
expr: syntax error

Surya Pavan Kumar@Surya ~


$ expr 10 + 10
20

Surya Pavan Kumar@Surya ~


$ echo expr 10 + 10
expr 10 + 10

Surya Pavan Kumar@Surya ~


$ echo `expr 10 + 10`
20

Surya Pavan Kumar@Surya ~


$ echo `expr $x + $y`
20
Surya Pavan Kumar@Surya ~
$ echo "expr $x + $y"
expr 10 + 10

Surya Pavan Kumar@Surya ~


$ echo `expr $x / $y`
1

Surya Pavan Kumar@Surya ~


$ echo `expr $x - $y`
0

Surya Pavan Kumar@Surya ~


$ echo `expr $x % $y`
0

Surya Pavan Kumar@Surya ~


$ echo `expr $x * $y`
expr: syntax error

Surya Pavan Kumar@Surya ~


$ echo `expr $x \* $y`
100

Surya Pavan Kumar@Surya ~


$ echo `expr 5 + 3 \* $y`
35

Surya Pavan Kumar@Surya ~


$ echo `expr \(5 + 3) \* $y`
-bash: command substitution: line 1: syntax error near
unexpected token `)'
-bash: command substitution: line 1: `expr \(5 + 3) \* $y'

Surya Pavan Kumar@Surya ~


$ echo `expr \(5 + 3 ) \* $y`
-bash: command substitution: line 1: syntax error near
unexpected token `)'
-bash: command substitution: line 1: `expr \(5 + 3 ) \* $y'

Surya Pavan Kumar@Surya ~


$ echo `expr \(5 + 3 \) \* $y`
expr: non-integer argument

Surya Pavan Kumar@Surya ~


$ echo `expr \( 5 + 3 \) \* $y`
80
$ cat basicif.sh
#!/bin/bash
# basic if statement
x=101
if [ $x -gt 100 ];
then
echo "that's a large number."
pwd
fi

Surya Pavan Kumar@Surya ~/shellscripts


$ sh basicif.sh
that's a large number.
/home/Surya Pavan Kumar/shellscripts

Surya Pavan Kumar@Surya ~/shellscripts


$ cat lab3a.sh
echo " enter user name: "
read name
echo " enter year of birth "
read yob
y=$(date +'%Y')
yd=`expr $y - $yob`
awk -v age=$yd 'BEGIN { if ( age < 18 ) {print ( "you are not
eligble for vote")}
else { print ( " you are eigble "); }}'

Surya Pavan Kumar@Surya ~/shellscripts


$ sh lab3a.sh
enter user name:
pavan
enter year of birth
1980
you are eigble

Surya Pavan Kumar@Surya ~/shellscripts


$ sh lab3a.sh
enter user name:
surya
enter year of birth
2005
you are not eligble for vote

Surya Pavan Kumar@Surya ~


$ cat votecomp.sh
echo -n "Enter the birthdate (mm-dd-yyyy): "
read bdate

bmonth=${bdate:0:2}
bday=${bdate:3:2}
byear=${bdate:6:4}

cdate=`date +%m-%d-%Y`

cmonth=${cdate:0:2}
cday=${cdate:3:2}
cyear=${cdate:6:4}

if [[ "$cmonth" -lt "$bmonth" ]] || [[ "$cmonth" == "$bmonth" &&


"$cday" -lt "$bday" ]]
then
let age=cyear-byear-1
else
let age=cyear-byear
fi

echo "Age = "$age

Surya Pavan Kumar@Surya ~


$ sh votecomp.sh
Enter the birthdate (mm-dd-yyyy): 08-02-1980
votecomp.sh: line 14: [[: 08: value too great for base (error token
is "08")
Age = 41

Surya Pavan Kumar@Surya ~/shellscripts


$ cat ifelse.sh
if [ $# -eq 1 ]
then
nl $1
else
nl /dev/stdin
fi

Surya Pavan Kumar@Surya ~/shellscripts


$ sh ifelse.sh
1
1 1
2
2 2
4
3 4
c
4 c
Surya Pavan Kumar@Surya ~/shellscripts
$ cat ifelifelse.sh
if [ $1 -ge 18 ]
then
echo "you may go to pub"
elif [ $1 -ge 15 ]
then
echo "you may go to party and you should come before midnight
"
else
echo "you may not go to pub"
fi

Surya Pavan Kumar@Surya ~/shellscripts


$ sh ifelifelse.sh 15
you may go to party and you should come before midnight

Surya Pavan Kumar@Surya ~/shellscripts


$ sh ifelifelse.sh 18
you may go to pub

Surya Pavan Kumar@Surya ~/shellscripts


$ sh ifelifelse.sh 14
you may not go to pub

Surya Pavan Kumar@Surya ~/shellscripts


$ cat nestedif.sh
#!/bin/bash
if [ $1 -gt 100 ]
then
echo "thats a large number"
if (( $1 % 2 == 0 ))
then
echo "and is also even number"
fi
fi
Surya Pavan Kumar@Surya ~/shellscripts
$ sh nestedif.sh 102
thats a large number
and is also even number

Surya Pavan Kumar@Surya ~/shellscripts


$ cat while1.sh
n=1
while [ $n -le 10 ]
do
echo $n
(( n++ ))
done

Surya Pavan Kumar@Surya ~/shellscripts


$ sh while1.sh
1
2
3
4
5
6
7
8
9
10

Surya Pavan Kumar@Surya ~/shellscripts


$ cat testfile2
2 28 JAY WIPRO C++
3 45 MOHIT CGI JAVA
ID AGE NAME COMPANY SKILLS
1 33 RAJ TCS JAVA
4 33 SURESH TCS .NET
5 48 VIJAY GOOGLE PYTHON

Surya Pavan Kumar@Surya ~/shellscripts


$ cat while2.sh
#USING echo COMMAND REMOVING EXTRA SPACES
cat $1 | while read k
do
echo $k
done
Surya Pavan Kumar@Surya ~/shellscripts
$ sh while2.sh testfile2
2 28 JAY WIPRO C++
3 45 MOHIT CGI JAVA
ID AGE NAME COMPANY SKILLS
1 33 RAJ TCS JAVA
4 33 SURESH TCS .NET
5 48 VIJAY GOOGLE PYTHON

Surya Pavan Kumar@Surya ~/shellscripts


$ cat testfile2
2 28 JAY WIPRO C++
3 45 MOHIT CGI JAVA
ID AGE NAME COMPANY SKILLS
1 33 RAJ TCS JAVA
4 33 SURESH TCS .NET
5 48 VIJAY GOOGLE PYTHON

Surya Pavan Kumar@Surya ~/shellscripts


$ cat while4.sh
#The break statement is used to terminate the current loop and
passes program
# control to the command that follows the terminated loop.It
usually used to
#terminate the loop when certain condition met. In the following
example, the
#execution of the loop is interrupted once the current iteratd
item is equal to
#2.
i=0
while [ $i -lt 5 ]
do
echo "number: $i"
((i++))
if [[ "$i" == 2 ]]; then
break
fi
done
echo "completed while with break"

Surya Pavan Kumar@Surya ~/shellscripts


$ sh while4.sh
number: 0
number: 1
completed while with break

Surya Pavan Kumar@Surya ~/shellscripts


$ cat whil5.sh
#This continue statement exits the current iteration of a loop and
passes the
#program control to the next itration of the loop.
i=0
while [ $i -lt 5 ]
do
((i++))
if [[ "$i" == '2' ]]; then
continue
fi
echo "number: $i"
done
echo "completed while with continue"

Surya Pavan Kumar@Surya ~/shellscripts


$ sh whil5.sh
number: 1
number: 3
number: 4
number: 5
completed while with continue
Surya Pavan Kumar@Surya ~/shellscripts
$ cat exprstrhandle.sh
if [ `expr "$1" : '.*'` -le 20 ]; then
echo $1
else
echo "large string "
fi

Surya Pavan Kumar@Surya ~/shellscripts


$ sh exprstrhandle.sh pavan
pavan

Surya Pavan Kumar@Surya ~/shellscripts


$ sh exprstrhandle.sh pavan kumar
pavan

Surya Pavan Kumar@Surya ~/shellscripts


$ sh exprstrhandle.sh SuryaPavanKumarGudla
SuryaPavanKumarGudla

Surya Pavan Kumar@Surya ~/shellscripts


$ sh exprstrhandle.sh SuryaPavanKumarGudla1
large string

Surya Pavan Kumar@Surya ~/shellscripts


$ cat while3.sh
# command following exec replaces the current shell, i.e; no
subshell is created only current process is replaced with new
command.
exec 0<$1
read k
n=${#k}
while (( $n > 0 ))
do
h=$(expr "$k" : '.*\(.\)')
echo -n $h
k=$(expr "$k" : '\(.*\).')
(( n = n-1 ))
done
exec 0<&-

Surya Pavan Kumar@Surya ~/shellscripts


$ sh while3.sh new2
ayrus

Surya Pavan Kumar@Surya ~/shellscripts


$ cat for1.sh
for syntax:
for variable in list_of_variables
do
cmd1
cmd2
---
---
done

Surya Pavan Kumar@Surya ~/shellscripts


$ cat for2.sh
#!/bin/bash
for x in 1 2 3 4 5
do
echo “the value of x is $x”
done
Surya Pavan Kumar@Surya ~/shellscripts
$ sh for2.sh
“the value of x is 1”
“the value of x is 2”
“the value of x is 3”
“the value of x is 4”
“the value of x is 5”
Surya Pavan Kumar@Surya ~/shellscripts
$ cat for3.sh
#!/bin/bash
for x in p*
do
echo "File name is $x"
done

Surya Pavan Kumar@Surya ~/shellscripts


$ sh for3.sh
File name is printdate.sh
File name is printtime.sh

Surya Pavan Kumar@Surya ~/shellscripts


$ cat for4.sh
for i in hello 1 \* 2 goodbye
do echo "Looping ... i is set to $i"
done

Surya Pavan Kumar@Surya ~/shellscripts


$ sh for4.sh
Looping ... i is set to hello
Looping ... i is set to 1
Looping ... i is set to *
Looping ... i is set to 2
Looping ... i is set to goodbye

Surya Pavan Kumar@Surya ~/shellscripts


$ cat until1.sh
n=1
until [ $n -gt 10 ]
do
echo $n
(( n++ ))
done

Surya Pavan Kumar@Surya ~/shellscripts


$ sh until1.sh
1
2
3
4
5
6
7
8
9
10

Case word($choice) in

Pattern1 ;;

Pattern2 ;;

--

--

*) default ;;

esac

$0 ,$1,$2, ${10}, $#,$*,$@,$?,$$,$! (positional parameters)


Surya Pavan Kumar@Surya ~
$ cat display.sh
#!/bin/bash
echo display the menu
echo "choose avalue
1. display list of files and dirs
2. display the date
3. display the pwd
4. quit:"
echo 'choose a value?'
read choice
echo
case $choice in
"1") ls ;;
"2") date ;;
"3") pwd ;;
"4") exit ;;
*)
echo wrong choice
esac

Surya Pavan Kumar@Surya ~


$ sh display.sh
display the menu
choose avalue
1. display list of files and dirs
2. display the date
3. display the pwd
4. quit:
choose a value?
1

'Unix is my favourite' file6 greptest test10


clparameters file7 greptest1 test11
commandtest.sh filefogrep hl test12
commtest4 fileforgrep lab3.sh test13
concattestfile fileforgrep2 letter testfile
cutlist1 fileforsed linux testfile2
difftest1.txt fileunq list testfileexp2
difftest2.txt first load.sh testfileforegrep
display.sh first.sh loop.awk trailaa
echo forgrep progs trailab
errorfile forgrep1 progs1 trialaa
file1 forgrep3 sample.sh unix
file2 formore second unix1
file3 foruniqtestfile select1.sh users.sh
file4 getopts1.sh shellscripts votecomp.sh
file5 getopts2.sh sl writread.c

Surya Pavan Kumar@Surya ~


$ sh display.sh
display the menu
choose avalue
1. display list of files and dirs
2. display the date
3. display the pwd
4. quit:
choose a value?
5

wrong choice

while condition 1

do

list of things to be executed

done

Surya Pavan Kumar@Surya ~


$ echo $*

Surya Pavan Kumar@Surya ~


$ set `date`

Surya Pavan Kumar@Surya ~


$ echo $*
Fri Sep 23 09:37:02 IST 2022

Surya Pavan Kumar@Surya ~


$ echo $1 $2 $3
Fri Sep 23

Surya Pavan Kumar@Surya ~


$ shift

Surya Pavan Kumar@Surya ~


$ echo $1 $2 $3
Sep 23 09:37:02

Surya Pavan Kumar@Surya ~


$ echo $1 $2 $3
Sep 23 09:37:02

Surya Pavan Kumar@Surya ~


$ shift

Surya Pavan Kumar@Surya ~


$ echo $1 $2 $3
23 09:37:02 IST

Surya Pavan Kumar@Surya ~


$ set 123 456 789

Surya Pavan Kumar@Surya ~


$ echo \$1 is $1 ,\$2 is $2 , \$3 is $3
$1 is 123 ,$2 is 456 , $3 is 789

Surya Pavan Kumar@Surya ~


$ ls filetest.sh
ls: cannot access 'filetest.sh': No such file or directory

Surya Pavan Kumar@Surya ~


$ cd shellscripts
Surya Pavan Kumar@Surya ~/shellscripts
$ ls
basicif.sh for4.sh myfile1 printdate.sh until1.sh
demoargs.sh ifelifelse.sh nestedif.sh printtime.sh
whil5.sh
exprstrhandle.sh ifelse.sh new1 reversefile.sh
while1.sh
for1.sh lab3a.sh new2 script.sh while2.sh
for2.sh lab4a.sh new3 stringreverse.sh while3.sh
for3.sh myfile palindrome.sh testfile2 while4.sh

Surya Pavan Kumar@Surya ~/shellscripts


$ cd

Surya Pavan Kumar@Surya ~


$ cat script2.sh
#!/bin/bash
# script2.sh
echo "program: $0
The number of arguments specified is $#
The argument are $* "
grep "$1" $2
echo "\n job done"

Surya Pavan Kumar@Surya ~


$ cd shellscripts

Surya Pavan Kumar@Surya ~/shellscripts


$ nano filetest.sh

Surya Pavan Kumar@Surya ~/shellscripts


$ sh filetest.sh testfile
File does not exist

Surya Pavan Kumar@Surya ~/shellscripts


$ sh filetest.sh testfile2
file is both readable and writable

asus@SuryaPavanKumar ~
$ x=3
y=5
echo "scale=4; $y / $x" | bc
1.6666

asus@SuryaPavanKumar ~
$ x=3
y=5
awk "BEGIN {print $y/$x}"
1.66667

asus@SuryaPavanKumar ~
$ x=3
y=5
result=$(echo "scale=6; $y / $x" | bc -l)
printf "%.3f\n" "$result"
1.667

asus@SuryaPavanKumar ~
$ echo `expr $x - $y
>`
-2

asus@SuryaPavanKumar ~
$ echo `expr $x % $y`
3

asus@SuryaPavanKumar ~
$ echo `expr $x / $y`
0

asus@SuryaPavanKumar ~
$ echo `expr $y / $3`
expr: syntax error: missing argument after ‘/’

asus@SuryaPavanKumar ~
$ echo `expr $y / $x`
1
asus@SuryaPavanKumar ~
$ echo `expr $y * $x`
expr: syntax error: unexpected argument ‘1file’

asus@SuryaPavanKumar ~
$ echo `expr $y \* $x`
15

asus@SuryaPavanKumar ~
$ x=3
y=5
echo "scale=4; $x / $y" | bc
.6000

You might also like