Bash 2
Bash 2
Type 📒 Lecture
Date @February 22, 2022
Lecture # ?
Lecture
URL
Notion https://21f1003586.notion.site/bash-
URL 60e8a6fdb5394c4385d7ac79f7c7b902
Week # 5
bash 1
Script location
Use absolute path or relative path while executing the script
bash environment
When you are already logged in to a system and using the GNOME environment
to open a terminal, then the shell we are opening is not asking for a login and is
called a Non-login shell
When we ssh into a remote machine, then the shell we are logging into would
have checked for the login credentials and then opened up the command line
environment for us, therefore that shell is called a Login shell
bash 2
echo
printf
Command substitution
var=`command`
var=$(command)
for do loop
bash 3
commands
done
case statement
case var in
pattern1)
commands
;;
pattern2)
commands
;;
esac
if loop
if condition
then
commands
fi
if condition; then
commands
fi
conditions in if
True, if the file exists in the current directory of the shell script
else False
bash 4
[ -e file ] or any [ expression ]
If the command returns True, i.e. upon successful execution, this means the
condition is satisfied
else False
Check if n1 is equal to n2
bash 5
Check if n1 is not equal to n2
$str1 != $str2
-n $str1
-z $str1
-d file
-f file
-r file
-s file
bash 6
-w file
-x file
-O file
-G file
Check if the file exists and the default group is the same as that of the
current user
while do loop
while condition
do
commands
done
until do loop
until condition
do
commands
done
functions
bash 7
definition
myfunc()
{
commands
}
call
myfunc
Debugging
set -x
./myscript.sh
Combining conditions
[ $a -gt 3 ] && [ $a -lt 7 ]
[ $a -le 3 ] || [ $a -ge 7 ]
Shell arithmetic
bash 8
expr command operators
bash 9
#!/bin/bash
if [ $# -lt 2 ]; then
echo "Use 2 natural numbers as arguments";
exit 1;
fi;
regex='^[0-9]+$'
if ! [[ $1 =~ $regex ]]; then
echo "$1 is not a natural number";
exit 1;
fi;
let a=$1*$2;
echo "Product a is $a";
(( a++ ));
echo "Product a incremented is $a";
let b=$1**$2;
echo "Power is $b";
c=$[ $1 + $2 + 10 ];
echo "sum + 10 is $c";
d=$(expr $1 + $2 + 20);
echo "sum + 20 is $d";
f=$(( $1 * $2 * 2 ));
echo "Product times 2 is $f";
bash 10
#!/bin/bash
# The following line is for debugging
# set -x;
ans=$( expr $a + $b );
echo $ans;
ans=$( expr $a - $b );
echo $ans;
ans=$( expr $a \* $b );
echo $ans;
ans=$( expr $a / $b );
echo $ans;
ans=$( expr $a % $c );
echo $ans;
ans=$( expr $a = $b );
echo $ans;
ans=$( expr $a != $b );
echo $ans;
ans=$( expr $a \| $b );
echo $ans;
bash 11
ans=$( expr index "$str" "vw" );
echo $ans;
heredoc feature
When writing shell scripts you may be in a situation where you need to pass a
multiline block of text or code to an interactive command, such as tee , cat , or sftp
In bash and other shells like zsh , a Here document ( heredoc ) is a type of redirection
that allows you to pass multiple lines of input to a command.
#!/bin/bash
# set -x;
echo "path is set as $PATH";
i=0;
IFS=:;
for n in $PATH; do
echo "$i $n";
(( i++ ));
done;
bash 12
if-elif-else-fi loop
#!/bin/bash
if [ $# -gt 2 ]; then
echo "More than 2 arguments";
elif [ $# -gt 1 ]; then
echo "More than 1 argument";
elif [ $# -gt 0 ]; then
echo "Not enough arguments";
else
echo "Arguments required";
fi;
bash 13
#!/bin/bash
case $pname in
[gG]imp | inkscape)
echo "Good choice";
;;
[aA]dobe*)
echo "Absolutely proprietary and costs a lot";
;;
imagej)
echo "Measuring things on the image?";
;;
*)
echo "$pname is a new find for me";
;;
esac;
bash 14
#!/bin/bash
begin=1;
finish=10;
#!/bin/bash
begin1=1;
begin2=20;
finish=10;
bash 15
NOTE: Output of the loop is re-directed to the tmp file
#!/bin/bash
filename=largefile.txt;
if [ -e $filename ]; then
echo "file $filename exists";
exit 1;
fi;
i=1;
while [ $i -lt 10 ]; do
echo "$i $[$i+1]";
(( i++ ));
done > $filename;
break
bash 16
break 2 refers to the outer loop as seen from the nesting
continue
Opposite of break
bash 17
shift
#!/bin/bash
while [ -n "$1" ]; do
echo "arg $i is $1";
shift;
(( i++ ));
done;
exec
exec ./my-executable --my-options --my-args
bash 18
To replace shell with a new program or to change i/o settings
If new program is launched successfully, it will not return control back to the shell
#!/bin/bash
echo "PID of shell running this command: $$";
echo "Leaving bash and opening xterm if available";
exec xterm;
echo "Looks like xterm is not available or failed to start";
eval
eval my-arg
#!/bin/bash
cmd="date";
fmt="+%d-%B-%Y";
eval $cmd $fmt;
Functions in bash
#!/bin/bash
usage() {
echo "usage $1 str1 str2";
}
swap() {
echo "$2 $1";
}
if [ $# -lt 2 ]; then
usage $0;
exit 1;
fi;
swap $1 $2;
getopts
bash 19
#!/bin/bash
while getopts "ab:c:" options; do
case "${options}" in
b)
barg=${OPTARG};
echo "accepted: -b $barg";
;;
c)
carg=${OPTARG};
echo "accepted: -c $carg";
;;
a)
echo "accepted: -b";
;;
*)
echo "Usage: -a -b barg -c carg";
;;
esac;
done;
Usage
bash 20
select loop
#!/bin/bash
bash 21
esac;
done;
bash 22