Shell Script Assignment 1.

Slides:



Advertisements
Similar presentations
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Advertisements

Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
Guide To UNIX Using Linux Third Edition
T UTORIAL OF U NIX C OMMAND & SHELL SCRIPT S 5027 Professor: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2015.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.
Introduction to Linux and Shell Scripting Jacob Chan.
Shell Script Examples.
CSCI6303 – Principles of I.T. Fall  Student will become familiar with scripting in shell using Linux/Ubuntu  Student will write a script and execute.
L INUX C OMMAND L INE I NTERFACE G UNAANBAN.G
Introduction to Linux Workshop February Introduction Rob Lane & The HPC Support Team Research Computing Services CUIT.
Help session: Unix basics Keith 9/9/2011. Login in Unix lab  User name: ug0xx Password: ece321 (initial)  The password will not be displayed on the.
Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”
Linux environment ● Graphical interface – X-window + window manager ● Text interface – terminal + shell.
Introduction to Shell Script Programming
Essential Unix at ACEnet Joey Bernard, Computational Research Consultant.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Unix Basics Chapter 4.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
CS 6560 Operating System Design Lecture 3:Tour of GNU/Linux.
System Administration Introduction to Unix Session 2 – Fri 02 Nov 2007 Reference:  chapter 1, The Unix Programming Environment, Kernighan & Pike, ISBN.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Session 2 Wharton Summer Tech Camp Basic Unix. Agenda Cover basic UNIX commands and useful functions.
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
Agenda Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Review next lab assignments Break Out Problems.
Unix/Linux cs3353. The Shell The shell is a program that acts as the interface between the user and the kernel. –The shell is fully programmable and will.
Next Unix Topics Tuesday, 2/11 & 18/2014. Change Password (by 2/14/14) ssh to account on – faclinux.cse.ohio-state.edu – stdlinux.cse.ohio-state.edu passwd.
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
L&T Infotech1 UNIX – Getting Started - Aneesh Ramani.
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming.
Introduction to Programming Using C An Introduction to Operating Systems.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Chapter Six Introduction to Shell Script Programming.
Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?
Agenda Basic Unix Commands (Chapters 2 & 3) Miscellaneous Commands: which, passwd, date, ps / kill Working with Files: file, touch, cat, more, less, grep,
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 2a – A Unix Command Sampler (Courtesy of David Notkin, CSE 303)
Linux Commands C151 Multi-User Operating Systems.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
A Brief Overview of Unix Brandon Bohrer. Topics What is Unix? – Quick introduction Documentation – Where to get it, how to use it Text Editors – Know.
1 CS3695 – Network Vulnerability Assessment & Risk Mitigation – Introduction to Unix & Linux.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
Lab 7 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
UNIX To do work for the class, you will be using the Unix operating system. Once connected to the system, you will be presented with a login screen. Once.
Tutorial of Unix Command & shell scriptS 5027
Quality Thought Technologies
Prepared by: Eng. Maryam Adel Abdel-Hady
Linux Commands Help HANDS ON TRAINING Author: Muhammad Laique
The UNIX Shell Learning Objectives:
System Programming and administration CS 308
Shell Script Assignment 1.
Tutorial of Unix Command & shell scriptS 5027
Basic UNIX OLC Training.
Introduction to UNIX.
Tutorial of Unix Command & shell scriptS 5027
John Carelli, Instructor Kutztown University
Tutorial of Unix Command & shell scriptS 5027
UNIX Reference Sheets CSE 2031 Fall 2010.
Linux Shell Script Programming
Chapter 3 The UNIX Shells
Presentation transcript:

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.

Shell Programming LogIn : Text Editor : ‘VI EDITOR’ 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) 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 done < /directory/filename

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 contain 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}’

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 ;; *) 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)

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

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

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 while read onlineuser do print “$index-$onlineuser” Like before, save $index $onlineuser in a new file Increase index by 1 done < userlist

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

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.

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

Thanks Any Questions?