1. Initialize
/etc/profile
.profile
2. Prompt
PS1($): the primary prompt
PS2(>): the secondary prompt
PS1=”uname -n
>”
PS2=”—–>”
3. variable
# local variable
name="Tom"
# global variable
export ORACLE_SID=ora11g
# read a line of input
read name
4. arguments
echo $1 $2 $3
echo $*
echo $# # the number of arguments
5. array
set apples pears plums
echo $1 $2 $3
6. variable expansion modifier
1) if variable is set and non-null, substitute its value. otherwise, substitute word
${variable:-word}
# Assigning temporary default values
fruit=peach
echo ${fruit:-plum} # peach
echo ${newfruit:-plum} # plum
2) if variable is set and non-null, substitute word. otherwise, substitute nothing.
${variable:+word}
# Assigning temporary alternate values
fruit=peach
echo ${fruit:+plum} # plum
echo ${newfruit:+plum} # null
3) if variable is set or non-null, substitute its value. otherwise set it to word. positional parameters may not be assigned in this way.
${variable:=word}
# Assigning permanent default values
echo ${EDITOR} # null
echo ${EDITOR:=/bin/vi} # /bin/vi
echo ${EDITOR} # /bin/vi
DATAFILE=
: ${DATAFILE:=$HOME/db/datafile} # : a "do-nothing" command
echo $DATAFILE # /home4/eli/db/datafile
: ${DATAFILE:=$HOME/junk}
echo $DATAFILE # /home4/eli/db/datafile
4) if variable is set and non-null, substitute its value. otherwise print word and exit.
${variable:?word} # sh: namex: namex is undefined
# Creating error messages based on default values
echo ${namex:?"namex is undefined"} # sh: y: 0403-040 Parameter null or not set.
7. postional parameters
$0 # the name of shell script
$1~$9 # denote positional parameters 1 though 9
$# # the number of parameters
$* # evaluate to all the positional parameters
$@ # same as $*
"$*" # "$1 $2 $3"
"$@" # "$1" "$2" "$3"
# To assign value to the positional parameters
set p1 p2 p3
# To unset all of the positional parameters
set - -
# for instance
set 'apple pie' pears peaches
for i in $*
for i in $@
for i in "$*"
for i in "$@"
apple
pie
pears
peaches
--------------------------------
apple
pie
pears
peaches
--------------------------------
apple pie pears peaches
--------------------------------
apple pie
pears
peaches
# $@ & $*
$@和$*效果一致
"$@" 原样保存参数列表,也就是"$1""$2"...
"$*" 以$IFS指定的分割符保存参数列表, "$1 $2 $3 ..."
pos_para.sh 1 "2 3" 4
#!/bin/bash
ifs=$IFS
IFS="{"
echo '$#: ' $# # 3
echo '$@: ' $@ # 1 2 3 4
echo '$*: ' $* # 1 2 3 4
echo '"$@": ' "$@" # 1 2 3 4
echo '"$*": ' "$*" # 1{2 3{4
IFS=$ifs
8. special variables
$$ # PID of the shell
$- # shell options, set
$? # exit value of the last command
$! # PID of the last running blackground process
$_ # the last postional argument
9. exec
exec date # replace the current program with a new one without starting a new process
exec < filea # open filea for reading stdin
exec > filez # open filez for writing stdout
exec > /dev/tty # recover stdout to the terminal
exec 3< datfile # open datfile as file descriptor 3 for reading input
sort <&3 # sort datfile
exec 3<&- # close fd 3
exec 4> newfile # open newfile as fd 4 for writing output
ls >&4 # ls > newfile
exec 4>&- # close fd 4
exec 5<&4 # make fd 5 as a copy of fd 4
10. test
-z string # Length of string is zero
-n string # Length of string is nonzero
-s filename # File is nonzero size
-g filename # Set-group-ID is set
-k filename # Sticky bit is set
-u filename # Set-user-ID is set
# Checking for null values
[ "$name" = "" ]
[ ! "$name" ]
[ -z "$name" ]
[ "x$name" != "x" ]
11. getopts
while getopts xy options 2>/dev/null
do
case $options in
x) echo "enter x";;
y) echo "enter y";;
z) echo "enter z";;
\?) usage;exit 1;;
ease
done
## with argument
while getopts xy:z: options
do
case $options in
x) echo "enter x";;
y) echo "enter y"; echo $OPTARG;;
z) echo "enter z"; echo $OPTARG;;
\?) usage; exit 1;;
esac
done
12. set
-f Disable globbing
-n Reads commands but not execute them
-x Print commands and their arguments as they are being executed.
echo $- # himBH
set -f
echo * # *
echo ?? # ??
echo $- # fhimBH
13. control statement
if [ expression ]
while [ expression ]
until [ expression ]
for variable in word1 word2
14. hash: a command controls the internal hash table used by the shell to improve efficiency in searching for command
hash # list hash table
hash vi # add vi to hash table
hash -r # remove all from hash table