zoukankan      html  css  js  c++  java
  • Shell-逻辑控制

    逻辑控制
    条件if
    分支case、select
    循环for、while、until
    break和continue
    有生之年也许你只需要用到if、for、while
    If结构

    if [ condition ];then...;fi
    if [ condition ];then...;else...;fi
    if [ condition ];then...;elif...;fi
    简单的逻辑可以使用&& ||去替代
    [ -f file ] && echo file exist || echo file not exist      条件成立后执行&&后语句,不成立执行||后语句
    条件可以用命令返回值代替

    a=/g/yaya/tmp/1.txt
    if [ -f "$a" ];
    then
    echo file exist;
    else
    echo file not exist;
    fi
    [ -f "$a" ] && echo file exist || echo file not exist

    运行结果如下:

    Case结构
    用于条件太多的情况。每一个条件最后使用两个分号结尾,不可缺少。
    case $var in
    p1)...;;
    p2)...;;
    ...
    pn)...;;
    *)...;;
    esac
    Select
    Select var in var_list;do...;done;
    菜单选择,一般与case结构一起用

    crtl+d退出

    For循环
    for(( c1;c2;c3 ));
    do
    ...;
    done
    for(( i=0;i<10;i++ ));do echo $i;done

    for(( i=0;i<10;i++ ));do echo $i;done

    运行结果如下:

    For 遍历循环
    用于递归数组,还可以递归以空格隔开的字符串序列。或者是某个命令的返回值。
    for f in $array[*];
    do
    ...
    done
    ss="aa bb cc dd";for x in $ss;do echo $x;done      默认空格进行拆解

    for x in 'ls';do echo $x;done

    ss=(aa bb cc"sss dd";for x in ${ss[@]};do echo $x;done

    While循环
    while设置条件
    i=0;
    while ((i<3));do
    echo $i;((i=i+1));
    done

    一行行的读取文件内容
    while read line;do echo $line;done < /tmp/tmp

    until
    i=0;
    until ((i>3));do
    echo $i;
    ((i+=1));
    done
    While可以替代until循环

    退出控制
    return函数返回
    exit脚本进程退出
    break退出当前循环
    continue跳过当前循环,进入下一次循环。



  • 相关阅读:
    如何手动封装 $ on off emit?
    Vue 实例身上的一些方法(二)
    Vue 实例身上的一些方法(一)
    Vue属性过滤
    Vue属性监听
    Vue实现简单的商品增减功能
    Vue 计算属性
    使用Vue实现一个简单地自定义拖拽功能
    数组的深拷贝与浅拷贝
    如何让html引用公共布局(多个html文件公用一个header.html和footer.html)
  • 原文地址:https://www.cnblogs.com/peiya/p/12587196.html
Copyright © 2011-2022 走看看