處理用戶輸入

demo1
簡單的讀取單個參數(shù)

#!/bin/bash
#ussing one command line parameter

factorial=1
for (( number = 1; number <= $1; number++ ))
do
    factorial=$[ $factorial * $number ]
done
echo the factorial of $1 is $factorial

//
hadoop@master:~/shell_test/cin$ ./test1 3
the factorial of 3 is 6
hadoop@master:~/shell_test/cin$ ./test1 4
the factorial of 4 is 24

注意:
如果有多個參數(shù),依次為$1 - $9,如果超過9個
如10察郁,則為${10}

demo2
讀取程序名字

#!/bin/bash
#testting $0 parameter

echo The command enter is: $0

#not include path
name=`basename $0`

echo the commadn name is $name

//result
hadoop@master:~/shell_test/cin$ ./test2
The command enter is: ./test2
the commadn name is test2

demo3
測試參數(shù)是否存在

#!/bin/bash
#testing parameter before use

if [ -n "$1" ]
then
    echo Hello $1 gland to meet you
else
    echo "sorry, you dont identify yourself."
fi
//result
hadoop@master:~/shell_test/cin$ ./test3
sorry, you dont identify yourself.
hadoop@master:~/shell_test/cin$ ./test3 a
Hello a gland to meet you

特殊參數(shù)變量

(1)$#
$# 統(tǒng)計命令行參數(shù)個數(shù)
if [ $# -ne 2 ]
then
fi
注意在字符串中直接輸出需要厉熟,${!#}
(2) $* $@
$* 會將所有參數(shù)當成一個參數(shù)
$@ 變量會單獨處理每個參數(shù)

demo4
shift移動變量

#!/bin/bash
# demonstrating the shift command

count=1
while [ -n "$1" ]
do
    echo  "Parameter #$count = $1"
    count=$[ $count +1 ]
    shift
done

//result
hadoop@master:~/shell_test/cin$ ./test4 kfd fjdslf
Parameter #1 = kfd
Parameter #2 = fjdslf
注意:
shift 2 移動兩個參數(shù)

demo5
(1)處理簡單選項

#!/bin/bash
#extracting command line options as parameters

while [ -n "$1" ]
do
    case "$1" in
    -a) echo "Fond the -a option" ;;
    -b) echo "Fond the -b option" ;;
    -c) echo "Found the -c option" ;;
    *) echo "$1 is not an option" ;;
    esac
    shift
done
//
hadoop@master:~/shell_test/cin$ ./test5 -c
Found the -c option
hadoop@master:~/shell_test/cin$ ./test5 -b
Fond the -b option
hadoop@master:~/shell_test/cin$ ./test5 -d
-d is not an option

(2)分離參數(shù)和選項

#!/bin/bash
#extracting command line options as parameters
#-- as a seperate 

while [ -n "$1" ]
do
    case "$1" in
    -a) echo "Fond the -a option" ;;
    -b) echo "Fond the -b option" ;;
    -c) echo "Found the -c option" ;;
    --) shift
        break ;;
    *) echo "$1 is not an option" ;;
    esac
    shift
done

count=1
for parm in $@
do
    echo "Parametrer #$count : $parm"
    count=$[ $count + 1 ]
done

//
hadoop@master:~/shell_test/cin$ ./test6 -a -b -c -- fjd fdsjfld jflds
Fond the -a option
Fond the -b option
Found the -c option
Parametrer #1 : fjd
Parametrer #2 : fdsjfld
Parametrer #3 : jflds
hadoop@master:~/shell_test/cin$ 

(3)處理帶值的選項

#!/bin/bash
#extracting command line options as parameters
#-- as a seperate 

while [ -n "$1" ]
do
    case "$1" in
    -a) echo "Fond the -a option" ;;
    -b) param=$2
        echo "Found the -b option , with paramerter value $param"
        shift  ;;
    -c) echo "Found the -c option" ;;
    --) shift
        break ;;
    *) echo "$1 is not an option" ;;
    esac
    shift
done

count=1
for parm in $@
do
    echo "Parametrer #$count : $parm"
    count=$[ $count + 1 ]
done
//result
hadoop@master:~/shell_test/cin$ ./test7 -a -b nihao -c
Fond the -a option
Found the -b option , with paramerter value nihao
Found the -c option
hadoop@master:~/shell_test/cin$ 

demo6
獲取用戶輸入

#!/bin/bash
#testing the read command

echo -n "Enter your name"
read name
echo "Heollo $name , welcome to my program"

//
hadoop@master:~/shell_test/cin$ ./test8
Enter your namechenhanghang
Heollo chenhanghang , welcome to my program
注意:
(1)-p 直接指定提示符號
read -p "input your name" name
(2)如果不指定變量名字,則會把變量放在 REPLY中
(3)-s 以隱藏方式讀取

demo7
超時退出

#!/bin/bash
#timing the data entry

if read -t 5 -p "please input your name:" name
then
    echo "Hello $name, welcome to my script"
else
    echo
    echo "Sorry, too slow"
fi

//
hadoop@master:~/shell_test/cin$ ./test9
please input your name:
Sorry, too slow
hadoop@master:~/shell_test/cin$

demo9
限定字符的個數(shù)

#!/bin/bash
#getting just one character of input

read -n1 -p "Do you want to continue [Y/N] " answer
case $answer in
Y | y) echo
    echo "fine ,continue on..." ;;
N | n) echo
    echo "OK googbye"
    exit ;;
esac
echo "This is the end of the script"

//result
hadoop@master:~/shell_test/cin$ ./test10
Do you want to continue [Y/N] Y
fine ,continue on...
This is the end of the script
hadoop@master:~/shell_test/cin$ 
hadoop@master:~/shell_test/cin$ ./test10
Do you want to continue [Y/N] N
OK googbye
hadoop@master:~/shell_test/cin$ 

demo10

#!/bin/bash
#reading data from file

count=1

cat q | while read line
do
    echo "Line $count: $line"
    count=$[ $count +1 ]
done
echo Finish processing the data

//result
hadoop@master:~/shell_test/cin$ ./test11
Line 1: #!/bin/bash
Line 2: #getting just one character of input
Line 3: 
Line 4: read -n1 -p "Do you want to continue [Y/N] " answer
Line 5: case $answer in
Line 6: Y | y) echo
Line 7: echo "fine ,continue on..." ;;
Line 8: N | n) echo
Line 9: echo "OK googbye"
Line 10: exit ;;
Line 11: esac
Line 12: echo "This is the end of the script"
Line 13: 
Finish processing the data
hadoop@master:~/shell_test/cin$ 

demo 12
getopts 工具獲取命令行參數(shù)

#!/bin/bash
#processing options and parameters with getopts

while getopts :ab:cd opt
do
    case "$opt" in
    a) echo "Found the -a option" ;;
    b) echo "Found the -b option with the value $OPTARG" ;;
    c) echo "Found the -c option" ;;
    d) echo "Found the -d option" ;;
    *) echo "Unhnown option: $opt" ;;
    esac
done
shift $[ $OPTIND -1 ]

count=1
for param in "$@"
do
    echo "Parameter $count: $param"
    count=$[ $count + 1 ]
done

adoop@master:~/shell_test/cin$ ./test12 -a -b test1 -d test2 test3 test4
Found the -a option
Found the -b option with the value test1
Found the -d option
Parameter 1: test2
Parameter 2: test3
Parameter 3: test4
hadoop@master:~/shell_test/cin$ ./test12 -a -b  -d test2 test3 test4
Found the -a option
Found the -b option with the value -d
Parameter 1: test2
Parameter 2: test3
Parameter 3: test4
hadoop@master:~/shell_test/cin$ 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖迹栓,帶你破解...
    沈念sama閱讀 222,464評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異俭缓,居然都是意外死亡克伊,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評論 3 399
  • 文/潘曉璐 我一進店門华坦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來愿吹,“玉大人,你說我怎么就攤上這事惜姐±绻颍” “怎么了?”我有些...
    開封第一講書人閱讀 169,078評論 0 362
  • 文/不壞的土叔 我叫張陵歹袁,是天一觀的道長坷衍。 經(jīng)常有香客問我,道長条舔,這世上最難降的妖魔是什么枫耳? 我笑而不...
    開封第一講書人閱讀 59,979評論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮孟抗,結(jié)果婚禮上迁杨,老公的妹妹穿的比我還像新娘。我一直安慰自己凄硼,他們只是感情好铅协,可當我...
    茶點故事閱讀 69,001評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著帆喇,像睡著了一般警医。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上坯钦,一...
    開封第一講書人閱讀 52,584評論 1 312
  • 那天,我揣著相機與錄音侈玄,去河邊找鬼婉刀。 笑死,一個胖子當著我的面吹牛序仙,可吹牛的內(nèi)容都是我干的突颊。 我是一名探鬼主播,決...
    沈念sama閱讀 41,085評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼律秃!你這毒婦竟也來了爬橡?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,023評論 0 277
  • 序言:老撾萬榮一對情侶失蹤棒动,失蹤者是張志新(化名)和其女友劉穎糙申,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體船惨,經(jīng)...
    沈念sama閱讀 46,555評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡柜裸,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,626評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了粱锐。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片疙挺。...
    茶點故事閱讀 40,769評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖怜浅,靈堂內(nèi)的尸體忽然破棺而出铐然,到底是詐尸還是另有隱情,我是刑警寧澤恶座,帶...
    沈念sama閱讀 36,439評論 5 351
  • 正文 年R本政府宣布锦爵,位于F島的核電站,受9級特大地震影響奥裸,放射性物質(zhì)發(fā)生泄漏险掀。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,115評論 3 335
  • 文/蒙蒙 一湾宙、第九天 我趴在偏房一處隱蔽的房頂上張望樟氢。 院中可真熱鬧,春花似錦侠鳄、人聲如沸埠啃。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,601評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽碴开。三九已至,卻和暖如春博秫,著一層夾襖步出監(jiān)牢的瞬間潦牛,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,702評論 1 274
  • 我被黑心中介騙來泰國打工挡育, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留巴碗,地道東北人。 一個月前我還...
    沈念sama閱讀 49,191評論 3 378
  • 正文 我出身青樓即寒,卻偏偏與公主長得像橡淆,于是被迫代替她去往敵國和親召噩。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,781評論 2 361

推薦閱讀更多精彩內(nèi)容