Shell Script Assignment 1.

Slides:



Advertisements
Similar presentations
Shell Script Assignment 1.
Advertisements

CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
More Shell Basics CS465 - Unix. Unix shells User’s default shell - specified in /etc/passwd file To show which shell you are currently using: $ echo $SHELL.
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
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.
Linux environment ● Graphical interface – X-window + window manager ● Text interface – terminal + shell.
Introduction to Shell Script Programming
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Unix Basics Chapter 4.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
CS 6560 Operating System Design Lecture 3:Tour of GNU/Linux.
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
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
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.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
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.
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.
Introduction to Programming Using C An Introduction to Operating Systems.
Chapter Six Introduction to Shell Script Programming.
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.
Shell Control Statements and More
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
Linux+ Guide to Linux Certification, Second Edition
1 CS3695 – Network Vulnerability Assessment & Risk Mitigation – Introduction to Unix & Linux.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Linux Administration Working with the BASH Shell.
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
Some Linux Commands.
Agenda Basic Unix Commands (Chapters 2 & 3) Miscellaneous Commands:
Introduction to UNIX Directory structure and navigation
CSE 303 Concepts and Tools for Software Development
Tutorial of Unix Command & shell scriptS 5027
Basic UNIX OLC Training.
Introduction to UNIX.
Tutorial of Unix Command & shell scriptS 5027
Using the Terminal Lab 3+.
Pepper (Help from Dr. Robert Siegfried)
Tutorial of Unix Command & shell scriptS 5027
Introduction Paul Flynn
UNIX Reference Sheets CSE 2031 Fall 2010.
Getting started with CentOS Linux
Linux Shell Script Programming
Chapter 5 Bourne Shells Scripts
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.

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