linux脚本语句,LINUX shell 脚本语句
LINUX中shell腳本中語句的基本用法
一、if ...then...fi
if [? 條件判斷一 ] && (||) [ 條件判斷二 ]; 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/30
echo "Press y to continue"
read yn
if [ "$yn" = "y" ] || [?"$yn" = "Y"?]; then
echo "script is running..."
elif [?"$yn" = ""?];? then
echo "You must input parameters?"
else
echo "STOP!"
fi
二、case ... esac
case? 種類方式(string) in????????????????????????????????? ?<== 開始階段,種類方式可分成兩種類型, 通常使用 $1 這種直接輸入類型
種類方式一)
程序執行段
;;??????????????????? <==種類方式一的結束符號
種類方式二)
程序執行段
;;
*)
echo "Usage: { 種類方式一|種類方式二}"?? <==列出可以利用的參數值
exit 1
esac????????????????????? <== case結束處
種類方式(string)的格式主要有兩種:
·? 直接輸入:就是以“執行文件 + string ”的方式執行(/etc/rc.d/init.d? 里的基本設定方式),string可以直接寫成$1(在執行文件后直接加入第一個參數)。
·? 交? 互? 式:就是由屏幕輸出可能的項,然后讓用戶輸入,通常必須配合read variable,然后string寫成$variable 的格式。
如(交互式):
#!/bin/bash
# program:????? Using case mode
# 2013/12/30
echo "Press your select one, two, three"
read number
case $number in
one)
echo "your choice is one"
;;
two)
echo "your choice is two"
;;
three)
echo "your choice is three"
;;
*)
echo "Usage {one|two|three}"
exit 1
esac
三、循環語句
1、for (( 條件1; 條件2; 條件3))?????????????? ---已經知道運行次數
如:
#!/bin/bash
# Using for and loop
# 2013/12/30
declare -i s? #??????? <==變量聲明
for (( i=1; i<=100; i=i+1 ))
do
s=s+i
done
echo "The count is ==> $s"
2、for variable in variable1 variable2 .....
如:
#!/bin/bash
# using for...do ....done
# 2013/12/30
LIST="a aa aaa aaaa aaaaa"
for i in $LIST
do
echo $i
done
腳本執行結果如下:
a
aa
aaa
aaaa
aaaaa
3、while [ condition1 ] && { | | } [ condition2 ] ...???????????? --先判斷條件
如:
#!/bin/bash
# Using while and loop
# 2013/12/30
declare -i i
declare -i s
while [ "$i" != "101" ]
do
s=s+i
i=i+1
done
echo "The count is ==> $s"
4、until [ condition1 ] && { | | } [ condition2 ] ...?????????????? --先做后判斷條件
如:
#!/bin/bash? # Using until and loop? # 2013/12/30 declare -i i? declare -i s? until [ "$i" = "101" ]? do? ??????? s=s+i? ??????? i=i+1? done? echo "The count is ==> $s"
總結
以上是生活随笔為你收集整理的linux脚本语句,LINUX shell 脚本语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 公司通讯录
- 下一篇: 架构之美—软件架构6大步骤(开篇)