1 Operating Systems Lecture 2 UNIX and Shell Scripts.

Slides:



Advertisements
Similar presentations
Learning Unix/Linux Bioinformatics Orientation 2008 Eric Bishop.
Advertisements

Introduction to UNIX CSE 2031 Fall May 2015.
1 Introduction to UNIX Ke Liu
©Colin Jamison 2004 Introduction to Linux Colin Jamison.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
The UNIX Shells 1. What is a Unix shell? 2. A few common shells in the Unix & Linux. A. Bourne shell B. Korn shell C. C shell D. Bash-the default shell.
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.
Scripting Languages and C-Shell. What is a scripting language ? Script is a sequence of commands written as plain text and run by an interpreter (shell).
The Unix Shell. Operating System shell The shell is a command interpreter It forms the interface between a user and the operating system When you log.
Introduction to Linux/UNIX. History UNIX beginnings in 1969 (Linus Torvalds is born!) AT & T Bell Laboratories (Ken Thompson & Dennis Richie) Working.
Introduction to Linux and Shell Scripting Jacob Chan.
Shell Script Examples.
Linux Commands LINUX COMMANDS.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
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”
1 Lecture 2 Working with Files and Directories COP 3344 Introduction to UNIX.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
Writing Shell Scripts ─ part 1 CSE 2031 Fall September 2015.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Shell The Shell The agency that.
1 Shell Programming – Extra Slides. 2 Counting the number of lines in a file #!/bin/sh #countLines1 filename=$1#Should check if arguments are given count=0.
Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
An Introduction to Unix Shell Scripting
Introduction to Computer Organization & Systems Topics: Intro to UNIX COMP John Barr.
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
Unix Tutorial for FreeSurfer Users. Helpful To Know FreeSurfer Tutorial Wiki:
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.
Unix Tutorial for FreeSurfer Users. Helpful To Know FreeSurfer Tutorial Wiki:
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
LINUX System : Lecture 6 Shell Programming
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
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.
Using UNIX Shell Scripts Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Introduction to Programming Using C An Introduction to Operating Systems.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Linux Commands C151 Multi-User Operating Systems.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
Lecture 1: Introduction, Basic UNIX Advanced Programming Techniques.
Various 2. readonly readonly x=4 x=44 #this will give an error (like what in java?)
Introduction to UNIX (part 2) CSE 2031 Fall March 2016.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Learning Unix/Linux Based on slides from: Eric Bishop.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Prepared by: Eng. Maryam Adel Abdel-Hady
System Programming and administration CS 308
Part 1: Basic Commands/Utilities
Some Linux Commands.
The Linux Operating System
Shell Script Assignment 1.
CSE 303 Concepts and Tools for Software Development
Basic UNIX OLC Training.
Introduction to UNIX.
Sarah Diesburg Operating Systems CS 3430
Introduction to Linux/UNIX
UNIX Reference Sheets CSE 2031 Fall 2010.
Linux Shell Script Programming
Chapter 3 The UNIX Shells
Linux Commands LINUX COMMANDS.
Introduction to UNIX (part 2)
Introduction to UNIX (part 2)
Presentation transcript:

1 Operating Systems Lecture 2 UNIX and Shell Scripts

2 C++ Programs with Arguments A C++ program may be called with arguments (like parameters for the whole program. Arguments may be used for: 1. Options used for decisions in main( ) 2. Strings that change in output statements 3. Filenames for input and output files 4. Strings can be changed to values with atoi( ) Arguments have built-in names: argc: Argument count (the number of arguments given) argv: List of arguments (array of strings)

3 Calling a program with Arguments Example: > myProgram inputFile outputFile argc = 3 (myProgram counts as one of the arguments) argv holds: argv[0] = "myProgram" argv[1] = "inputFile" argv[2] = "outputFile"

4 C++ Code using argc and argv #include int main(int argc, char * argv[ ] ) { ifstream inFile; ofstream outFile; if (argc != 3)//Error checking cout << "Usage: myProgram inFile outFile " << endl; else { inFile.open(argv[1]) if (!inFile)//Error checking cout << argv[1] << " not opened." << endl; outFile.open(argv[2]) if (!outFile) //Error checking cout << argv[2] << " not opened." << endl; //Rest of code for input and output from files... } return 0; }

5 Using atoi All arguments are input as strings. If you want to use numeric values, you must convert from the string to a number: #include... int number, square;... number = atoi(argv[1]); square = number * number;...

6 UNIX Commands Commands you should already know: ls, pwd, cd, mkdir, rmdir cp, mv, rm * = substitution character Commands you should learn: echo, cat, more, grep | = pipe that connects stdout with stdin e.g. ls -l | grep Aug Use man or apropos to find out about commands. A good UNIX tutorial can be found here:

7 Kernel vs. Utilities The kernel is a process that is always executing. Utilitites reside on the disk (e.g. grep, lpr, etc.) The shell program is a utility. The shell knows some built-in commands that don't have to be read off the disk. (cd, pwd, etc)

8 Shells The shell is a command line interpreter. Its function is to get and execute the next statement. Common shells: Bourne shell (/bin/sh) Korn shell (/bin/ksh) C shell(/bin/csh) T shell (/bin/tcsh) GNU Bourne-Again Shell (/bin/bash) Z shell (/bin/zsh) The Bourne shell is standard. All information here is for the Bourne shell. The shell is just a program. Anyone can write their own custom taylored shell.

9 The Shell Environment The shell environment is a list of associated strings in the shell: PATH: path or paths used to look for programs or utilities. e.g. /opt/local/bin HOME: Location of home directory e.g. /home/fac/croyden SHELL: Current shell e.g. /bin/csh Type setenv to see a list of current environment variables. Use setenv to change environment: setenv TERM xterm Change terminal type to xterm

10 Redirecting I/O Normally, the shell will use standard input and standard output for executing commands. You can redirect the standard input and output using ls -l > filex Redirect the output of ls -l into the file named filex. Using >> allows you to append output to a file: ls -l >> filex Append the output of ls -l to the end of file named filex.

11 File Permissions Files have permissions for the user/owner, group and others. You can view the permissions by typing: ls -l -rwxr--r-- 1 croyden fac81 Jan 3 21:53 myFile The information given is: file type (- = plain file) permissions for user (rwx), group (r--) and other (r--) number of hard links (1) user/owner name (croyden) group name (fac) size of file (81) date and time last modified (Jan 3 21:53) filename (myFile)

12 Changing Permissions File types: - = plain file, d = directory Permissions: r = read permission, w = write permission, x = execute permission. Use chmod to change permission for user, group, other or all: chmod a+r filename everyone gets read permission chmod g+x filename group gets execute permission chmod u+w filename user gets write permission chmod o-w filename others lose write permission chmod og-rw filename group and others lose read and write permission

13 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a comment. Comments run from # to the end of the line. 3. All shell scripts begin with the interpreter you want to use: #!/bin/sh Example: #!/bin/sh who | grep croyden exit 0

14 Running a shell script To run a UNIX script: 1)Type the script into a file. 2)Change the file permission to executable. 3)Execute it (by typing the filename at the prompt).

15 Shell Variables Shell variables are stored as strings: Example: #!/bin/sh x=1# Note: No spaces in assignment. # If space after x, thinks x is a command echo The value of x is $x # $x prints the value of variable x echo The home directory is $HOME echo The current shell is $SHELL (Note: to debug, use -x: sh -x scriptFileName This will list the commands as they are executed.)

16 Using Quotes Single quote: Groups together characters until end quote. $ is not processed. Example: #!/bin/sh grep Constance Royden /etc/passwd #Tries to open Royden as a file grep 'Constance Royden' /etc/passwd #Searches for Constance Royden #in passwd file x=1 echo $x#echos 1 echo '$x'#echos $x exit 0

17 Double Quotes Double quotes act like single quotes, except the $ is processed: #!/bin/sh x=1 echo $x#echos the value of x echo "$x"#echos the value of x address="College of the Holy Cross" echo $address#echos College of the Holy Cross echo "$address"#ditto exit 0

18 More Quotes Backslash (\): Places a single quote around a character: \> is the same as '>' Back quote (`): Tells shell to execute the enclosed command and insert the output here: #!/bin/sh echo There are `who | wc -l` users logged on exit 0 Try these examples out for yourself!