1、顺序结构体

命令从上往下顺序执行

2、分支结构体

1)判断真假

test 表达式  或者  [ 表达式 ](必须有空格)    真返回0,假返回1  test的别名是[, 参数是]

判断表达式 记忆 解释
!表达式    
表达式1 -a 表达式2 and 相与
表达式2 -o 表达式2 or  
-z string zero string为空字符串,则返回真0
-n string not zero  
string1 = string2   两个字符窗相等,返回0
string1 != string2    
INTEGER1 -eq INTEGER2 equal  
INTEGER1 -ge INTEGER2 greater equal 大于等于
INTEGER1 -gt INTEGER2 greater 大于
INTEGER1 -le INTEGER2 little  equal   小于等于
INTEGER1 -lt INTEGER2  little 小于
INTEGER1 -ne INTEGER2   not equal 不等

文件存在?文件类型?

-d FILE dir 如果FILE是目录则为真
-e FILE  同-a  exists 是否存在如果是文件则为真
-N     检测文件自从上次读取之后是否被修改
-f   是否是文件,是否是常规文件
-s     文件存在且不为空
-b、-c、-L、S、p  

块文件、字符文件、符号连接

套接字、命名(FIFO)管道

硬连接实际是文件本身    

rwx

-r、-w、-x read 存在且可读、、
-g、-u sgid、suid 以文件所有用户/组的身份运行

属主、属组

-O、-G   是否属于当前用户或组

-ef 比较两个文件是否为同一文件系统中相同inode节点的硬连接,

比较文件是否相同用diff -q $file1 $file2,相同返回码0

if [ new -nt old ]; then比较修改时间mtime  n——new    t——time     o——old  -ot/-nt  不能对访问时间atime  inode修改时间ctime进行测试

字符串比较测试

[ = ]   [ != ]  [[ > ]]   [[ < ]]

-z  空字符串;最好用“”把变量引起来,因为当为空时没引号会是这样 [ -z  ]不合法

-n  非空字符串;

正则表达式测试

#!/bin/bash
for rpms in /home/lixn/Downloads/*
do
rpmname=`basename $rpms`
if [[ $rpmname =~ .*\.rpm ]];then
echo "$rpmname is a .rpm package"
else
echo "File \"$rpmname\" is not a .rpm name"
fi
done

也有类似python正则的(),

if [[ $rpmname =~ (.+)_(.*)_(.*)\.rpm ]]; then
echo “package ${BASH_REMATCH[]}第一个括弧

bash正则表达式

 数值测试 -eq  -ne -lt -gt  -le  -ge  
组合测试 &&——逻辑与      if [ -r "$filename" ] && [ -s "$filename" ]; then md5sum $filename   前边不成立后边就不进行了(短路)
       ||——逻辑或

例如:

[lixn@localhost ~]$ [-d learn] && cd learn
bash: [-d: command not found...
[lixn@localhost ~]$ [ -d learn ] && cd learn
[lixn@localhost learn]$

2)if分支语句

if list; then list; [elif list; then list]...[else list] fi;then后可以没有换行符,换行可以用;代替

#!/bin/bash
if [ `id -u` -eq 0 ]; then
PS1='##'
else
PS1='$$'
fi

3) case分支语句,可以使用模式匹配

#!/bin/bash
case $ in
[-]) echo 'digital';;
[a-z]) echo 'lower char';;
[A-Z]) echo 'upper char';;
"Good") echo 'OK';;
*) cd /tmp  
touch a
echo "Other";;
esac ;;——表示不再执行其他语句
;;&——表示还要匹配接下来的所有模式
;&——表示接下来的模式已经匹配

3、循环语句

1)for循环语句

for name [ [ in [ word ... ] ] ; ] do list; done

for (( expr1; expr2; expr3 )) ; do list; done

#!/bin/bash
for i in
do
echo -n "$i次|"
done
cd /tmp
for fil in * ; do
[ -f ${fil} ] && mv ${fil} ${fil}.old
[ -d ${fil} ] && break
done total=
for ((i=; i<; i=i+))
do
[ `expr $i % ` -eq ] && continue
total=`expr ${total} \* $i`
done
echo ${total}

2)while循环

while list; do list; done

#!/bin/bash
#通过管道循环读取/etc/passwd内容
cat /etc/passwd | while read line
do
#以:为分割符,只读取第一个字段
user=`awk -F : '{print $1}' <<< ${line}`
echo "Account: ${user}" done

3) select选择语句

   PS3="Please Select:"
menus="com|net|org|edu|quit"
IFS="|"
a='Apply '
b=' domain'
select item in $menus
do
case $item in
com) echo "$a$item$b";;
net) echo "$a$item$b";;
org) echo "$a$item$b";;
edm) echo "$a$item$b";;
quit) break;;
esac
done

4)break  continue 循环控制

         for fil in *
do
if [ $fil = "." -o $fil = ".." ]
then
echo 'x'
continue
fi
echo $fil
[ $fil = '' ] && break
cp -r $fil $fil.old
done

最新文章

  1. iOS--(UITableViewCell)、(UITableViewController)微信个人主页
  2. 6个原因说服你选择PostgreSQL9.6
  3. vundle按照YouComplete
  4. Vim识别编码
  5. Windows实用命令
  6. obj-c编程10:Foundation库中类的使用(6)[线程和操作队列]
  7. SKU : Stock Keeping Unit
  8. Linux命令行下快捷键
  9. Python集合(set)类型的操作 (转)
  10. 10.15仿admin开发stark组件(一)
  11. 使用代码段遍历,枚举类型Enum
  12. easyui treegrid idField 所在属性中值有花括号(如Guid)当有鼠标事件时会报错,行记录一下
  13. 【Java】多线程
  14. Oracle EBS 报错 您不具有分配给您的清除MDS的权限
  15. OcLint的使用
  16. 20145204《Java程序设计》第8周学习总结
  17. Uncaught TypeError: _react2.default.createContext is not a function
  18. Linux基础之-网络配置,主机名设置,ssh登陆,scp传输
  19. (八)for语句
  20. django的ORM中的2个易混点

热门文章

  1. 实用---生命游戏 Java
  2. 【教程】基于Ubuntu系统的PyTorch虚拟环境配置
  3. Vue学习系列(三)——基本指令
  4. ArcGIS Engine专题地图渲染器的实现(入门版)
  5. Python之路(第四十六篇)多种方法实现python线程池(threadpool模块\multiprocessing.dummy模块\concurrent.futures模块)
  6. 面试题-javascript-面向对象编程
  7. 怎么在.NetCore3.0 中使用Log4net 写日志 及读取配置文件的信息
  8. day3-02 python入门之基本的数据类型
  9. day2------运算符和编码
  10. django-URL反向解析Reverse(九)