User <--> bash <--> kernel
shell is not kernel or part of kernel
various shells: tcsh, csh, bash, ksh
find the using shell: echo $SHELL
find all the shells: cat /etc/shells
what's a shell script?
constants are as in any common programming languages.
variables consist of user defined ones and system defined ones.
system variables are always in CAPITAL letters and using 'set' will list most of system variables
user defined variables: same as ones in the C language
use $ symbol before varible name to access.
output: echo
input: read
basic arithmetic: expr
quotable quotes: " ' `
multiple commands: cmd1;cmd2;...
io redirection: > >> <
pipes: |
process: each process owns a "pid" which ranges form 0~65535
command line arguments symbols: $#, $*, $@, $0, $1,...,$9
comparision symbols:
mathemetical operators: -eq -ne -lt -le -gt -ge
string operators: = != < >
file/directory operators: -s -f -d -r -w -x
logical operators: ! -a -o
condition statement: if <condition> then cmd [elif/else cmd...] fi
loops: while <condition> do cmd done
for x in list do cmd done
- $# :传给脚本的参数个数;
- $0 :脚本名称;
- $n :n为数字,代表传给脚本的第n个参数;
- $@ :参数列表;
- $* :也是显示参数列表,与上一条命令不同的是,当在双引号里面时,”$*”表示一个参数,即”a b c”,而”$@”表示三个参数,即”a” “b” “c”;
- $$ :执行当前脚本的进程ID;
- $? :最后一条命令的退出状态,0表示执行成功,非0表示执行失败.
示例: 建立一个脚本test.sh
echo "number:$#"
echo "scname:$0"
echo "first :$1"
echo "second:$2"
echo "third :$3"
echo "fourth:$4"
echo "argume:$@"
echo "show parm list:$*"
echo "show process id:$$"
echo "show precomm stat: $?"
执行脚本,传入参数a b c:
number:3
scname:bash_brief.sh
first:a
second:b
third:c
fourth:
argume:a b c
show param list:a b c
show process id:30238
show precomm stat: 0