Bash速成

条件语句(注意:条件里两边的空格,引号,等号)

if [ “$var” = “abc” ]; then

elif [ “$var” = “ac” ]; then

else

fi

for循环

for var in $(ls *.sh); do
echo $var
done

while循环

var=1
while [ “$var” -le 20 ] ; do
var=$(($var+1))
done

until循环(跟while循环相反的)

until condition
do

done

case条件(可用正则,;;相当于break)

case “$var” in
yes | YES | y )
echo “YES”
echo “haha”
;;
[Nn]* ) echo “NO”;;
* ) echo “OTHER”;;
esac

定义/赋值变量

var=xxx  (等号两边不能有空格)

变量读取

echo $var

读取用户输入

read var

不输出换行

echo -n

执行命令并捕获返回值

$(command)

其它

shell里默认类型是字符串型

Leave a Reply

Your email address will not be published. Required fields are marked *