shell脚本shell脚本学习笔记

脚本语言基本语法

2018-12-04  本文已影响11人  苏啦啦哇咔咔

本文参考链接:
[1] https://blog.csdn.net/oqqHuTu12345678/article/details/71319006
[2] https://www.cnblogs.com/tongye/p/9707590.html

变量类型

赋值

引用

删除

输出

条件循环

if[表达式]
then  
  xxx
else
  yyy
fi

if[表达式]
then 
  xxx
elif [表达式]
then 
  yyy
else
  zzz
fi
for b变量 in 列表
do
  command1
  command2
  ···
done
while command
do
  Statement(s) to be excuted if command is true
done
#例子
COUNTER=0
while [ $COUNTER -lt 5 ]
do
    COUNTER=’expr $COUNTER+1‘
   echo $COUNTER
done
#读取键盘信息输入信息为FILM,按<ctrl-D>结束循环
echo 'type <Ctrl-D> to terminate'
echo -n 'enter your most liked film:'
while read FILM
do
  echo "Yeah! great film the $FILM"
done 
case $aNum in 
  1) echo '1'
  ;;
  2) echo '2'
  ;;
  *) echo 'you do not select a number between 1 and 2'
  ;;
esac

文件测试

重定向至文件/追加输入文件

运算符

算术运算符

#!/bin/bash

a=2
b=3
c=`expr $a + $b`

echo $c
exit 0

这段代码的输出结果是:5 。注意使用 expr 命令的那一行,使用的是反引号 `` ,反引号中的内容会被优先执行,所以这一行代码的作用是将 expr a +b 这一表达式的执行结果赋给变量 c 。

关系运算符

布尔运算符

字符串运算符

文件测试运算符

文件包含

上一篇 下一篇

猜你喜欢

热点阅读