shell_编程2(语法)

1.判断

#if判断结构     
if expression;then
    command
fi
#if/else判断结构
if expression;then
    command
else
    command
fi
#嵌套if
if expression1;then
    command1
elif expression2;then
    command2
elif expression3;then
    command3    
fi

#case判断
case VAR in
var1) command1;;
var2) command2;;
var3) command3;;
*) command;;
esac

2.循环

#带列表的for循环
for VARIABLE in (list)
do
    commadn
done
#不带列表的for循环
for VARIABLE
doprin
    command
done
#类c的for循环
for ((expression;expression2;expression3))
do
    command
done

#while循环
while expression
do
    command
done

#util循环
until expression
do
    command
donef

#打印乘法口诀表
1 #!/bin/sh
2 #fun print 9*9
3 for ((i=0;i<=9;i++))
4 do
5     for ((j=1;j<=9;j++))
6     do
7         if [[ ${i} -ge ${j} ]]; then
8             let mul=${i}*${j}
9             echo -n "${i}*${j}=${mul} "
10         else
11             break
12         fi
13     done
14     echo
15 done

3.函数

function FUNCTION_NAME(){
    command1
    command2
}

FUNCTION_NAME(){
    command1
    command2
}

  1 #!/bin/sh
  2 #usage : *.sh arg1
  3 #fun   :  print arg1 file colmns
  4 arguJudge(){
  5     echo "Usage: *.sh  fileName "
  6     return 1
  7 }
  8 
  9 if [[ $# -lt 1 ]]; then
 10     arguJudge
 11 fi
 12 
 13 FILE=$1
 14 countLine(){
 15     local i=0
 16     while read line
 17     do
 18         let ++i
 19     done < ${FILE}
#求和
#!/bin/bash
TOTAL=0
until [ $# -eq 0 ]
do
    let "TOTAL=TOTAL+$1"
    shift
done
echo "$TOTAL"

4.加载函数库

函数库11

#函数库 libbase.sh 
#!/bin/bash
_checkFileExit_B_L(){
if [ -f $1 ]; then
    echo "Files: $1 exits"
else
    echo "Files: $1 not exits"
fi
}

#test libbase.sh
#!/bin/bash
source ./Leolib/libbase.sh
_checkFileExit_B_L /etc/passwd
_checkFileExit_B_L /var/log/message

4.IO

  • >    标准输出覆盖重定向
  • >>    标准输出追加重定向
  • >&   表示输出重定向  commadn > stderr.log 2>&1 如果有错误也输出到stderr.log
  • <    标准输入重定向
  • |    管道

5.exec

exec < file 把file文件的内容作为exec的输入
exec > file exec的结果输出到file中
exec num< 指定文件描述符
exec num<&-  关闭文件描述符
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值