shell函数示例

来源:互联网 发布:算法初步 试题 编辑:程序博客网 时间:2024/05/01 20:42

一,shell常见语句总结:

a,if ,for,while 等语句和后面的'['要保持一个空格。
b,句子结尾用不用分号都可以,建议使用。
c,while后的'[' 是双个的包含变量的话

二,linux操作实例

#!/bin/bashhello(){if [ -f 'test.sh' ]; thenecho " the file is exist."elseecho "the file is not  exits."fi}app1(){a=1b=2if [ -n $a ];thenecho $a '-n a is true.'elseecho '-n a is false.'   fiif [ -n $c ];thenecho $c  '-n c is true.'elseecho '-n c is false.'   fi } app3(){echo "function name @:"$0echo "all param  @:"$@echo "all params * :"$*echo "total params count:"$#echo "first param" $1echo "second param" $2    return 3 } app4(){ a=5; for i in a b  c d ; do        echo "i="$i; done; for((i=1;i<=5;i++));doecho $i;done    j = 0;while [[ $j -le 10 ]];doecho 'j is :'$j;((j++));done;echo '$1:'$1case $1 ina|b)echo 'input is value is a or b ';;c|d)echo 'input is value is c  or d ';;    *)        echo 'your input is :' $1;;    esac }#hello#app3 a bapp4 aecho 'return value: '$?


0 0
原创粉丝点击