转载自: http://www.cnblogs.com/stephen-liu74/archive/2011/11/04/2228133.html

五、BASH SHELL编程:

1.    初始化顺序: /etc/profile    ( ~/.bash_profile | ~/.bash_login | ~/.profile )    ~/.bashrc

2.    set -o allexport 当前shell变量对其所有子shell都有效.
       set +o allexport 当前shell变量对其所有子shell都无效.
       set -o noclobber 重定向输出时,如果输出文件已经存在则提示输出失败, date > out; date > out, 第二次操作失败
       set +o noclobber 缺省shell行为. date > out; date > out, 第二次操作成功
       shopt -s extglob 使用扩展通配符,如 abc?(2|9)K, abc*([0-9]), abc+([0-9]), no@(thing|body), no!(thing|body)
       其中?,*,+,@和!都是用于修饰后面的()的.
   
3.    变量声明: declare, 在赋值的时候等号的两边不需要空格. variable=value.   
       declare -r variable=value 声明只读变量.
       declare -x variable=value 相当于export variable=value.
       数组声明:
       declare -a variable=(1 2 3 4)
       or
       name=(tom tim helen)
       or
       x[0]=5
       x[4]=10
       数组的声明可以不是连续的, 这一点和awk中的数组比较类似.
       e.g.
       /> declare -a friends
       /> friends=(sheryl peter louise)
       /> echo ${friends[0]}
      

       #在%前面, #代表从前面删除, %代表从后面删除。 ##最大化删除, %%最大化删除
7.    引用:
       \: 可以使shell中的元字符无效, 如?, < >, \, $, *, [ ], |, ( ), ;, &, { }
       ': 单引号可以史其内的所有元字符无效, 也包括\.
       ": 双引号也可以史其内的所有元字符无效, 变量和命令替换除外. 如 echo "What's time? $(date)", 这里date命令将被执行.
   
8.    命令替换:
       variable=$(date) or variable=`date`
       variable=`basename \`pwd\`` or variable=$(basename $(pwd))    命令替换可以嵌套, 第一种方法中,嵌套的命令必须使用\进行转义.
       eval: 可以进行命令行求值操作.
       e.g.
       /> set a b c d
       /> echo The last argument is \$$#
       /> The last argument is $4
       /> eval echo The last argument is \$$#
       /> The last argument is d
   
9.    数学计算:
       /> echo $[5+4-2]
      
       /> echo $[5+2*3]
      
       /> echo $((5+4-2))
      
       /> num=4*6
       /> echo $num
      
   
       使用不同进制(2~36)表示数字.
       /> declare -i x=017
       /> echo $x
      
       /> x=2#101
       /> echo $x
      
       /> x=8#17
       /> echo $x
      
       /> let "i = i + 2"
       /> echo $i
      
       /> let "i+=1"
       /> echo $i
      
   
10.    读取用户输入(read)命令:
       /> read answer
      
       
        /> [ $name = [Tt]?? ]  //[]和test不允许使用通配符.
        /> echo $?
       
       
        /> x=5
        /> y=20
        /> [ $x -gt $y ]
        /> echo $?
       
       
        /> [ $x -le $y ]
        /> echo $?
       
       
        /> name=Tom
        /> friend=Joseph
        /> [[ $name == [Tt]om ]]
        /> echo $?
       
       
        /> [[ $name == [Tt]om && $friend == "Jose" ]]
        /> echo $?
       
       
        /> x=2
        /> y=3
        /> (( x > 2))
        /> echo $?
      
       
       2) test命令操作符:
       字符串判断:
       [ string1 = string2 ]    or    两个字符串相等时返回true.
       [ string1 == string2 ]    
               
       [ string1 != string2 ]         两个字符串不等时返回true.
       [ string ]                          string非空时返回true.
       [ -z string ]                      为空时返回true.    // z表示非空
       [ -n string ]                      为非空时返回true.  // n表示null
       
       逻辑判断:(cond1可以包含元字符)
       [ string1 -a string2 ] or     string1和string2都非空时
       [[ cond1 && cond2 ]]        cond1和cond2都为true
           
       [ string1 -o string2 ] or     string1或string2为非空时
       [[ cond1 || cond2 ]]          cond1或cond2为true.
           
       [ ! string ] or                    string为空
       [[ !cond ]]                        cond为false.

整数判断:
       [ int1 -eq int2 ]                int1等于int2
       [ int1 -ne int2 ]                int1不等于int2
       [ int1 -gt int2 ]                 int1大于int2
       [ int1 -ge int2 ]                int1大于等于int2
       [ int1 -lt int2 ]                  int1小于int2
       [ int1 -le int2 ]                 int1小于等于int2
       
       文件判断
       [ file1 -nt file2 ]               file1比file2新
       [ file1 -ot file2 ]               file1比file2旧
   
       文件检验:
       [ -d $file ] or                   表示判断目录是否存在
       [[ -d $file ]]
           
       [ -e $file ] or                   表示判断文件是否存在
       [[ -e $file ]]
           
       [ -f $file ] or                   表示判断非目录普通文件是否存在
       [[ -f $file ]]
           
       [ -s $file ] or                  表示判断文件存在, 并且非0.
       [[ -s $file ]]

[ -L $file ] or                  表示判断为链接符号存在
       [[ -L $file ]]
           
       [ -r $file ] or                  表示判断文件存在, 并且可读.
       [[ -r $file ]]
           
       [ -w $file ] or                 表示判断文件存在, 并且可写.
       [[ -w $file ]]

[ -x $file ] or                 表示判断文件存在, 并且可执行.
       [[ -x $file ]]

3) if语句:
       if command     //当command的返回状态为0时执行then后面的语句.
       then
            command
       fi
       
       if test expr
       then
           command
       fi
       
       if [ string/numeric expr ]
       then
           command
       fi
       
       一下两种为new format, 建议使用.
       if [[ string expr ]]     //支持通配符和扩展通配符(shopt -s extglob)
       then
           command
       fi
       
       if (( numeric expr ))
       then
           command
       fi
      
       if command
       then
           command1
       else
           command2
       fi
       
       if command
       then
           command1
       elif command
       then
           command2
       elif command
       then
           command3
       else3
           command4
       fi
   
       4) 空语句:
       用冒号表示.
       if expr
       then
           :
       fi
       
       5) case语句:    //;;相当于c语言中break.
       case variable in
       value1 | value2)        // value1支持通配符和|作为or的关系
           command1
           ;;
       value3)
           command2
           ;;
       [vV]alue4)
           command3
           ;;
       *)
           command4
           ;;
       esac
      
       6) 循环语句:
       IFS 变量表示缺省的分隔符whitespace, tab, newline等. 可以自行修改该值, 但是建议再改之前赋值给其他变量作为备份, 有利于再恢复到缺省值.
       for variable in word_list
       do
           commands
       done
      
       e.g.
       for pal in Tom Dick Harry Joe
       do
           echo "Hi $pal"
       done
       echo "Out of loop"
       
       for file in testfile[1-5]
       do
           if [[ -f $file ]]
           then
               echo "$file exist"
           fi
       done
      
       for name in $*    // 等同于 for name
       do
           echo "Hi $name"
       done
       
       while command    //这里当条件为true的时候, 或者command的退出为0时执行循环体的命令.
       do
           commands
       done
       
       while (( $num < 10 ))
       do
           echo -n "$num"
           let num+=1
       done
       
       until !command    //这里当条件为false的时候, 或者command的退出为非0时执行循环体的命令.
       do
           commands
       done       
   
       break/continue:    功能等同于c语言中的break和continue.
       
       重定向循环和后台执行的循环:
       while command
       do
           command
       done > file    //重定向到文件
       
       while command
       do
           command
       done | command    //重定向到其他程序的管道
       
       while command
       do
           command
       done &        //后台执行.
       
       shift [n]命令: 用于把脚本的参数列表移出指定的位数, 不指定为左移一位, 一旦发生位移, 被移出的参数将永远被删除.
       e.g.
       
       [scriptname: doit]
       while (( $# > 0 ))
       do
           echo $*
           shift
       done
       
       /> doit a b c d e
       a b c d e
       b c d e
       c d e
       d e
       e

速查笔记(Linux Shell编程<下>)的更多相关文章

  1. 速查笔记(Linux Shell编程<上>)

    转载自: http://www.cnblogs.com/stephen-liu74/archive/2011/11/01/2202027.html 零.shell中的内部变量: 1.    $?:   ...

  2. 写shell脚本速查笔记

    linux shell脚本的语法蛋疼,而且对于java开发人员来说又不常用,常常是学了一次等到下次用的时候又忘记了.因此制作这个速查笔记,用于要写shell脚本时快速回忆&速查. 获取当前脚本 ...

  3. linux —— shell 编程(文本处理)

    导读 本文为博文linux —— shell 编程(整体框架与基础笔记)的第4小点的拓展.(本文所有语句的测试均在 Ubuntu 16.04 LTS 上进行) 目录 基本文本处理 流编辑器sed aw ...

  4. linux —— shell 编程(编程语法)

    导读 本文为博文linux —— shell 编程(整体框架与基础笔记)的第4小点的拓展.(本文所有语句的测试均在 Ubuntu 16.04 LTS 上进行) 目录 再识变量 函数 条件语句 循环语句 ...

  5. Makefile速查笔记

    Makefile速查笔记 Makefile中的几个调试方法 一. 使用 info/warning/error 增加调试信息 a. $(info "some text")打印 &qu ...

  6. linux shell编程总结

    linux shell编程总结 本周学习了unix/linux shell编程,参考的是<LINUX与UNIX Shell 编程指南>,David Tansley著:徐焱,张春萌等译,由机 ...

  7. Linux Shell编程参考大全

    本文记录Linux Shell编程中常用基本知识,方便快速入门以及查询使用. 本文主要分为以下几个部分: 一.Shell中的变量 任何编程语言中,有关变量的定义,作用范围,赋值等都是最最基础的知识. ...

  8. Linux Shell编程中的几个特殊符号命令 & 、&& 、 ||

    https://blog.csdn.net/hack8/article/details/39672145 Linux Shell编程中的几个特殊符号命令 & .&& . || ...

  9. 【转】shell编程下 特殊变量、test / [ ]判断、循环、脚本排错

    [转]shell编程下 特殊变量.test / [ ]判断.循环.脚本排错 第1章 shell中的特殊变量 1.1 $# $# 表示参数的个数 1.1.1 [示例]脚本内容 [root@znix ~] ...

随机推荐

  1. 洛谷 最小费用最大流 模板 P3381

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...

  2. POJ 2406 Power Strings 暴力

    emmmm 显然的是a串长度是s串长度的因数 我们可以暴力枚举因数然后暴力check #include<cstdio> #include<algorithm> #include ...

  3. 染色 color

    染色 color 题目描述 有一块矩阵平板,分成n*m个格子,一开始全是白色.在这上面进行k次染色,每次染色按照如下步骤:1. 随机选择一个格子,称为A.2. 随机选择一个格子,称为B.3. 将由A ...

  4. css实现0.5像素

    .border{ position: relative; } .border:before{ content: ''; position: absolute; width: 200%; height: ...

  5. 中英文混截,一个中文相当于n个英文

    项目中遇到这么个需求,截取中英文字符串,一个中文相当于2个英文,全英文时截取12个英文字母,全中文时是6个中文汉字,中英文混合时是12个字节,在网上有找到这样的解决方案,但我没能静下心来研究懂,于是自 ...

  6. [ CodeVS冲杯之路 ] P3145

     不充钱,你怎么AC? 题目:http://codevs.cn/problem/3145/ 经典的汉诺塔问题 我们移动的时候,如果是最小的1号就可以直接移动,否则先将上面的x-1号先移动到借用塔上,然 ...

  7. 飞扬的小鸟(NOIP2014)(丧病DP题)

    原题传送门 刚开始我还以为这道题目非常的简单.. 然后随便打了一个DP,直接WA,被zxyer狠狠地D了一顿. 然后发现有好多细节.. 首先假如某横坐标没有管子,那么l[x]=0;h[x]=m+1; ...

  8. ectouch 微信支付成功后订单状态未改变的解决办法 (转载)

    原文地址: 微信支付支付成功后,返回到mobile/wx_native_callback.php 之前代码 define('IN_ECS', true); require(dirname(__FILE ...

  9. AlarmManager定时闹钟

    一.AlarmManager介绍: AlarmManager是Android中常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent.简单的说就是我们设定一个时间,然后在该时间到来 ...

  10. Selenium2+python自动化19-单选框和复选框(radiobox、checkbox)【转载】

    本篇主要介绍单选框和复选框的操作 一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是圆的:下图复选框是方的,这个是业界的标准,要是开发小伙伴把图标弄错了 ...