显示信息:
#!/bin/bash echo "Hello World!" #display one message
获取输入:
#!/bin/bash echo $1 echo $2 echo $3
变量定义:
#!/bin/bash A=b All=all echo "$A"ll echo $All
条件判断:
#!/bin/bash if [ "$1" == "normal" ] then echo "this is normal case" elif [ -z "$1" ] then echo "no input..." fi
while循环:
#!/bin/bash echo "please use add or delete or exit" ACTION="default" while [ -n $ACTION ] do read ACTION case $ACTION in add) echo "add somebody" ;; delete) echo "delete somebody" ;; exit) echo "complete" break ;; *) echo "invalide action, please re-enter" ;; esac done
for循环:
#!/bin/bash for x in 1 2 3 4 5 hello do echo $x done for y in `ls /home/oss` do echo "$y" done
函数:
#!/bin/bash strcat() { OUT="$1"" ""$2" return 0 } strcat2() { echo "$1"" ""$2" return 3 } A="bird" B="mouse" OUT="" strcat $A $B echo $OUT OUT2=`strcat2 $A $B` echo $? echo $OUT2
常用内置符号常量:
符号值 | 意义 |
$@ | 代表全部参数,比如test a b c,此处“a”,“b”,“c”,展开后为三个字符串 |
$* | 全部参数,比如test a b c,此处“a b c”,展开后为一个字符串 |
$# | 参数个数 |
$? | 上一个命令的返回结果 |
$$ | 当前命令所在的进程ID |