You are on page 1of 4

Lab Manual: Open Source Technology: TE IT Sem V

Prof. A. S. Kunte
September 10, 2014
Experiment Number 05
Aim: Learn Basic Shell Scripting
Requirements: Any PC with Linux installation and Terminal Emulator or Console based Command Line
Objective: We will learn shell scripting basics thorough this practical session.
Procedure: [This write up is organized as a tutorial. Reader needs to executes it step by
step and learns Shell Scripting. All bits must be executed and corresponding
output must be enlisted on left portion of your write up page. On right side
of write up one should copy the tutorial steps enlisted]
This tutorial is written to help student understand some of the basics of shell script
programming, and hopefully to introduce some of the possibilities of simple yet powerful
programming available under the Bash shell.
Bash is a Unix shell written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell (sh). Released in 1989, it has been distributed widely as
the shell for the GNU operating system and as a default shell on Linux and Mac OS X.
It has been ported to Microsoft Windows and distributed with Cygwin and MinGW, to
DOS by the DJGPP project.
Bash stands for Bourne Again Shell, we start with checking which shell currently running
on our terminal. To do so, execute command
$ echo $SHELL
SHELL is the environmental variable that stores current shell inside terminal. echo
command displays the contents of the variable SHELL. To know what all valid login shells
are we need to check contents of file /etc/shells.
$ cat /etc/shells
Execute following set of commands to create and execute your first bash script.

echo "#!/bin/bash" >
echo "echo Hello World" >>
chmod 755

First two commands will create a file called having two lines first being a
comment telling that this script is to be executed on bash shell. The second line displays

" Read command here accepts user input and assigns it to variable MY NAME. We create following script named var.the message Hello World. Like just about every programming language shell programming also has the concept of variables .sh #!/bin/bash echo What is your name? read MY_NAME echo "Hello $MY_NAME . By default we do not have permissions to execute the script in the bash./script − name Create another script file called first. too! echo "Hello World" echo "Hello * World" echo Hello * World echo Hello World echo "Hello" World echo Hello " " World echo "Hello \"*\" World" echo ‘hello‘ world echo ’hello’ world 4. Note that the language is loosely typed and there is no explicit declaration of variable before its Show output and explain it in line or two. read and manipulate its contents. execute $touch first. Now we are ready to execute the script by typing .hope you’re well. var. Note the output of the script 2 . Explain output of above script in one or two lines. Write following commands in the file #!/bin/bash # This is a comment! echo "Hello World" # This is a #!/bin/bash MY_MESSAGE="Hello World" echo $MY_MESSAGE Here we instantiate a variable MY MESSAGE with a message and access it’s content using $variable-name. For that one has to change the file access permissions of the script file using chmod command. first 5 specify that group has read and execute permission (4+1) and same is the case for all other users (4+1). to demonstrate use of variables.a symbolic name for a chunk of memory to which we can assign values. The program asks user to input his name and greets him with message containing his name. execute $gedit first./ 5. Change file access permissions for to do so follow these steps: 1. Execute .sh. execute (4+2+1). Third command checks the content of the file & 3. The option 755 specify that owner can read. Now look at another script ioexample.

/myvar2. ]. we will look at syntax of if if [ .if you assign MY OBFUSCATED VARIABLE=Hello and then display content of MY OSFUCATED VARIABLE. $ ./myvar2. ] then # do something condition is true fi Note that then is present on the next line and semicolon is missing. as they do in languages like C. Alternative syntax for if is if [ . having following content: #!/bin/bash echo "MYVAR is: $MYVAR" MYVAR="hi there" echo "MYVAR is: $MYVAR" After you create this script try following two alternative execution sequences and note the output you see: 1.. This can cause some subtle bugs ... ] then 3 .sh 3.It is important to know more about the scope of the variables. $ MYVAR=hello $ . then you will get nothing displayed as the echoed variable name is misspelled. You get no warnings or $echo $MYVAR Explain briefly what do you understand about variable scope from above output Another important aspect in programming environment is that of flow control statements. create a small shell script. In order to really know what’s going on with your variables.. There is a command called export which has a fundamental effect on the scope of variables. Use of semicolon was to terminate the current line./myvar2. Variables in the Bash need not be declared. you will need to understand something about how this is used. But if you try to read an uninitialized variable... We can also have if then else which has following syntax: if [ . To begin. $ export MYVAR $ . then # do something fi Note the semicolon between closing square bracket and then The test condition is enclosed within square brackets fi tells us the end of if 2. the result is the empty string.

" else echo "This is not a leap year. Now check the use of looping construct for. February has 29 days. then if [ $[$year % 100] -ne 0 ]. February has 28 days. February has 28 days. For as usual looping construct #!/bin/bash for((i-=0. February has 29 days." fi Check if the else if control statement works as expected by changing the value of variable year. Conclusion: After performing this tutorial we have understood basic components for writing shell scripting. then echo "This is a leap year.i<5." elif [ $[$year % 4] -eq 0 ]." fi else echo "This is not a leap year. 4 . year=‘date +%Y‘ if [ $[$year % 400] -eq "0" ]. 1.# if-code else # else-code fi Consider following code segment which shows how we can use if then elif then else ladder to evaluate how many days will February have. we will discuss two examples the first one will use for as iterater and second will be the usual for loop. then echo "This is a leap year.i++)) do echo echo "Loop variable $i" done Run this script and enlist the output. For loop as Iterater #!/bin/bash for i in 1 2 3 4 5 do echo "Loop variable $i" done 2.