bash shell script 简明教程

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

 

转载于:https://my.oschina.net/mskk/blog/1835057

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值