5. Creating Standard Output and Quoting
• The echo builtin
• Single versus double quotes
• “${VAR} gets expanded”
• ‘${VAR} not expand’
6. Getting Help for Shell Builtins
• Type: type [-afptP] name [name…]
• For each NAME, indicate how it would be interpreted if used as a command
name.
• Help: help [-s] [pattern …]
• Display helpful information about builtin commands
7. Getting Help for Linux Commands
• Man – format and display the on-line manual pages
8. Shell Variables
• The shell sets several variables automatically
•
HOSTNAME
RANDOM
UID
…
11. Exit Statuses
• 0 = true/ successful
• 1=false/unsuccessful
• Any non-zero exit status represents a failure
• Exit 1
• Echo ${?} # you can also use $?
• $? Will store the last exit status value.
12. Sanity Checking
if [[ “${UID}” –ne 0 ]]
then
echo ‘Please run as root.’ >&2
exit 1
fi
13. Obtaining Standard Input
• The read shell builtin.
• Read –p “A prompt: ” VARIABLE_
readonly VERBOSE=‘true’
Is like const where this variable cannot be reassigned any value.
14. Generating Random Data
• The $RANDOM shell variable
Echo ${RANDOM}
Seemingly random data using checksums.
date +%s%N | sha256sum | head -c8
15. Positional parameters
• Arguments vs Parameters
• $0 = Stores the script name.
• $1 = Stores the first argument
• $2= Stores the second argument
• $* = When used in quotes: “$1 $2 ….”
• $@ = When used in quotes “$1” “$2”…
• $# = total number of positions parameters
• For var in “$@”
• Do
• Echo “$var”
• Done
• _____________
• ./test.sh 1 2 ‘3 4’
• 1
• 2
• 3 4
• ______________
• Now change “$@” to “$*”
• 1
• 2
• 3
• 4
16. The for Loop
for VARIABLE in LIST
do
COMMANDS
done
The while Loop
while [[ COMMANDS ]]
do
COMMANDS
done
17. I/O Redirection - Pipes
• Sending STDOUT as STDIN.
Echo ${PWORD} | passwd –stdin ${USER_NAME}
• String manipulation & data munging with pipes.
echo ‘!@#$%^’ | fold –w1 | shuf | head -c1
19. I/O Redirection
• COMMAND |& COMMAND: to redirect both standard output and standard
error to command (output)
• Head –n3 /etc/passwd /fakefile |& cat -n
• COMMAND &> FILE: to redirect both standard output and standard error to fil
• COMMAND < FILE
• COMMAND >&2
• COMMAND > /dev/null
24. • Bar
• Foo
• Baz
• ______________
• $cat test | sort
• $sort < test
• $sort test
• ___________
• Bar
• Baz
• Foo
Sort and Uniq
To sort numerically use $sort –n test
Uniq
• $cat namelist1.txt
• Jones, Bob
• Smith, Mary
• Babbage, Walter
• $cat namelist2.txt
• Jones, Bob
• Jones, Shawn
• Smith, Cathy
• ___________________
• $sort namelist1.txt namelist2.txt | uniq
• Babbage, Walter
• Jones, Bob
• Jones, Shawn
• Smith, Cathy
• Smith, Mary
25. -d on uniq will output the duplicate line
• $sort namelist1.txt namelist2.txt | uniq –d
• Jones,bob
-c on uniq will provide how many times it has
found each entry
• $sort namelist1.txt namelist2.txt | uniq –c
• 1 Babbage, Walter
• 2 Jones, Bob
• 1 Jones, Shawn
• 1 Smith, Cathy
• 1 Smith, Mary