网站在服务器,福州seo网站建设,房地产公司网站建设与推广方案,wordpress 佛系汉化组LINUX中shell脚本中语句的基本用法一、if ...then...fiif [ 条件判断一 ] (||) [ 条件判断二 ]; then if 是起始#xff0c;后面可以接若干个判断式#xff0c;使用 或 || 执行判断elif [ 条件判断三 ] (||) [ …LINUX中shell脚本中语句的基本用法一、if ...then...fiif [ 条件判断一 ] (||) [ 条件判断二 ]; then if 是起始后面可以接若干个判断式使用 或 || 执行判断elif [ 条件判断三 ] (||) [ 条件判断四 ]; then 第二段判断如果第一段不符合要求就转到此搜寻条件执行第二段程序else 当前两段都不符合时就执行这段内容fi 结束 if then 的条件判断上面的意思是中括号[] 里的是条件表达式如果是复合条件判断(如若A 及B 则C 之类的逻辑判断)那么就需要在两个中括号之间加(and)或| | (or )这样的逻辑运算符。如果是多重选择那么需要以elif(可选的需要时才加上)新增另一个条件如果所有的条件都不适用则使用else (可选的)执行最后的内容。如#!/bin/bash# This program is used to study if then# 2013/12/30echo Press y to continueread ynif [ $yn y ] || [ $yn Y ]; thenecho script is running...elif [ $yn ]; thenecho You must input parameters elseecho STOP!fi二、case ... esaccase 种类方式(string) in 开始阶段种类方式可分成两种类型 通常使用 $1 这种直接输入类型种类方式一)程序执行段;; 种类方式一的结束符号种类方式二)程序执行段;;*)echo Usage: { 种类方式一|种类方式二} 列出可以利用的参数值exit 1esac case结束处种类方式(string)的格式主要有两种· 直接输入就是以“执行文件 string ”的方式执行(/etc/rc.d/init.d 里的基本设定方式)string可以直接写成$1(在执行文件后直接加入第一个参数)。· 交 互 式就是由屏幕输出可能的项然后让用户输入通常必须配合read variable然后string写成$variable 的格式。如(交互式)#!/bin/bash# program: Using case mode# 2013/12/30echo Press your select one, two, threeread numbercase $number inone)echo your choice is one;;two)echo your choice is two;;three)echo your choice is three;;*)echo Usage {one|two|three}exit 1esac三、循环语句1、for (( 条件1; 条件2; 条件3)) ---已经知道运行次数如#!/bin/bash# Using for and loop# 2013/12/30declare -i s # 变量声明for (( i1; i100; ii1 ))dossidoneecho The count is $s2、for variable in variable1 variable2 .....如#!/bin/bash# using for...do ....done# 2013/12/30LISTa aa aaa aaaa aaaaafor i in $LISTdoecho $idone脚本执行结果如下aaaaaaaaaaaaaaa3、while [ condition1 ] { | | } [ condition2 ] ... --先判断条件如#!/bin/bash# Using while and loop# 2013/12/30declare -i ideclare -i swhile [ $i ! 101 ]dossiii1doneecho The count is $s4、until [ condition1 ] { | | } [ condition2 ] ... --先做后判断条件如#!/bin/bash # Using until and loop # 2013/12/30 declare -i i declare -i s until [ $i 101 ] do ssi ii1 done echo The count is $s