shell編程基礎(chǔ)2

一、shell腳本傳參

4.1.shell文件中輸入

echo $1 $2 $3

su test.sh one two three
打印出對(duì)應(yīng)輸入的$1 $2 $3參數(shù)one two three
也可以使用args數(shù)組方式存儲(chǔ)參數(shù)

args=("$@")
echo ${args[0]} ${args[1]} ${args[2]}

還有簡(jiǎn)潔的方式

args=("$@")
#echo ${args[0]} ${args[1]} ${args[2]}
echo $@

二、if-else語(yǔ)句

在shell中的if控制語(yǔ)句很像python中的語(yǔ)

  1. if -then
if condition
then
    command1
    command2
    ...
    commandN
fi

2.if--else語(yǔ)句

if condition
then
    command1
    command2
    ...
    commandN
else
    command
fi

例如:

echo -e "Enter the name of file: \c"
read filename

if [ -e $filename ]#檢查文件是否存在
then
  echo "File found"
else 
  echo "File is not exist or not found"
fi

echo -e "Enter the name of file: \c"
read filename

if [ -f $filename ]#檢查文件是常規(guī)文件
then
  echo "File found"
else 
  echo "File is not exist or not found"
fi

echo -e "Enter the name of file: \c"
read filename

if [ -d $filename ]#-d表示directory,判斷是否是一個(gè)目錄
then
  echo "File found"
else 
  echo "File is not exist or not found"
fi

注意 -e表示是否存在
-f表示file,判斷是否是常規(guī)的文件
-d表示directory,判斷是否是一個(gè)目錄。
-e表示empty,默認(rèn)文件是不為空。
3.if-then-elif-then-else

if condition1
then
    command1
elif condition2
then
    command2
else
    commandN
fi

例如:

a=10
b=20
if [ $a == $b ]
then
   echo "a == b"
elif [ $a -gt $b ]
then
   echo "a > b"
elif [ $a -lt $b ]
then
   echo "a < b"
else
   echo "Ineligible"
fi

4.利用if在文件尾部寫(xiě)入內(nèi)容
首先要判斷是不是常規(guī)文件,如果是,在判斷是否有寫(xiě)的權(quán)限,如果有寫(xiě)得權(quán)限需要輸入,并不輸入的內(nèi)容保持到文件的維度
上面可以看出這里需要嵌套if,
文件寫(xiě)入需要用到cat命令 cat > file 就是覆蓋 cat >> file就是不覆蓋,直接在后面寫(xiě)入新內(nèi)容

echo -e "echo -e "Enter the name of file: \c"
read filename

if [ -f $filename ]#判斷文件是否存在
then
  if [-w $filename ]#判斷寫(xiě)權(quán)限
   then
    echo "type some test date. press ctrl+d to quit."
    cat >> $filename
  else
    echo "$filename do not have write permissiong"
  fi
else
  echo "$filename is not exit"
fi

2.for循環(huán)
循環(huán)一般格式為:

for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
dovimne

例如:

for str in This is a string
do
    echo $str
done

還有一種
for (( 表達(dá)式1; 表達(dá)式2;表達(dá)式3))
do
command1
command2
command3
done
例如

t=0
for ((i=1; i<=100; i++ ))
do
  t=$(( t +i ))
done
echo $t

使用sh filename.tes這里會(huì)發(fā)現(xiàn)Syntax error: Bad for loop variable
因?yàn)閟h 命令執(zhí)行腳本的時(shí)候?qū)嶋H使用的是 dash,而 dash 不支持這種 C 語(yǔ)言格式的 for 循環(huán)寫(xiě)法。
所以,使用 bash 代替 sh 運(yùn)行腳本 bash filename.tes

3.while語(yǔ)句
1.常見(jiàn)的while語(yǔ)句格式

while condition
do
    command
done

例如

#!/bin/bash
int=1
while(( $int<=5 ))
do
    echo $int
    let "int++"
done

無(wú)線循環(huán)

while :
do
    command
done
或者
while true
do
    command
done
或者前面學(xué)到的for
for (( ; ; ))

4.until循環(huán),until 循環(huán)執(zhí)行一系列命令直至條件為真時(shí)停止
格式

until condition
do
    command
done

例如:打印1到9

n=1
until [ $n -ge 10 ]
do
  echo "$n"
  (( n++ ))
done

5.case
case 語(yǔ)句匹配一個(gè)值與一個(gè)模式,如果匹配成功,執(zhí)行相匹配的命令。case 語(yǔ)句格式如:

case 值 in
模式1)
    command1
    command2
    ...
    commandN
    ;;
模式2)
    command1
    command2
    ...
    commandN
    ;;
esac
注意:取值后面必須為單詞 in,每一模式必須以右括號(hào)結(jié)束。取值可以為變量或常數(shù)。匹配發(fā)現(xiàn)取值符合某一模式后,其間所有命令開(kāi)始執(zhí)行直至 ;;。
取值將檢測(cè)匹配的每一個(gè)模式。一旦模式匹配,則執(zhí)行完匹配模式相應(yīng)命令后不再繼續(xù)其他模式。如果無(wú)一匹配模式,使用星號(hào) * 捕獲該值,再執(zhí)行后面的命令。

例如:

echo 'Enter a number between 1 and 4:'
echo 'The number you entered is:'
read aNum
case $aNum in
    1)  echo 'You have chosen 1'
    ;;
    2)  echo 'You have chosen 2'
    ;;
    3)  echo 'You have chosen 3'
    ;;
    4)  echo 'You have chosen 4'
    ;;
    *)  echo 'You did not enter a number between 1 and 4'
    ;;
esac

6.跳槽循環(huán)
break 和 continue
break 命令允許跳出所有循環(huán)(終止執(zhí)行后面的所有循環(huán))
例如

while :
do
    echo -n "Enter a number between 1 and 5:"
    read aNum
    case $aNum in
        1|2|3|4|5) echo "The number you entered is $aNum!"
        ;;
        *) echo "The number you entered is not between 1 and 5! game over!"
            break
        ;;
    esac
done

continue 命令與 break 命令類似,只有一點(diǎn)差別,它不會(huì)跳出所有循環(huán),只跳出當(dāng)前循環(huán)。

while :
do
    echo -n "Enter a number between 1 and 5: "
    read aNum
    case $aNum in
        1|2|3|4|5) echo "The number you entered is $aNum!"
        ;;
        *) echo "The number you entered is not between 1 and 5!"
            continue
            echo "game over"
        ;;
    esac
done

三、數(shù)組

arr=('Alice' 'Jone' 'Jack' 'Lili')
arr[4]='Alex'#添加元素

echo "${arr[@]}"#遍歷數(shù)組
echo "${arr[1]}"#單個(gè)制定位置的元素
echo "${!arr[@]}"#數(shù)組的索引查詢
echo "${#arr[@]}"#數(shù)組的長(zhǎng)度
unset  arr[1]# 刪除數(shù)組

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容