assignment clarifications
play

Assignment clarifications How many errors to print? at most 1 per - PowerPoint PPT Presentation

Assignment clarifications How many errors to print? at most 1 per token. Interpretation of white space in { } treat as a valid extension, involving white space characters. Assignment FAQs have been updated. Shells and Shell


  1. Assignment clarifications � How many errors to print? – at most 1 per token. � Interpretation of white space in { } – treat as a valid extension, involving white space characters. � Assignment FAQs have been updated.

  2. Shells and Shell Programming

  3. Shells � A shell is a command line interpreter that is the interface between the user and the OS. � A “program launcher” of sorts. � The shell: � analyzes each command � determines what actions are to be performed � performs the actions � Example: wc –l file1 > file2

  4. Which shell? � sh – Bourne shell � Most common, other shells are a superset � Good for programming � csh or tcsh – default for command line on CDF � C-like syntax � Best for interactive use. Not good for programming. � bash – default on Linux (Bourne again shell) � Based on sh, with some csh features. � korn – written by David Korn � Based on sh – Some claim best for programming. � Commercial product.

  5. Shell Diagram

  6. Shell Startup � When a shell is invoked, it does the following: 1. Read a special startup file (usually in home directory) 2. display prompt and wait for command 3. Ctrl-D on its own line terminates shell, otherwise, goto step 2.

  7. Shell startup files � used to set shell options, set up environment variables, alias � sh – executes .profile if it’s there. � ksh – executes .profile if in interactive mode. Executes $ENV (usually $HOME/.kshrc) � csh – executes .cshrc if it exists. If a login shell, executes .login � bash – executes .bashrc, if a login shell, executes .bash_profile instead

  8. Executables vs. Built-in Commands � Most commands you run are other compiled programs. � Found in /bin � Example: ls – shell locates ls binary in /bin directory and launches it � Some are not compiled programs, but built into the shell: � cd, echo

  9. Common shell facilities � Input-output redirection prog < infile > outfile # csh stdout and stderr ls >& outfile # sh stdout and stderr ls > outfile 2>&1 � Pipelining commands � send the output from one command to the input of the next. ls -l | wc ps –aux | grep reid | sort

  10. Job Control � A job is a program whose execution has been initiated by the user. � At any moment, a job can be running or suspended. � Foreground job: � a program which has control of the terminal � Background job: � runs concurrently with the parent shell and does not take control of the keyboard. � Start a job in the background by appending & � Commands: ^Z, jobs, fg, bg, kill

  11. File Name Expansion ls *.c rm file[1-6].? cd ~/bin ls ~reid ls *.[^oa] - ^ in csh, ! in sh � * stands in for 0 or more characters � ? stands in for exactly one character � [1-6] stands in for one of 1, 2, 3, 4, 5, 6 � [^oa] stands in for any char except o or a � ~/ stands in for your home directory � ~reid stands in for reid’s home directory

  12. Sequences � Enter series of simple commands or pipelines separated by semicolons (;) � Commands will be executed in sequence � Each process the shell executes has return value – shell checks for it � conditional execution with &&,|| � && executes next command if return value is 0 (success) � || executes next command if return value is nonzero (failure) example: g++ myprog.cpp || echo compile failed �

  13. Scripts � A bunch commands stored inside a text file � Must add execute permission with chmod � provides all the programming essentials: variables, logic, control flow, loops � powerful way to automate tasks � First line of script should always be #! Pathname � Pathname will be used to interpret script � If #! Not there, sh (Bourne shell) is used � Should provide explicit return value by using exit

  14. Subshells � A copy of the current shell (child) � Created when: � grouped commands with ; are executed. Parent waits until child (subshell) finishes � Script is executed. Parent sleeps until script is done � A background job is executed. Parent runs concurrently with sub-shell � In 1st two cases, parent doesn’t wait if jobs are in background

  15. Subshell Cont’d � Environment space copied from parent � Local variables are clean:

  16. Shell Programming (Bourne shell) � file starts with #!/bin/sh � absolute path to the shell program � not the same on every machine. � can also write programs interactively by starting a new shell at the command line. � Tip: this is a good way to test your shell programs

  17. Example � In a file: #! /bin/sh echo “Hello World!” � At the command line: penguin% sh eddie% sh bash$ echo “Hello World!” $ echo “Hello World!” Hello World! Hello World! bash$ exit $ exit penguin% eddie%

  18. Commands � You can run any program in a shell by calling it as you would on the command line. � When you run a program like grep or ls in a shell program, a new process is created. � There are also some built-in commands where no new process is created. � echo � test � set � shift � read � wait � exit “ man sh” to see all builtins.

  19. Variables Local shell variables vs. environment variables � Environment variables are copied to subshell, local shell variables � are not. Use export to make local shell variables into environment variables: � sh-2.05a$ pet1=fuzzoo sh-2.05a$ pet2=moomoo sh-2.05a$ echo $pet1 $pet2 fuzzoo moomoo sh-2.05a$ export pet1 sh-2.05a$ sh sh-2.05a$ echo $pet1 $pet2 fuzzoo sh-2.05a$ exit sh-2.05a$ echo $pet1 $pet2 fuzzoo moomoo

  20. Predefined Environment Variables � $HOME – full path of your home directory � $PATH – list of directories to search for files � $MAIL – full path of mailbox � $USER – your user name � $SHELL – full path of your login shell � The initial values of the environment variables are set by looking at the .profile file (Bourne shell) in your home directory.

  21. Other Built-in Variables � $$ – process id of the current shell � $! – process id of last background command � $0 – name of shell script � $1...$9 – commandline arguments � $* – list of all commandline args � $? – return value of the previously executed command � $# – number of commandline params.

  22. Variable Access � $name – replaced by value of name � ${name} – replaced by value of name. Useful if ${name} is followed by alphanumerical characters: verb=play Doesn’t work: echo I like $verbing Works: echo I like ${verb}ing Output: I like playing

  23. Command line arguments � positional parameters: variables that are assigned according to position in a string � Command line arguments are placed in positional parameters: giant $ giant fee fie fo fum #!/bin/sh arg1: fee echo arg1: $1 arg2: fie echo arg2: $2 echo name: $0 name: giant echo all: $* all: fee fie fo fum

  24. Another Example sh-2.05a$ cat > shelldemo.sh #! /bin/sh echo $HOME echo $PATH echo commandline=$* echo 1st arg=$1 echo $? gcc hello.cpp echo $? gcc noexist.cpp echo $? sh-2.05a$ chmod ugo+x shelldemo.sh sh-2.05a$ shelldemo.sh arg1 arg2 arg3 /u/kenxu /u/kenxu/bin/Linux:/u/kenxu/bin:/local/bin/X11:/usr/X11R6/bin:/local/bin:/usr/ucb:/bin:/usr/bin:.:/u/kenxu commandline=arg1 arg2 arg3 1st arg=arg1 0 0 gcc: noexist.cpp: No such file or directory gcc: no input files 1

  25. Local Variables � local variables – spaces matter � name=value – assignment � variables can have a single value or list of values. � Single value: bindir=“/usr/bin” � List of values (separated by spaces): searchdirs=“~/tests $HOME/test2 .”

  26. Example: ($ is the default sh prompt) $ bindir=“/usr/bin” $ searchdirs=“~/tests $HOME/test2 .” $ echo $searchdirs ~/tests /u/reid/test2 . $ echo $bindir /usr/bin

  27. Quoting Single and double quotes are on the same key. Back quote is often on the same key as ~. � Double quotes inhibit wildcard replacement only. � Single quotes inhibit wildcard replacement, variable substitution and command substitution. � Back quotes cause command substitution. � Practice and pay attention.

  28. Quoting example ” – double quotes ’ – single quote $ echo Today is date ` - back quote Today is date $ echo Today is `date` Today is Thu Sep 19 12:28:55 EST 2002 $ echo ”Today is `date`” Today is Thu Sep 19 12:28:55 EST 2002 $ echo ’Today is `date`’ Today is `date`

  29. Another Quoting Example � What do the following statements produce if the current directory contains the following non-executable files? a b c $ echo * ” – double quotes $ echo ls * ’ – single quote $ echo `ls *` ` - back quote $ echo ”ls *” $ echo ’ls *’ $ echo `*`

  30. More on Quoting � Command substitution causes another process to be created. � Which is better? What is the difference? src=`ls *.c` or src=*.c

  31. Here Documents � Easy way to provide input to commands without using auxiliary files: sh-2.05a$ cat << ENDOFTEXT > type some text and > have cat repleat > it! > ENDOFTEXT type some text and have cat repleat it! sh-2.05a$

Recommend


More recommend