Shell Scripting for beginners
[Link]
[Link]
Check below post for more info
[Link]
activity-6834000725916434432-MCV3/
[Link]
activity-6834383915680182273-ElfY/
First you need to find out where is your Bash interpreter located. Enter the following into your command line:
This command reveals that the Bash shell is stored in /bin/bash
$ which bash
/bin/bash
simple hello_world script
Just like in every programming course, we start with a simple hello_world script. The
following script will output Hello World.
echo Hello World
After creating this simple script in vi or with echo, you'll have to chmod +x hello_world to
make it executable. And unless you add the scripts directory to your path, you'll have to type
the path to the script for the shell to be able to find it.
$ echo Hello World > hello_world
$ chmod +x hello_world
$ ./hello_world
Hello World
she-bang
Let's expand our example a little further by putting #!/bin/bash on the first line of the script. The
#! is called a she-bang (sometimes called sha-bang), where the she-bang is the first two
characters of the script.
#!/bin/bash
echo Hello World
You can never be sure which shell a user is running. A script that works flawlessly in bash
might not work in ksh, csh, or dash. To instruct a shell to run your script in a certain shell, you
can start your script with a she-bang followed by the shell it is supposed to run in. This script
will run in a bash shell.
#!/bin/bash
echo -n hello
echo A bash subshell `echo -n hello`
#!/bin/ksh
echo -n hello
echo a Korn subshell `echo -n hello`
This script will run in a Korn shell (unless /bin/ksh is a hard link to /bin/bash). The /etc/
shells file contains a list of shells on your system.
comment
Let's expand our example a little further by adding comment lines.
#!/bin/bash
#
# Hello World Script
#
echo Hello World
variables
Here is a simple example of a variable inside a script.
#!/bin/bash #
# simple variable in script #
var1=4
echo var1 = $var1
Scripts can contain variables, but since scripts are run in their own shell, the variables do not survive the end
of the script.
$ echo $var1
$ ./vars
var1 = 4
$ echo $var1
sourcing a script
Luckily, you can force a script to run in the same shell; this is called sourcing a script.
$ source ./vars
var1 = 4
$ echo $var1
4
The above is identical to the below
$ . ./vars
var1 = 4
$ echo $var1
4
troubleshooting a script
Another way to run a script in a separate shell is by typing bash with the name of the script as a
parameter.
$ cat runme
# the runme script
var4=42
echo $var4
$ bash runme
42
Expanding this to bash -x allows you to see the commands that the shell is executing (after
shell expansion).
$ bash -x runme
+ var4=42
+ echo 42
42
Notice the absence of the commented (#) line, and the replacement of the variable before
execution of echo.
Scripting loops
test [ ]
The test command can test whether something is true or false. Let's start by testing whether 10
is greater than 55.
$ test 10 -gt 55 ; echo $?
The test command returns 1 if the test fails. And as you see in the next screenshot, test returns 0
when a test succeeds.
$ test 56 -gt 55 ; echo $?
If you prefer true and false, then write the test like this.
$test 56 -gt 55 && echo true || echo false
true
$test 6 -gt 55 && echo true || echo false
false
The test command can also be written as square brackets, the screenshot below is identical to
the one above.
$ [ 56 -gt 55 ] && echo true || echo false
true
$ [ 6 -gt 55 ] && echo true || echo false
false
if then else
The if then else construction is about choice. If a certain condition is met, then execute
something, else execute something else. The example below tests whether a file exists, and if
the file exists then a proper message is echoed.
#!/bin/bash
if [ -f [Link] ]
then echo [Link] exists!
else echo [Link] not found!
fi
If we name the above script 'choice', then it executes like this.
[vijay@biradar scripts]$ ./choice
[Link] not found!
[vijay@biradar scripts]$ touch [Link]
[vijay@biradar scripts]$ ./choice
[Link] exists!
[vijay@biradar scripts]$
if then elif
You can nest a new if inside an else with elif. This is a simple example.
#!/bin/bash
count=42
if [ $count -eq 42 ]
then
echo "42 is correct."
elif [ $count -gt 42 ]
then
echo "Too much."
else
echo "Not enough."
fi
for loop
The example below shows the syntax of a classical for loop in bash.
for i in 1 2 4
do
echo $i
done
An example of a for loop combined with an embedded shell.
#!/bin/ksh
for counter in `seq 1 20`
do
echo counting from 1 to 20, now at $counter
sleep 1
done
The same example as above can be written without the embedded shell using the bash
{from..to} shorthand.
#!/bin/bash
for counter in {1..20}
do
echo counting from 1 to 20, now at $counter
sleep 1
done
This for loop uses file globbing (from the shell expansion). Putting the instruction
on the command line has identical functionality.
kahlan@solexp11$ ls
[Link] [Link]
kahlan@solexp11$ for file in *.ksh ; do cp $file $[Link] ; done
kahlan@solexp11$ ls
[Link] [Link] [Link] [Link]
while loop
Below a simple example of a while loop.
i=100;
while [ $i -ge 0 ] ;
do
echo Counting down, from 100 to 0, now at $i;
let i--;
done
Endless loops can be made with while true or while : , where the colon is the
equivalent of no operation in the Korn and bash shells.
#!/bin/ksh
# endless loop
while :
do
echo hello
sleep 1
done
until loop
Below a simple example of an until loop.
let i=100;
until [ $i -le 0 ] ;
do
echo Counting down, from 100 to 1, now at $i;
let i--;
done
Input Output Redirection in Linux
Types of Redirections
1. Overwrite
• “>” standard output
• “<” standard input
2. Appends
• “>>” standard output
• “<<” standard input
3. Merge
• “p >& q” Merges output from stream p with stream q
• “p <& q” Merges input from stream p with stream q
#!/bin/bash
# Author : Vijaykumar S Biradar
# Script follows here:
#with and without standard output eg
echo hello everyone > without_standardoutput.txt
cat without_standardoutput.txt
#redirects standard output to with_standardoutput.txt; This is also works same as above
echo hello everyone 1> with_standardoutput.txt
cat with_standardoutput.txt
echo thanks for reading1 >> without_standardoutput.txt
cat without_standardoutput.txt
echo thanks for reading2 >> without_standardoutput.txt
cat without_standardoutput.txt
echo thanks for reading3 > without_standardoutput.txt
cat without_standardoutput.txt
#redirects stdout to with_standardoutput.txt
echo thanks for reading1 1>> with_standardoutput.txt
cat [Link]
echo thanks for reading2 1>> with_standardoutput.txt
cat [Link]
echo thanks for reading3 1> with_standardoutput.txt
cat with_standardoutput.txt
#standard error eg
ls without_standardoutput.txt with_standardoutput.txt without_standarderror.txt
cat without_standarderror.txt
ls without_standardoutput.txt with_standardoutput.txt without_standarderror.txt 2>
with_standarderror.txt
cat with_standarderror.txt
#standard input eg
cat > with_standardinput.txt <<EOF
This is Shell Scripting Course. We are covering IO Redirections.
Thanks.
EOF
cat with_standardinput.txt
[Link]
Bash scripting cheat sheet