1 / 18

Operating Systems Lecture 2 UNIX and Shell Scripts

Operating Systems Lecture 2 UNIX and Shell Scripts. 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

nili
Download Presentation

Operating Systems Lecture 2 UNIX and Shell Scripts

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. Operating SystemsLecture 2UNIX 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 <iostream.h> #include <fstream.h> 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 <stdlib.h> ... 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: http://www.ee.surrey.ac.uk/Teaching/Unix/index.html

  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 < and > 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 fac 81 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 • 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: Type the script into a file. Change the file permission to executable. 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!

More Related