----------------------------------------------------- Chapter 10 Reading Guide (third half) (!?) ----------------------------------------------------- -IAN! idallen@ncf.ca - How can you execute a file in the current directory if the current directory is not in your PATH? (p.330) - Does the file named in the MAIL environment variable exist for your account on ACADUNIX? (p.330) - When you log in, the system runs the commands in your .profile file in your home directory. Is this file executed in a separate process, or is it run as part of the current login process? (p.332) - If you set a variable in your .profile and export the variable, is the variable available to your login shell? (p.332) - The "." (dot) or "source" command instructs the current shell to open the given file (you need read permission) and to read commands from that file and execute them directly. No second process is used. This differs from running a script in a subshell or separate process; because, it is the *current* shell that is executing the commands and all variable assignments will change variables in the current shell. (p.332) - True or False: If I put the single command "exit" into an executable shell script named "myexit" and run it ("./myexit"), my current shell will exit (and I may be logged out). (p.332) - True or False: If I put the single command "exit" into a readable file "myexit" and "source" it (". myexit" or "source myexit"), my current shell will exit (and I may be logged out). (p.332) - True or False: The first file name argument to the "." (dot) or "source" command must have execute permissions so that the shell can execute the file and process the commands that it contains. - What is the name of the positional parameter (numeric variable name) that contains the name of the shell script being run, inside the script being run? (p.333) - Study well the short section on "Command-line arguments" (p.333) Note! Fix the sample scripts given in the book to double-quote all uses of shell variables! You *must* always double-quote your variables to prevent Shell wildcard (glob) expansion! - What value is kept in the $# variable? (p.334/338) - Use the argv.sh script (given elsewhere) to learn the difference between the $* and $@ variables (p.337/338). Write and run this small "numargs" script: #!/bin/sh -u # This script shows how $* and $@ are different. echo "First using $*" ./argv.sh "$*" echo "Next using $@" ./argv.sh "$@" Create the above executable numargs script and then study these outputs: $ ./numargs a b c d $ ./numargs a\ b c\ d $ ./numargs "a b" "c d" $ ./numargs 'a b' 'c d' What is the difference between "$*" and "$@" ? (p.338) - True or False: The $* variable expands to be all the files in the current directory. (p.337) - True or False: The $@ variable expands to be your email address. (p.338) - Put the current process id ($$) into your PS1 prompt and export it. Now start up a sub-shell (another copy of bash). The prompt should change to be the new PID of the new shell. Exit that shell. The prompt should indicate the original shell again. (p.338) - Predict the output of the following (p.332/338): $ echo $$ 1234 $ cat myscript #!/bin/sh -u # This script echoes the process ID of the shell running it. echo The PID is $$ $ ./myscript The PID is 2345 $ . myscript The PID is [what is the output] - What is the "Exit Status" of a command? (p.339/965) - What shell variable contains the exit status of the most recently executed command? (p.339) WARNING: The exit status of a command appears backwards for C programmers who think 0 means "bad (FALSE)" and 1 means "good (TRUE)". Unix commands exit with 0 if they succeed. - How can I have a shell script set the exit status of the script to be the number 7? (p.340) - Do you need to quote the variables $$, $#, and $? when using them in shell scripts, to prevent wildcard (glob) expansion? Why or why not? - State the rules that relate single and double quoted strings to (a) shell variable expansion and (b) shell GLOB (wildcard) expansion. - If a shell variable contains a string with GLOB characters in it, under what conditions may the GLOB characters expand after the shell variable expands? What will permit the variable to expand, but not the GLOB characters? What will prevent the variable from expanding? (p.37/320/322/348/358) - Predict the output of these sequences of commands: $ x='$$' ; echo $x ; echo "$x" ; echo '$x' $ x="$$" ; echo $x ; echo "$x" ; echo '$x' If a shell variable contains another dollar-variable, will the shell expand the inner variable after expanding the outer variable? - What is "command-line expansion"? (p.351) - What is one of the first things a shell does to a command line, to generate "tokens" or "words"? (p.351) - What does the word "parse" mean, when applied to a shell command line? (p.351) - Give a frequent example of one type of command-line expansion done by the shell before it executes your command. (p.351) - How does tilde expansion work for ~/foo/bar and ~foo/bar? (p.352) What is the difference between these types of tilde expansion: $ echo ~alleni/bin ~root/bin ~daemon/bin $ echo ~/alleni/bin ~/root/bin ~/daemon/bin - Remember to skip over parts of this chapter. - What is "parameter expansion"? (p.353) - What is "variable expansion"? (p.353) - What is "command substitution"? (p.353) - What is "pathname expansion"? (p.106/357) - How do single and double quotes affect pathname expansion? (p.357) - Replace this confusing first sentence on p.358: "The shell can distinguish the value of a variable from a reference to a variable and does not expand ambiguous file references if they occur in the value of a variable." Use this explanation instead: You do not actually need to double-quote variables that appear on the right-hand side of assignment statements, e.g. x=$1 though it doesn't hurt to do so, e.g. x="$1" The shell does not expand blanks or GLOB characters inside a variable when it expands the variable on the right-hand side of an assignment statement. Most everywhere else, an unquoted variable containing GLOB characters or blanks will have the shell expand both. Double quote all your variables, to be safe! - Study well the use of quoting on p.358. - Note: The text is wrong about the order of expansion on p.358. Items 2 through 6 (tilde, parameter, variable, command, and arithmetic) happen *all at the same time*, not one after the other. You cannot have a command substitution inside a variable or inside a parameter; it doesn't work. - See the file: expansion_order.txt (under Notes) for an explanation and exercises on understanding the revised Order of Expansion (p.358). THIS IS IMPORTANT! See the Summary in the expansion_order.txt file. - What are the meanings and uses of the following shell variables: HOME MAIL PATH PS1 SHELL (this does *not* always contain the name of your *current* shell) $0 $1, $2, $3, etc. $* $@ $# $$ $? - Chapter 10 Chapter Review questions: 1,2,3,4,5,6,10,13,16 Chapter 10 review question 11 is wrong. Parameter expansion happens *at the same time* as variable expansion (but before pathmame expansion). Important additional reading (under Notes): expansion_order.txt