0% found this document useful (0 votes)
2 views

c40exp2OS

Uploaded by

cat3eow9503
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

c40exp2OS

Uploaded by

cat3eow9503
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Experiment No-02

PART A

(PART A : TO BE REFFERED BY STUDENTS)

A.1 Aim:
Write shell scripts to do the following:

a. Display OS version, release number, kernel version


b. Display current logged in user and log name.
c. Display current shell, home directory, operating system type, current path setting, and current
working directory.
d.Display top 10 processes in descending order
e.Display processes with highest memory usage.

A-2 Prerequisite

Knowledge about C programming and basic command of linux /Unix.

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:

>> cat /etc/shells.

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:

>> chmod +x example.csh

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.

Line 2: # the simplest…

- you can add comments, and should frequently, to your scripts if you start the line out with the #
symbol

#!/bin/csh

# Script to print user information who currently login ,

# current date & time

Clear

echo "Hello $USER"

echo "Today is \c "; date

echo "Number of user login : \c" ; who | wc –l

echo "Calendar"

cal

C Shell Variables There are two types of variables

(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

>> echo $HOME

(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

set myvar = super

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

set days = (mon tues wed thurs fri)

echo $days echo $days[3]

done with the @ symbol. Below is a simple example.

#!/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.

There are three types of quotes Quotes

"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

set mydir = `pwd` # set variable to current working directory

@ nr = `awk ‘END {print NR}’ input_file` # what does this do?

@ nfiles = `ls *UU* | wc –l`

As a final note on displaying shell variables it is often useful to concatenate shell variables:

#!/bin/csh

set year = 2010

set month = 12

set day = 30

set output1 = ${year}_${month}_${day}

set output2 = ${year}${month}${day}

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

echo “How many records in this file do you want to skip? “

set nlines = $< echo $nlines

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

set ifile = $argv[1]

echo “Now lets perform some kind of action on file: $ifile”

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:

date > datefile

The output of the date command is saved into the contents of the file,

datefile. a.out < inputfile

The program, a.out receives its input from the input file, inputfile.

sort gradefile >> datafile

The sort command returns its output and appends it to the file, datafile.

A special form of redirection is used in shell scripts.

calculate << END_OF_FILE ... ... END_OF_FILE

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

(PART B : TO BE COMPLETED BY STUDENTS)

(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)

Roll. No. 40 Name: Atharva Shirke

Class SE Batch: C

Date of Experiment: 15-01-25 Date of Submission: 22-01-25

Grade:

B.1 Software Code written by student:

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.

B.4 Question of Curiosity

(To be answered by student based on the practical performed and learning/observations)

Q1. What is the difference between $* and $@?

$*: 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.

Q2. What is the command to display the list of files in a directory?

:- ls

Q3. What are the different types of loops in shell scripting?

:- for,while,untill, select, infinite loop.

Q4. What code would you use in a shell script to determine if a directory exists?

:- directory="path/to/directory"

if [ -d "$directory" ]; then

echo "The directory '$directory' exists."

else

echo "The directory '$directory' does not exist."

fi

You might also like