shell script n.
Skip this Video
Loading SlideShow in 5 Seconds..
Shell Script PowerPoint Presentation
Download Presentation
Shell Script

Loading in 2 Seconds...

play fullscreen
1 / 25

Shell Script - PowerPoint PPT Presentation

  • Uploaded on

Shell Script. Assignment 1. Shell. It is called a "shell" because it hides the details of the underlying OS behind the shell's interface. In the Unix operating system users can select which shell they want to use. Example – Bourne Shell, C Shell etc.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Shell Script' - tristessa

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
shell script

Shell Script

Assignment 1

  • It is called a "shell" because it hides the details of the underlying OS behind the shell's interface.
  • In the Unix operating system users can select which shell they want to use.
  • Example – Bourne Shell, C Shell etc.
  • Programming with shell – A shell script is a file that contains commands to be executed by the shell.
shell programming
Shell Programming
  • LogIn :
    • Log in to any Linux machine of CS213 using PuTTy. Your program SHOULD RUN on any of those machines. 
    • Eg:
  • Text Editor: ‘VI EDITOR’
    • Open a file : vim
    • Run a file :
      • sh  (no need for execute permission)
      • chmodu+x filename (add execute permission)


basic shell programming
Basic Shell Programming
  • #![path to shell you want to use]
    • eg. #!/bin/sh: current script should be run by the Bourne shell
  • #: begins a comment
  • Variables:
    • Assignment: variablename=5 (no space between)
    • Usage: $variablename
  • Reading from the keyboard
    • readvariablename
basic shell programming1
Basic Shell Programming
  • Pipe: |
    • eg. program 1 | program2 (send output of 1 to 2)
  • Redirection:
    • > : redirect standard output
      • eg. program1 > file1 (put output of program1 to file1)
    • >>: appends standard output
    • < : redirect standard input
basic shell programming2
Basic Shell Programming
  • Reading from a file:
    • cat filename | while readvariablename



    • while readvariablename


done < /directory/filename

useful commands in shell
Useful commands in shell
  • ls: display all file and directory names. Just like dir command in MS-dos.
  • mkdir: help to create the directory
  • cd: helps to change directory
  • pwd: helps to display current path
  • chmod: change mode of permission
  • echo: display the message
  • date: display the current date
  • who: output the currently logged in users’ info
useful commands in shell1
Useful commands in shell
  • ps: reports a snapshot of current process, give details of all the user
  • touch: update the access and modification times of each file to current time
  • grep: searches the named input files (or standard input if no files are named) for lines containing a match to the given pattern
    • eg. grepaa file1
  • cat: create, append new data and display the contain of a file
useful commands in shell2
Useful commands in shell
  • read: take input through keyboard
  • cmp: compares files byte by byte
  • cut: slice a file vertically, is used to output a column of data
    • eg. cat file1 | cut –f1 –d‘||’
  • awk: search for and process patterns in a file. When awk reads in a line, the first field can be referred to as “$1”, the second “$2”. The whole line is “$0”
    • eg. cat file1 | awk ‘{print $1}’
logic of the assignment 1
Logic of the Assignment 1
  • Read the option that the user wants to go with –
    • 1 for printing ancestry tree of the shell script you are running
    • 2 for printing online usernames
    • 3 for printing what process any user is running
    • a separate option for quitting the program.
  • Execute the option that is chosen by the user
  • Example run:
  • Better to use “case” for the menu
case statement
Case Statement
  • Syntax:

case $variableName in











part 1
Part 1
  • Print the ancestry tree of the currently running process (i.e. your shell script). Print is like a tree.










  • Find out, store and print the current process ID and its parent process ID. You can use ps –ef(-ef for standard syntax), and awk to print the required field - procees ID, program name (optional).
  • Store all the process details (output of ps) in a file (say file1)
  • In an iterative loop, check if the PID field of any entry matches the parent ID. If so, print the PPID field of that entry, and continue the same search with this PPID until reach init()

currentProcessID=previous parentProcessID

While ( currentProcessID NOT equal to 1 )

If (currentProcessID IS equal to ProcessID) in file1

Print parentProcessID field of that entry from file1


End If

End while

useful syntax using awk
Useful syntax using awk:
  • awk ‘{if(condition) statement}’
  • currentProcessID=$(awk ‘{if(currentID field matches ProcessID)print;}’ file1 | print parentProcessID field)
part 2
Part 2
  • Figure out which users are online (print only username)
  • Hints: only one command (who command in conjunction with the cut command)
part 3
Part 3
  • Figure out what processes any user is running (Print Process details, not only name or ID)
  • Update the access and modification times of each file using touch command(optional)
  • Read and print the entire userlist
  • Prompt to select one particular user and read the selected user
read and print entire userlist
Read and print entire userlist
  • Read who is online (who command)
  • While (read the users online)

var assign the current user

either print the var directly or write varin file1….the 2nd option is preferred.

End of while

  • Print file1 (in case you have saved it in file1)
get the list of currently online user
Get the list of currently online user

who | while read onlineuser


echo $onlineuser | cut –f1 –d‘ ’ >>userlist


print numbered list of online users
Print numbered list of online users


while read onlineuser


print “$index-$onlineuser”

Like before, save $index $onlineuser in a new file

Increase index by 1

done < userlist

select to see particular user
Select to see particular user
  • Remove userlist
  • Read numbered user from newfile and select one user using input 1 or 2 or …

desireduser=$(open newfile | pattern match with $choice | cut the 2nd field as o/p)

  • Get the process details of desired user
    • ps –ef | grep $desireduser (you will get extra processes)
    • ps -ef | while read process

do compare username

  • Remove newfile
  • When you execute the program, don’t forget to script your session in a file called mySession1.
    • Use the command: script mySession1.
  • This will store your sample run in the file mySession1.
  • After run, don’t forget to exit, which will save and commit this sample file.
  • Submit this file along with your solution in dropbox.
cs284 program submission
CS284 Program Submission
  • Follow instructions on the class website:



Any Questions?