270 likes | 304 Views
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.
E N D
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. • Programming with shell – A shell script is a file that contains commands to be executed by the 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 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. grep aa file1 • cat: create, append new data and display the content of a file
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}’
Shell Programming • LogIn : • Log in to any Linux machine of CS213 using PuTTy. Your program SHOULD RUN on any of those machines. • Eg: rc16xcs213.managed.mst.edu • Text Editor: ‘VI EDITOR’ • Open a file : vim fileneame.sh or vi filename.sh • Run a file : • sh filename.sh (no need to give execute permission) • chmod u+x filename (add execute permission) filename
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 • read variablename
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 Programming • Reading from a file: • cat filename | while read variablename do … done • while read variablename do … done < /directory/filename
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: • http://web.mst.edu/~ercal/284/Asg-1/assignment1run.txt • Better to use “case” for the menu
Case Statement • Syntax: case $variableName in pattern1) command ;; pattern2) command ;; *) command ;; esac
Part 1 • Print the ancestry tree of the currently running process (i.e. your shell script). Print is like a tree. 3465 | 3464 | 2990 | 509 | 1
Hints: • 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()
Hints: 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 currentProcessID ← parentProcessID End If End while
Useful syntax using awk: • awk ‘{if(condition) statement}’ • currentProcessID=$(awk ‘{if(currentID field matches ProcessID)print;}’ file1 | print parentProcessID field)
Part 2 • Figure out which users are online (print only username) • Hints: only one command (who command in conjunction with the cut command) • Need to print each username only once
Part 3 • Figure out what processes any user is running (Print Process details, along with name and ID)
Part 3 • Read and print the entire userlist • Prompt to select one particular user and read the selected user • Usernames must be prepend by number
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 var in 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 who | while read onlineuser do echo $onlineuser | cut –f1 –d‘ ’ >>userlist done
Print numbered list of online users index=1 users = ( $(command that finds out unique users) ) # creates array of all unique logged in users while read onlineuser do print “$index-$onlineuser” Increase index by 1 done
Select to see particular user • Read user number from terminal • Get the process details of desired user • ps –ef | grep $desireduser • To access desireduser from users array: • ${users[$userNo]} • ps -ef | while read process do compare username
Notes: • 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 through csssubmit.
CS284 Program Submission • Follow instructions on the class website: http://web.mst.edu/~ercal/284/CS3800submission.docx
Thanks Any Questions?