1 / 25

Shell Script

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.

tristessa
Télécharger la présentation

Shell Script

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shell Script Assignment 1

  2. 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.

  3. 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 • Run a file : • sh filename.sh  (no need for execute permission) • chmodu+x filename (add execute permission) filename

  4. 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

  5. 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

  6. Basic Shell Programming • Reading from a file: • cat filename | while readvariablename do … done • while readvariablename do … done < /directory/filename

  7. 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

  8. 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

  9. 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}’

  10. 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/~cchv5f/assignment1run.txt • Better to use “case” for the menu

  11. Case Statement • Syntax: case $variableName in pattern1) command ;; pattern2) command ;; *) command ;; esac

  12. 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

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

  14. 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

  15. Useful syntax using awk: • awk ‘{if(condition) statement}’ • currentProcessID=$(awk ‘{if(currentID field matches ProcessID)print;}’ file1 | print parentProcessID field)

  16. Part 2 • Figure out which users are online (print only username) • Hints: only one command (who command in conjunction with the cut command)

  17. Part 3 • Figure out what processes any user is running (Print Process details, not only name or ID)

  18. Hints: • 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

  19. 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)

  20. Get the list of currently online user who | while read onlineuser do echo $onlineuser | cut –f1 –d‘ ’ >>userlist done

  21. Print numbered list of online users index=1 while read onlineuser do print “$index-$onlineuser” Like before, save $index $onlineuser in a new file Increase index by 1 done < userlist

  22. 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

  23. 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 in dropbox.

  24. CS284 Program Submission • Follow instructions on the class website: http://web.mst.edu/~ercal/284/CS284submission.doc

  25. Thanks Any Questions?

More Related