c40exp2OS
c40exp2OS
PART A
A.1 Aim:
Write shell scripts to do the following:
A-2 Prerequisite
A.3 Outcome
After successful completion students will able to execute shell commands using kernel API’s.
A.4 Theory:
The shell is just a command line interpreter. That is, a shell is really just a computer program that reads
what you type into the terminal and then interprets what to do with it. There are a lot of different shells
your system type:
The most common shells today seem to be the Bourne Again Shell (bash) and the C Shell, but there are
some older ones you might encounter such as the Korn shell. To see which shells are actually available
on your
This means the shell takes the command you type in and then it executes this command. This can be
rather tedious if you w ant to do a larger number of commands in a specific order and maybe do it over
and over again on different sets of data. Write sequence of commands into a text file, and then tell the
shell to run all of the commands in this text file. This text file containing all of our commands is a shell
script. Let’s make a simple one as an example. Open up a new file named example.csh with text editor
and type the following:
#!/bin/csh #
the simplest shell script possible clear echo “geophysics kicks ass” After creating this file, type the
following on the command line:
This will set the permissions for your new file example.csh such that you are allowed to execute it. You
only need to do this once for a new file and not after every time you edit it. Now you can execute the
commands in this text file by typing:
>> ./example.csh
Line 1: #!/bin/csh
- this basically just says that I want to use the C Shell to interpret these commands. Every C Shell script
must start out with this as the top-most line.
- you can add comments, and should frequently, to your scripts if you start the line out with the #
symbol
#!/bin/csh
Clear
echo "Calendar"
cal
(1) System variables – that are created and maintained by the Linux system itself. We saw one
example of these in the example script above: $USER. Another example would be if you wanted to
print out your home could type: directory then you
(2) User defined variables – that are created and maintained by the User.
Variables in a C Shell script is done in two ways:
(a) String variables. String variables are just treated as a bunch of text characters. i.e., you cannot do
math with them. String variables are created with the set command as shown below.
#!/bin/csh
set x = 1
set y = 10.5
echo $x $y $myvar
echo $x + $y
(b) Numeric variables. The C Shell can only handle integer valued numeric variables. Setting variable
names is
@ x = $x + 10
echo $x
(c) Arrays of String Variables. You can also use a single variable name to store an array of strings.
#!/bin/csh
#!/bin/csh
@x=1
A shell is a command language interpreter. Csh is the name of one particular command
interpreter on UNIX. The primary purpose of csh is to translate command lines typed at a terminal into
system actions, such as invocation of other programs. Csh is a user program just like any you might
write. Hopefully, csh will be a very useful program for you in interacting with the UNIX system.
Scripting is a powerful and time-saving tool for performing repetitive jobs, creating data
processing pipelines, and encapsulating useful recipes. A script is a text file containing a set of shell
commands and constructs that perform a routine task. The former can include UNIX commands like
mv, cd, awk, sed; other scripts; and private and Starlink applications. You can create and modify scripts
with a text editor.
"Double Quotes" - Anything enclosed in double quotes removes the meaning of the characters
(except \ and $). For example, if we set arg = blah, then echo “$arg” would result in blah being printed
to the screen.
' Single quotes 'Single quotes' – Text enclosed inside single quotes remains unchanged
(including $variables). For example, echo ‘$arg’ would result in $arg being printed to the screen. That
is, no variable substitution would take place.
` Back quote `Back quote` - To execute a command. For example, `pwd` would execute the print
working directory command. To see the effect of the single or double quote add the following to the
above script:
echo “$minX”.
echo ‘$minX’
The back quote is really useful. This allows us to set a shell variable to the output from a Unix
command:
#!/bin/csh
As a final note on displaying shell variables it is often useful to concatenate shell variables:
#!/bin/csh
set month = 12
set day = 30
echo $output1
echo $output2
mv inputfile ${output1}.txt
Command Line Arguments It is often useful to be able to grab input from the command line or
to read user input. The next example shows a simple way to interactively get information and set the
result to a variable.
#!/bin/csh
To see how command line arguments are handled let’s consider the following example where I
want to read in a filename and then perhaps do some action on this file later.
#!/bin/csh
Redirection of standard output/input The input and output of commands can be sent to or
received from files using redirection. Some examples are shown below:
The output of the date command is saved into the contents of the file,
The program, a.out receives its input from the input file, inputfile.
The sort command returns its output and appends it to the file, datafile.
In this form, the input is taken from the current file (usually the shell script file) until the string
following the << is found. An example of using the program SAC (Seismic Analysis Code) is shown
below (it is becoming more and more of a rarity for people to write SAC macros!):
#!/bin/csh
sac << EOF
r infile.sac
qdp off
ppk
EOF
If the special variable, noclobber is set, any redirection operation that will overwrite an existing file will
generate an error message and the redirection will fail. In order to force an overwrite of an existing file
using redirection, append an exclamation point (!) after the redirection command. For example for the
command: date >! datefile The file datefile will be overwritten regardless of its existence. The output of
one command can be sent to the input of another command. This is called piping. The commands which
are to be piped together are separated by the pipe character. For example: ls -l | sort -k 5n This
command takes the output of the ls -l command and puts the output of it into the sort command.
PART B
(Students must submit the soft copy as per following segments within two hours of the practical. The
soft copy must be uploaded on the Blackboard or emailed to the concerned lab in charge faculties at
the end of the practical in case the there is no Black board access available)
Class SE Batch: C
Grade:
Program
B.2 Input and Output:
B.3 Conclusion:
successfully changed the script's permissions to make it executable and ran it from the current
directory. The outcome or functionality depends on the content of the exp2.sh script, which would
define what it performs when executed.
$*: Treats all arguments as a single string, separated by the first character of the IFS (Internal Field
Separator, usually a space).
$@: Treats each argument as a separate string and preserves them as individual elements.
:- ls
Q4. What code would you use in a shell script to determine if a directory exists?
:- directory="path/to/directory"
if [ -d "$directory" ]; then
else
fi