Log - Linux Shell Scripting With Bash (2004) 
                                                                                                       Burtch


Ch2 -  Operating the Shell 

- Variables can be created and assigned text using an equals sign. Ex: $ FILENAME="info.txt" . The value of variables can be printed using the printf command. 
   Ex: printf "%s\n" "$FILENAME". The results of a command can be assigned to a variable using backquotes.
   Ex:  $ DATE=`date`	$ printf "%s\n" "$DATE"
- Multiple commands can be combined on a single line. How they are executed depends on what symbols separate them. 
 	Ex: printf "%s\n" "bau" ; printf "%s\n" "bau1 bau2" 
  If each command is separated by a double ampersand (&&), the commands are executed until one of them fails or until all the commands are executed. If each command is 
  separated by a double vertical bar (||), the commands are executed as long as each one fails until all the commands are executed.
  The history can also be searched with an exclamation mark (!). The history command with no parameters lists the command history the colon command (sometimes called 
  a "no-op" or "null command") and does nothing.
- The built-in dirs command shows the list of saved directories.   The built-in pushd (push directory) command adds (or pushes) directories onto the list and changes the 
  current directory to the new directory.  The built-in popd (pop directory) command is the opposite of the pushd. popd discards the first directory and moves to the next 
  directory in the list.

************************************************************
  Ch3 - Files, Users, and Shell Customization 

- The built-in printf (print formatted) command prints a message to the screen.
  Ex: $ printf "%f\n" 5  -> 5.000000
- Redirection operator <<< redirects a string into a command as if it were piped using printf
 Ex: $ printf "%s\n" "$ERRMSG" | tr [:lower:] [:upper:]
	WARNING: THE FILES FROM THE OHIO OFFICE HAVENT ARRIVED.
	$ tr [:lower:] [:upper:] <<< "$ERRMSG"
	WARNING: THE FILES FROM THE OHIO OFFICE HAVENT ARRIVED. 
- The finger command shows who is on the computer and provides additional information.
- The who command shows who is on the computer, which connection they are using, and when they signed on
- The w command provides even more information, including system statistics and what the users are currently running.
- Typing the alias command by itself, or with the -p switch, lists the current aliases
- The built-in hash command maintains the hash table. Without any switches, hash lists the memorized commands, where they are, and the number of times the command has
  been executed during this session.
- If a variable named PS1 (prompt string 1) is defined, Bash will use the value of this variable for your main prompt. Ex:  declare -x PS1="[\u@\h \W]\$" 
- The bind command enables you to change the editing keys, editing options, as well as create keyboard macros.  The -P switch displays the information in an easy-to-read 
  format.
- The bind command can also define keyboard macros. These are short sequences of keys that are automatically expanded to a longer, common sequence to reduce the amount of
  typing.The -s or -S switches list all currently defined macros. The format for creating a macro is the same as the one used to assign functions, except that \" must appear
  around the expanded text.  Ex: bind "\C-w:\" bau  \""  Control-w will now insert 'bau' into the current line."
  05.03->63-81
- This file is called a profile file because it contains the commands to tailor the session to your particular requirements. The original Bourne shell ran two profile files 
  whenever a user logged on. First, a file called /etc/profile was the general profile file executed for all users. Second, if a file named .profile appeared in the user's
  home directory, this contained additional commands for each user.
- Bash differentiates between a login session and other instances. Bash runs as a login shell when a user or program first logs in or when a login is simulated with Bash's
   --login (or -l) switch. Ex:  bash --login 
- Bash runs as an interactive shell when it is started without a script or when the  -i switch is used. Ex:  bash -i 
  Bash runs a different set of files for interactive sessions that are not also login sessions. Bash looks for a customization script called ~/.bashrc (Bash resources) 
  and executes it instead of the login profile files. 
- The login_shell shell option is turned on when you are in a login shell.This option can be used to verify that you are in a login shell. Ex: shopt login_shell
*************************************************************
 Ch4 - Script Basics 

- By convention, Bash shell scripts have names ending with .sh . You can run your script by starting a new shell:  $ bash hello.sh 
- The exit command, followed by a zero, informs the program running your shell script that the script ran successfully. Ex:  exit 0 
- A well-structured Bash script can be divided into five sections:  The header;  Global declarations;  Sanity checks;   The main script;  Cleanup;
  The header defines what kind of script this is, who wrote it, what version it is, and what assumptions or shell options Bash uses. The very first line of a script is the 
  header line. Ex: #!/bin/bash . Linux uses this information to start the right program to run the script. 
- The suspend command likewise unconditionally stops a script. However, unlike exit, execution is suspended until the script is signaled to wake up and resume the 
  next statement after the suspend command.
- There is also a Linux utility called sleep. Sleep suspends the script for a specific number of seconds after which it wakes up and resumes the next statement after 
  the sleep command. Ex: sleep 5  # wait for 5 seconds
- The built-in read command stops the script and waits for the user to type something from the keyboard. 
   Ex:  printf "Archive files for how many days? " read ARCHIVE_DAYS 
- The > operator redirects the messages of a command to a file. The redirection operator is followed by the name of the file the messages should be written to.
  To add messages to a file without overwriting the earlier ones, Bash has an append operator, >>. This operator redirects messages to the end of a file.
- In the same way, input can be redirected to a command from a file.The input redirection symbol is <.	Ex:  wc --lines < purchase_orders.txt 
- Instead of files, the results of a command can be redirected as input to another command. This process is called piping and uses the vertical bar (or pipe) operator |.
	Ex:  who | wc --lines # count the number of users 
  06.03->81-88
- To Linux, the screen is a file called /dev/tty. Ex: printf "Sales are up" > /dev/tty 
- The output goes through a special file called standard output. By default, standard output represents the screen. Everything sent through standard output is redirected to 
  the screen. Bash uses the symbol &1 to refer to standard output, and you can explicitly redirect messages to it. Ex: printf "sales are up" >&1
- /dev/stdout is another name for the standard output file.
- Linux defines a second file especially for messages intended for the user called standard error. This file represents the destination for all error messages. 
  The symbol for standard error is &2. /dev/stderr can also be used. The default destination, like standard output, is the screen.
	Ex: printf "$SCRIPT:$LINENO: No files available for processing" >&2
- The redirection symbols for standard error are the same as standard output except they begin with the number 2. Ex: bash listorders.sh 2> listorders_errors.txt
- Linux treats all input as if it was being read from a file.This special file is called standard input, and uses the symbol &0. /dev/stdin can also be used 
  for standard input. When commands are joined together with the | symbol, the standard input of the second command becomes the standard output of the first command.
- Redirecting both standard input and standard error is so common that Bash provides a short form &> that redirects both.
   Ex: bash myscript.sh &> list_and_error.txt 
- If you don't know whether a command is built-in, the Bash type  command will tell you. If the command is a Linux command, it shows the path of the command (like 
  the Linux whereis command). Ex:  $ type cd 
- The builtin command explicitly runs a built-in command. The command runs even if there's an alias with the same name. 
  Ex: builtin pwd ; Likewise, command explicitly runs a Linux command
- The built-in enable command temporarily hides the shell built-in commands and reenables them later. The -n switch disables the command. 
   Ex: $ enable test $ type test ; test is a shell builtin $ enable -n test $ type test ; test is /usr/bin/test 
- In a script or at the Bash dollar prompt, you can disallow the use of undefined variables by using this: shopt -s -o nounset 
- Bash provides the shopt (shell option) command to turn options on and off by name instead of a letter. shopt -s (set) turns on a particular shell option. 
  shopt -u (unset) turns off an option.Without an -s or -u, shopt toggles the current setting. shopt by itself or with -p prints a list of options and shows whether they are 
  on, excluding the -o options. To see these options, you need to set -o. A list of the letter codes is stored in the shell variable $-.

******************************************************************************
  CH5 - Variables 

07.03->88-94
- Variables are declared using the Bash declare command. To declare a variable named COST, use this: $ declare COST . Variables are assigned new values with 
  an equals sign (=). To refer to the value of a variable, you must precede the name with a dollar sign ($). Ex: $ printf "%s" $COST . Values can be assigned an 
  initial value when the variable is first declared. Ex: $ declare COST=5 .  The variables remain in existence until the script ends or until the variable is destroyed
   with the built-in unset command.  Ex: $ unset COST  .
- The results of a command can also be assigned to a variable. If a command is contained in backquotes (`), everything written to standard output is stored in the variable
  being assigned instead. Ex: declare Var ; Var=`ls -l | wc -l` ; echo $Var 
- Bash has more than 50 predefined variables. These variables, created when Bash is first started, provide information about the Bash session and can be used to control some
  of the shell's features. The declare command, with no switches, lists all currently defined values.
- It is a safe practice to enclose all assignment values in double quotation marks. Ex: declare Tax=7.45 ; Xvar="taxa este "$Tax"" ; echo $Xvar ; taxa este 7.45 
- To print strings without interpreting the special characters inside, use single quotes. Double quotes do not prevent Bash from interpreting the special
  characters $,`, and \, but single quotes leave all characters unchanged. Ex: $ printf "%s" '$TAX_Message'  . 
- The backslash (\) acts like single quotes for one character, leaving the character unchanged. For example, to print a double quote mark, do this: $ printf "%s" "\""#"
- The printf formatting code %q (quoting) prints backslashes before every character that has a special meaning to the shell. Use this to ensure that spacing is left 
  intact.   Ex: $ printf "%q" "$TAX_MESSAGE" ; The\ tax\ is\ 7.25% 
08.03->94-97
- If a variable is declared with the -i (integer) switch, Bash turns on the integer attribute for that variable. The shell will remember that the string should be treated as 
an integer value. If a non-numeric value is assigned to an integer variable, Bash does not report an error but instead assigns a value of zero.
 Ex: $ declare -i NUMBER_ACCOUNTS=15  The attributes of a variable can be displayed with the -p (print) switch. 
 Ex: $ declare -p NUMBER_ACCOUNTS declare -i NUMBER_ACCOUNTS="12" 
The integer attribute can be turned off with a plus sign. Ex:  $ declare +i NUMBER_ACCOUNTS # turn off integer attribute 
- Constants are unchanging variables that are created with the -r (read-only) attribute. If you attempt to assign a value to a constant, Bash reports an error. 
 Ex: declare -r Yvar="bau and miau" The readonly attribute can be turned off using a plus sign.
- Arrays are lists of values that are created with the -a (array) attribute. A number called an index refers to the position item in the array. Bash arrays differ from 
arrays in other computer languages because they are open-ended. Arrays can be any length and are initially filled with empty strings for items.
  Ex:  $ declare -a PRODUCTS 
 New items are assigned to the array using square brackets to indicate the position in the list. The first position is position zero (not one). 
  Ex:  declare -a myArr[0]="1" myArr[1]="2"  . 
 Because of the square brackets, use curly braces to delineate the variable name and supercede the shell's pathname matching process.
  Ex: echo "${myArr[1]}" 
 The entire array can be accessed using an asterisk (*) or an at sign (@) for the array position.These two symbols differ only when double quotes are used: The asterisk
returns one string, with each item separated with the first character of the IFS variable (usually space), and the at sign returns each item as a separate string 
with no separation character.
  Ex:# printf "%s" "${myArr[@]}" -> 12 #printf "%s" "${myArr[*]}" -> 1 2 . In this example, the at sign version requires two separate %s formatting codes to 
display the array properly, one for each array item. Ex: # printf "%s %s" "${myArr[*]}"
 Multiple values can be assigned with a list in parentheses. Ex: myArr=("1" "2" "3" "4") .
 Combining a list with a declare command, arrays can be assigned values at the time they are created.
 The number of items in the array is returned when # is used in front of the variable name with a position of * or @. 
  Ex: echo "${#myArr[*]}" -> 4 
 Individual array values can be removed with the unset command. Ex: # unset myArr[3] # echo "${myArr[*]}" -> 1 2 3
 The read command can read a list into an array using an -a (array) switch. Ex: read -a myArr
09.03->97-101
- In order to make shell variables available outside of their place of origin, they have to be declared as exportable. Variables are marked as exportable with the export
attribute using the declare -x  (export) switch. The export attribute reminds the shell that you want to "export" or provide the variable, to all programs run by 
the script. Create global constants by using both the export and read-only switches. Ex: $ declare -rx COMPANY_BRANCH="West Coast Branch"  . COMPANY_BRANCH is only 
a read-only variable in the current script.When it's exported to a second script, it's exported as a normal variable-the read-only effect is lost.
- Environment variables are the variables Linux shares between a program and the program that executed it. Like layers of an onion, each program must explicitly export a
variable into the environment for the next program to see it. When Bash variables are given to Linux to share with a new program, the attributes are lost. The variables 
shared with a new program are copies of the original. If a script declares an exported variable and runs a second script, any changes made to the variable by the second 
script are invisible to the first.
- Bash performs variable substitutions as variables are encountered in a command. Bash performs this substitution once. Under rare cases, you might want to force Bash to 
perform an additional round of substitutions. The Bash eval command can do substitutions on demand and run the results.