More Intro. to Shell Scripting
Prof. Brian D. Davison
Computer Science & Engineering, Lehigh University
Announcements
- Today's schedule
- Take short quiz #6
- Return homework 3 -- range 5-10, mean 8.6
- 6 points for code
- 1 point for naming the style used
- 1 point for say why the style was chosen
- 2 points for describing the (major) changes made
- Continue Introduction to Shell Scripting
Types of commands
- A command that you type can be
- An alias
- A function
- A built-in command
- An executable program
- The shell will attempt each of these in turn
Bash shell constructs
- First line contains the path to the shell to execute the script
#!/bin/bash or #!/usr/bin/bash
- Comment lines
# anything following a hash symbol
- Display output
echo "Hello world."
- Local variables (case sensitive)
variable_name=value
declare variable_name=value
name="John Smith"
x=5
- Global variables (environment variables)
export VARIABLE_NAME=value
export PATH=/bin:/usr/bin:.
declare -x VARIABLE_NAME=value
Bash shell constructs
- Use of variables (using $)
echo $variable_name
echo $PATH
- Reading user input (read a line at a time)
echo "Enter your name (first and last):"
read wholename
read firstname lastname
- Working with script arguments (e.g., scriptname arg1 arg2 arg3)
echo $1 $2 $3 # individual arguments
echo $* # all arguments
echo $# # number of arguments
- Arrays in Bash
declare -a array_name=(word1 word2 word3 ...)
declare -a fruit=(apples pears plums)
echo "I like to eat" ${fruit[0]}