----------------------------------------------------- Chapter 10 Reading Guide (second half) ----------------------------------------------------- -IAN! idallen@ncf.ca - Skip over the paragraph about using the "exec" builtin. (p.310) - Rewrite the last sentence on p.310/311 to say: The jobs builtin lists all the background jobs *of the current shell*. (The "jobs" builtin cannot know about the jobs created by other shells.) - True or False: The "jobs" command shows all background jobs, including background jobs running in other shells. (p.311) - True or False: Background jobs can read from your terminal. (p.311/312) (What happens when a Background job tries to read from the terminal?) Remember to skip over the sections in this chapter given earlier. The list of sections to skip over is at the top of this file. - What is a Unix "process"? (p.314) - What is the difference between a shell "job" and a Unix process? (p.104/314) - True or False: Even built-in shell commands have new unique process numbers. (p.315) - Suppose a shell script contains the single command line "date". When you execute the shell script (e.g. "./myscript.sh"), how many processes will have been created after the shell script is done? (p.314/315) - Repeat the above question, replacing "date" by "cd $HOME". (p.315) - True or False: A directory can have more than one parent directory. - True or False: A process can have more than one parent process (PPID). (p.315) - True or False: A directory can have more than one subdirectory. - True or False: A process can have more than one child process. (p.315) - What is the name and process number of the very first, "root" process on Unix? (It has process number one.) (p.315) - The option "-f" gives you the nicest output from the "ps" command, especially on ACADUNIX. Try both "ps -l" and "ps -f". See? - What is your shell doing after it has found a command and started it running for you as a child process (in the foreground)? (p.316) - What is different about your shell's behaviour when it starts a command in the "background", e.g. "sleep 999 &"? (p.316) - Suppse you log in and type this set of four commands to your login shell: sleep 77 sleep 88 sleep 99 echo Here Your login shell is one process. How many other processes (if any) are running by the time the word "Here" prints on your screen? (p.316.) - Suppse you log in and type this set of commands to your login shell: sleep 77 & sleep 88 & sleep 99 & echo Here Your login shell is one process. How many other processes (if any) are running by the time the word "Here" prints on your screen? (p.316.) - The "cd" command is a built-in command to the shell; the shell does not look for it in your PATH, nor does the shell fork and start a separate child process to execute it. Why doesn't the shell start up a separate process to do the "cd" command? (p.316) - Are all the shell variables you set and change in your shell automatically exported to all of your child processes and shells? - True or False: Almost every command you type into a shell causes a new process to be started. (p.316) - What kinds of commands do not cause separate processes to be started? (p.316) - True or False: The Unix shells will read commands from a (single) file given as a command line argument. (p.316) If there is no file argument, the shells read from standard input (as do most Unix commands). When a shell reads from a keyboard, it issues a prompt first. - p.317 - In this course (and in the real world), you *must* put the name of the script interpreter as the first line of your scripts. If you don't do this, the system may choose the wrong shell to run your script. Always put the #! interpreter line first in your scripts. Always start Bourne-type shell scripts with: #!/bin/sh -u (The -u checks for undefined variables. Use it!) The Shell /bin/sh is available on every Unix system on the planet. Only Linux systems are guaranteed to have Shell /bin/bash installed - /bin/bash doesn't exist on many commercial Unix systems. For maximum portability, use "#!/bin/sh -u" to start all your shell scripts. --------------- Note: The book makes two errors thought this chapter: 1) The book fails to use "#!/bin/sh -u" to start scripts 2) The book fails to use "./scriptname" to run scripts. (The book incorrectly assumes that "." is in your search PATH.) In this course (and in the real world), you must not make these two mistakes. Go through the chapter now and fix all the script examples! --------------- Read the Notes file shell_script_execution.txt to understand how a shell script is actually "executed". - True or False: The "#!/bin/sh -u" line at the start of a shell script is treated as a comment when the script is processed by /bin/sh. - Put temporary echo commands into your files named .bashrc and .profile in your HOME directory. Log out and log back in. Which echo commands executed? Now start another bash shell (from the command line). Which echo commands executed? Now, remove the echo commands. - p.318 The last script line at the bottom of the page is missing a blank before the "-f" test. It should read: if [ -f ~/.bashrc ]; then source ~/.bashrc; fi The "if" statement is explained in Chapter 11. - Can the name of a shell variable start with a digit? (p.319) - What conventions do Unix Shell programmers use when using UPPER and lower case letters in variable names? (p.319) - True or False: To assign to a variable, you must preface its name with a dollar sign, e.g. type: $myvar=abc (p.319) - Name some "keyword shell variables" that are inherited by your login shell from its parent environment. (p.319) - What is a "positional parameter"? Is it the same as "a command line argument"? (p.320/333) - Is the variable "$#" a valid name? What about "$$" and "$?"? (p.320/334/339) - True or False: When assigning to a variable, you must both precede and follow the equals sign ("=") with at least one space. (p.320) - What command is most often used to display the expanded values of shell variables on the screen for you? (p.320) - True or False: putting a variable inside double quotes prevents the variable from being expanded by the shell. (p.320) - True or False: putting a variable inside single quotes prevents the variable from being expanded by the shell. (p.320) - Replace this sentence on p.321 "If you do not quote the variable, echo collapses each string of nonblank characters into a single SPACE" by this new sentence: "If you do not quote the variable, the shell splits the expanded variable into separate arguments on whitespace (losing the extra whitespace) and the echo command outputs the multiple arguments separated by just one space" (p.321) - Fix the syntax of the following variable assignment: $ myvar=You owe me $2 * Canadian * $ echo $myvar Fix the above so that the exact text appears on the screen. You will need to use the proper quoting style. (p.320/321/322) - How do you append text to a shell variable? For example, how would you append the string " dog" to the (unknown) contents of an existing variable named "foo"? For example: $ foo=my $ ... what goes here ... ? $ echo "$foo" my dog You don't know what $foo contains before you append to it. You must use a shell variable assignment to append to the existing value of $foo. (The same syntax can be used to add a path to your PATH variable.) - Why is it critical to put double quotes around variables when you use them? (Study the glob expansions on top of p.322 and p.326!) It is essential that you quote all your variables when you use them in shell scripts. Many scripts malfunction because novice shell programmers do not do this. I will always test your scripts with shell glob special characters as input, to see if you have remembered to quote all your variables. - When would you "export" a shell variable? (p.323) Name a variable that is already exported into your login shell when you log in. - Can a child process change the value of a variable in its parent process? Can a child process change the current working directory of its parent process? (same answers!) (p.323) - Remeber to add #! lines to the scripts extest1 and subtest on p.323. Remember to execute scripts in the current directory using "./name" - Note that if you use the "-u" option to the shell to catch undefined variables, you won't be able to run the "subtest" script on p.323 as written (because "subtest" uses the value of an undefined variable, which usually indicates an error in your script). Remove the "-u" option for *the subtest script only* on p.323. - How do you make a shell read one line of input? (p.325) - Is the "read" command run by the shell as a built-in command, or is it run in a separate process? Why? (p.325) - True or False: the "read" command always reads lines from the keyboard, not from standard input. (p.325) - True or False: If reading from the keyboard, the "read" command reads many lines until you type your EOF character (usually ^D - CONTROL-D). - What does "command substitution" do? (p.327) Always try the command substitution by itself, to make sure it is generating the output you want, before you try to capture the output by making it a command substitution. - True or False: The output of all these command substitutions is the same: $ echo "The date is $(date) today." $ echo "The date is $( date ) today." $ echo "The date is $( date ) today." - Experiment with Command Substitution. You can put any Unix shell command line or pipeline into a Command Substitution, and the output will be placed into the current command line by the shell, for use by some other command (as if the output were contained in a shell variable): $ echo "There are $(who | wc -l) users online" $ echo "There are $(ls | wc -l) non-hidden names in this directory" $ echo "The file /etc/motd contains $(wc -l