Objectives: Shell Scripting
Shell Scripting:
A Shell script is a little more than a list of commands that are run in sequence. Conventionally, a shell script should start with a line as the following.
#!/bin/bash
This indicates that the script should be run in the bash shell. This is very inpotant, since the syntax of different shells can vary greatly.
To find all available shells in your system type following command:
$ cat /etc/shells
“echo” Statement In Bash Shell:
This statement will echo the result of the script on command line when you run the script.
Options:
-n: Do not output the trailing new line.
-e: Enable interpretation of the following backslash escaped characters in the strings:
\a: alert (bell)
\b: backspace
\c: suppress trailing new line
\n: new line
\r: carriage return
\t: horizontal tab
\\: backslash
Example1.
echo -e “An apple a day keeps away \a\t\tdoctor\n”
Example2.
echo “Operating system is an interesting subject”
Variables:
Any programming language need variable. There are two type of variable.
- Standard variables
“These are the variables defined by the bash shell itself, we don’t need to defined
it in the script.”
- User defined variables
“User has to define these variables.
The syntax of defining variable is as follows.
Examples.
- x=hello
- x=1
- x=a
- x=”hello world”
As the value of the variable is enclosed in the double quotes is it not always necessary, where you need these quotes is when your variable value includes spaces.
Example:
X=hello world #error
X=”hello world” #ok
Some system variables are listed as follows:
BASH=/bin/bash | Our shell name |
BASH_VERSION=1.14.7(1) | Our shell version name |
COLUMNS=80 | No. of columns for our screen |
HOME=/home/student | Our home directory |
LINES=25 | No. of columns for our screen |
LOGNAME=students | students Our logging name |
OSTYPE=Linux | Our Os type |
PATH=/usr/bin:/sbin:/bin:/usr/sbin | Our path settings |
PS1=[\u@\h \W]\$ | Our prompt settings |
PWD=/home/students/Common | Our current working directory |
SHELL=/bin/bash | Our shell name |
USERNAME= | User name who is currently login to this PC |
Precautions Regarding Variable defining:
This is because the shell essentially sees command line as a pile of commands and arguments that are separated by spaces. For example x=hello will consider as a variable have some value, but the problem with x = hello is the shell sees the world x, separated by spaces and interprets it as a command. Like wise the problem with x=hello world is the x=hello will considered as command which obviously does not make any sense
Referring Value Of Variable:
The value of variable will be referred by using “$” sign following the variable name, which is known as “variable expansion”
Example1.
x=”hello world”
y=$x
echo &y
Single quotes Versus Double Quotes:
Basically variable names are expanded within double quotes, but not within single quotes. If you do not need to refer to variable, single quotes are good to use, as the result are more predictable.
Example1.
echo -n ‘user=’
echo “$USER”
Example2.
echo “user=$USER”
Both of the examples will work same
O/P:
user=student
user=student.
Using Braces To protect your Variable:
Here is a potential problem situation. Suppose. You want to echo the value of the variable
x, followed immediately by the letters “abc”.
Example.
x=abc
echo “$xabc”
This gives no output? The answer is that shell thought that we were asking for the variable xabc, which is uninitiated .The way to deal with this, is to put braces around x to separate it from the other character. The following gives the desired result.
x=abc
echo “${x}abc”
Quotes | Name | Meaning |
“ | Double Quotes | “Double Quotes” – Anything enclose in double quotes removed meaning of that characters (except \ and $). |
‘ | Single quotes | ‘Single quotes’ – Enclosed in single quotes remains unchanged. |
Bash Arithmetic:
Operators:
- +
- –
- *
- /
- %
“expr” Statement in arithmetic:
expr is used in order evaluate any arithmetic statement .
Example1.
x=3+4
Echo $x
# the output of this will be 3+4
Example2.
x= `expr 3 + 4` # see expr statement is enclosed in back quotes
echo $x
# the output is 7
How to Run you script:
./yourscriptname
“/” Indicates that from within your current directory (present working directory PWD) run this script
Task to be performed:
Q1.Print this “this is my my first shell script”
Q2.How to print sum of two numbers, let’s say 6 and 3?
Q3.How to define two variable x=20, y=5 and then to print division of x and y (i.e. x/y)
Q4.Modify above and store division of x and y to variable called z
Q5.How to define variable x with value 10 and print it on screen.
Q.6.How to define variable xn with value “Operating system” and print it on screen
Q7.Generate the following output
My name is your name
My Operating system is Linux
I am using the bash shell.
(Hint: make use of user-defined variable, standered variable for name of operating system, and shell name.)
Q8.Generate following output.
“Hello, (username) .I wish to list some files of yours
Listing files in the current directory.(name of your present working directory)”
List of the directory
(Hint: for user name use standard variable, for present working directory use standard
variable PWD and for list of directory use ls command
Q9. Print your home directory location