Shell Scripting-8
Shell Scripting-8
Shell Scripting-8
1) Write a shell script to print files of a directory,show logged in user,and your current working
directory?
Shell
2) Interactive Shell script- A worthwhile program needs to talk to the user,so two very basic words
in the shell vocabulary are read (to accept input ) and echo (to display output).
#This is interactive shell script
echo What is your name ?
read name
echo Hello My name is $name ,Happy Programming
Shell Variables
They are an integral part of Shell Programming.They provide the ability to store and manipulate
information within a shell program.You can create and destroy any no of variables as needed to solve
the problem at hand.
echo
read
set
unset
readonly
shift
export
if
fi
else
while
do
done
for
until
case
esac
break
continue
exit
return
trap
wait
eval
exec
ulimit
umask
name=mandar
age =20
dirname=/usr/aa5
(There should be no spaces on either side of =).If the variable already exist,it will store the new value
replacing the old value which the variable contains.)
The list of all system variables and their values can be displayed by saying at the $prompt.
Set command
So, $a=20
$readonly a
When the variables are made readonly ,the shell does not allow us to change their values.All such
variables can be listed by entering readonly at the dollar prompt.
Positional Paramters
When we need to convey information to a program that can be done by specifying arguments at the
command line.
There are some variables defined by the shell.They are nine in number named $1 to $9.
Echo $1 $2 $3 $4 $5
Q1- Write a shell script to accept two filenames from the command line ,copies the first file to second
one and then display it.
cp $1 $2
cat $2
Echo $1 $2 $3 $4 $5
Q3- Write a shell script that reads a filename from the command line and changes the name to
filename.aa1 where aa1 is the logname of the user.
name=$1
set `who am i`
mv $name $name.$1
Displaying date in desired Format
Set `date`
Echo $1 $2 $3 $4
Echo $2 $4 $6
This command is useful when you want to get rid of the command line arguments which are not needed
after parsing them.
!/bin/bash
shift 2
shift
Note- echo $* is used to show the total no of command line arguments passed.
Calculations in Script
#here is an example of floating point arithmetic operations
a=10.5
b=3.5
c=`echo $a+$b|bc`
d=`echo $a-$b|bc`
e=`echo $a\*$b|bc`
f=`echo $a/$b|bc`
echo $c $d $e $f
Practice this- echo “I like work ……\n I can sit and watch it for hours”
Here the cursor waits as the line ends and not on the second line.
Output- 1 2 3
Predefined Variables and Access
All programming languages use variables to retain data for later use or modification. Unlike compiled
languages, most scripting languages do not require a type declaration before a variable is created. The
type is determined by usage. The value of a variable is accessed by preceding the variable name with a
dollar sign. The shell defines several variables it uses for configuration and information like available
printers, search paths, and so on. These are called environment variables.
Let command
Simple Script:-
1. a=5
2. b=10
3. let result=a+b
4. echo $result
For increment- let result++ ,let no+=6
Tee command
Tee command reads the standard input and writes it to both the std output and one or more files.
Alias Command
Alias are like custom shortcuts used to represent a command (or set of commands) executed with or
without custom options.It is like a shortcut command which will have same functionality as if we
are writing the whole command.