Linux之shell用戶輸入


講完了shell的變量、條件判斷和循環後,今天再來講以下如何處理用戶的輸入。

1.1命令行參數

我們編寫shell腳本的時候,可能有些是需要用戶傳遞參數進去的,命令行參數是最常用的方式。$0是可以獲取程序名,$1獲取第一個參數,$2獲取第二個參數,以此類推,不同的參數用空格分隔,如果參數本身包含空格,必須使用單引號或雙引號包起來。直接上示例:

1.1.1參數本身沒有空格的話,加不加引號無所謂
[root@aliyun-hk1 linux-shell-test]# cat shell-input.sh
#!/bin/bash

echo $(basename $0)
echo $0
echo $1
echo $2
[root@aliyun-hk1 linux-shell-test]# ./shell-input.sh 1 2 3
shell-input.sh
./shell-input.sh
1
2
1.1.2參數本身有空格,不加引號的話,會被當做多個變量處理,用引號包起來纔對
[root@aliyun-hk1 linux-shell-test]# ./shell-input.sh "hello world" "nihao zhongguo"
shell-input.sh
./shell-input.sh
hello world
nihao zhongguo
[root@aliyun-hk1 linux-shell-test]# ./shell-input.sh hello world nihao zhongguo
shell-input.sh
./shell-input.sh
hello
world
1.1.3檢查參數是否爲空
[root@aliyun-hk1 linux-shell-test]# cat shell-input.sh
#!/bin/bash

if [ -n "$1" ]
then
  echo $(basename $0)
  echo $0
  echo $1
fi
[root@aliyun-hk1 linux-shell-test]# ./shell-input.sh
[root@aliyun-hk1 linux-shell-test]# ./shell-input.sh 10
shell-input.sh
./shell-input.sh
10
[root@aliyun-hk1 linux-shell-test]#

-z判斷變量的值,如果爲空返回0,否則返回1;-n判斷變量的值,如果爲空返回1,否則返回0,使用[]的時候,變量用雙引號包起來。

1.1.4檢查參數的個數
先打印變量的個數,如果變量個數大於0,輸出它。
[root@aliyun-hk1 linux-shell-test]# cat shell-input.sh
#!/bin/bash

echo "$#"

if [ "$#" -gt 0 ]
then
  echo $(basename $0)
  echo $0
  echo $1
fi
[root@aliyun-hk1 linux-shell-test]# ./shell-input.sh
0
[root@aliyun-hk1 linux-shell-test]# ./shell-input.sh 10 11
2
shell-input.sh
./shell-input.sh
10
[root@aliyun-hk1 linux-shell-test]#
1.1.5 抓取所有的參數值

抓取所有參數,並且當作一個整體處理,使用$*

[root@aliyun-hk1 linux-shell-test]# cat shell-input.sh
#!/bin/bash

echo "$*"
count1=0

for a in "$*"
do
  echo "$a"
  count1=$[ $count1 +1 ]
  echo "$count1"
done

[root@aliyun-hk1 linux-shell-test]# ./shell-input.sh 10 20 hello
10 20 hello
10 20 hello
1

抓取所有參數,並且把每個參數分開處理,使用$@

[root@aliyun-hk1 linux-shell-test]# cat shell-input.sh
#!/bin/bash

echo "$@"
count1=0

for a in "$@"
do
  count1=$[ $count1 +1 ]
  echo "$count1"
  echo "$a"
done

[root@aliyun-hk1 linux-shell-test]# ./shell-input.sh 10 20 hello
10 20 hello
1
10
2
20
3
hello

1.2獲取用戶輸入

shell腳本運行時,可能有時候需要用戶臨時輸入參數,繼續後面的操作。接下來,我們就來講講獲取用戶輸入的多種方式。

1.2.1 基本的讀取

使用read name讀取輸入到name,使用read -s password讀取輸入到password並且不shell上隱藏輸入。

[root@aliyun-hk1 linux-shell-test]# cat shell-input.sh
#!/bin/bash

echo -n "please input your name:"
read name
echo -n "please input your password:"
read -s password
echo ""
echo "your user name is $name, password is $password"
[root@aliyun-hk1 linux-shell-test]# ./shell-input.sh
please input your name:robin
please input your password:
your user name is robin, password is nihao
[root@aliyun-hk1 linux-shell-test]#

1.2.2 設置等待用戶超時時間
[root@aliyun-hk1 linux-shell-test]# cat shell-input.sh
#!/bin/bash

if read -t 5 -p "please input name" name
then
   echo "hello $name"
else
   echo
   echo "sorry fo timeout"
fi
[root@aliyun-hk1 linux-shell-test]# ./shell-input.sh
please input name
sorry fo timeout
[root@aliyun-hk1 linux-shell-test]#

1.2.3 從文件讀取參數

最後再講講如何從文件讀入參數,並直接來使用。

[root@aliyun-hk1 linux-shell-test]# cat shell-input.sh
#!/bin/bash

count=1
cat test | while read line
do
  echo "Line $count: $line"
  count=$[ $count + 1 ]
done

echo ”finish read the file"
[root@aliyun-hk1 linux-shell-test]# vim test
[root@aliyun-hk1 linux-shell-test]# ./shell-input.sh
Line 1: 10
Line 2: 20
Line 3: hello
finish read the file
[root@aliyun-hk1 linux-shell-test]#

1.3 參數標準化

在linux世界裏,對於傳遞給shell程序的選項,已經有了某種程序的保準含義。我們在自己編寫shell腳本時,也可以參考該標準。

-a 顯示所有對象
-c 生成一個計數器
-d 一定一個目錄
-e 擴展一個對象
-f 指定讀入數據的文件
-h 顯示命令的幫助信息
-i 忽略文本的大小寫
-l 顯示輸出的長格式版本
-n 使用非交互模式
-o 將所有輸出重定向到指定的文件
-q 以安靜模式運行
-r 遞歸的處理目錄和文件
-s 以安靜模式運行
-v 生成詳細輸出
-x 排除某個對象
-y 對所有問題回答yes
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章