For premium DevOps and AWS Courses: https://imojo.in/2cox7em
- AWS with the project
- DevOps Real-Time
- DevOps with the project
- AWS Real-Time
- Linux Admin
Running shell script
To
run our script, enter:
1.
$ ./script.sh
The dot slash (./) denotes the current directory. This is necessary because the current directory is usually not in $PATH.
The dot slash (./) denotes the current directory. This is necessary because the current directory is usually not in $PATH.
2. $ bash script.sh
3.
$ source script.sh
The file need not be executable but it must be a valid shell script. The file can be in current directory or in a directory in $PATH.
The file need not be executable but it must be a valid shell script. The file can be in current directory or in a directory in $PATH.
4.
$ . script.sh
Bash defined source as an alias to the dot command.
Bash defined source as an alias to the dot command.
Note
that sourcing will run the commands in the current shell process while bash
executing will run the commands in a new shell process.
For
loop - print odd numbers from 1 to 10
Output
should look like this:
1 3 5 7 9
Shell
1
#!/bin/bash
for i in 1 3 5 7 9
do
echo -n "$i "
done
echo
Shell
2
#!/bin/bash
for i in {1..9..2}
do
echo -n "$i "
done
echo
Shell
3
#!/bin/bash
for ((i=1; i<10;
i+=2))
do
echo -n "$i "
done
echo
Shell
4
#!/bin/bash
for i in {1..10}
do
out=$(( $i % 2 ))
if [ $out -eq 1 ]
then
echo -n "$i "
fi
done
Ruby
Using
"print" for no-new-line, the "puts" for a new line:
i = 1
loop do
break if i > 10
print "#{i} " unless i % 2 == 0
i += 2
end
puts
Bash
- delete the last line of files in a sub directory
We'll
use 'vi' in the script:
#!/bin/bash
for file in sub/*
do
if [ -f $file ]
then
vi -c '$d' -c 'wq' "$file"
fi
done
The
bash script will remove the last line from all files in a 'sub' directory.
Bash
- Replacing a string in multiple files
When
we need to replace a string across multiple files, we can use sed and
the coding is similar to the one we used in the previous section:
#!/bin/bash
for file in ./*
do
if [ -f $file ]
then
sed -i 's/2016/2017/g'
"$file"
fi
done
Note
that we may tempted to do it with a single line with the "sed" command
like this:
sed -i s/2016/2017/g *
But
it won't work when we meet a directory. That's why we need to check if it is a
file or not.
Bash
- if statement
To be
more familiarized with if statement, try the following script:
#!/bin/bash
TWO=2
for (( i=1;i<=3;i++
))
do
if [ ${i} -lt $TWO ];
then
echo "${i} is less then $TWO"
elif [ ${i} -eq $TWO ];
then
echo "${i} is equal to $TWO"
else
echo "${i} is greater than $TWO"
fi
done
Output:
1 is less then 2
2 is equal to 2
3 is greater than 2
To
check if a string is not null nor a space:
#!/bin/bash
str1="Not Null"
str2=" "
str3=""
message="is not
Null nor a space"
if [ ! -z
"$str1" -a "$str1" != " " ]; then
echo "str1 ${message}"
fi
if [ ! -z
"$str2" -a "$str2" != " " ]; then
echo "str2 ${message}"
fi
if [ ! -z
"$str3" -a "$str3" != " " ]; then
echo "str3 ${message}"
fi
Output:
str1 is not Null nor a
space
Here
document
The
syntax looks like this:
command << delimiter document delimiter
The
shell interprets the << operator as an instruction to read input until it finds a
line containing the specified delimiter. All the input lines up
to the line containing the delimiter are then fed into
the standard input of the command.
Example
1:
$ wc -l << EOF
This is a simple lookup program
for good (and bad) restaurants
in Cape Town.
EOF
3
$
Example
2:
cat << EOF >
/etc/apt/sources.list.d/kubernetes.list
deb
http://apt.kubernetes.io/ kubernetes-xenial main
EOF
The
command appends "deb http://apt.kubernetes.io/ kubernetes-xenial
main" string to "/etc/apt/sources.list.d/kubernetes.list" file.
Example
3:
$ sed 's/_/-/g' << EOF
2013_4_5
2013_4_6
EOF
2013-4-5
2013-4-6