接收鍵盤或其它檔案描述符的輸入。read
命令接收標準輸入(鍵盤)的輸入,或者其他檔案描述符的輸入。得到輸入後,read
命令將資料放入乙個標準變數中。
read 命令格式如下:
read [選項]
[變數名]
選項:
-p
:「提示資訊」:在等待read輸入時,輸出提示資訊;
-t
:秒數:read命令會一直等待使用者輸入,使用此選項可以指定等待時間;
-n
:字元數:read命令只接收指定的字元數就會執行;
-s
:隱藏輸入的資料,適用於機密資訊的輸入;
更詳細的命令說明,請檢視《linux命令:read-單行讀取資料》
變數名可以自定義。如果不指定變數名,則會把輸入儲存到預設變數reply
中;
如果只提供了乙個變數名,則將整個輸入行賦予該變數;
如果提供了乙個以上的變數名,則輸入行分為若干字,乙個接乙個地賦予各個變數,而命令列上的最後乙個變數取得剩餘的所有字元;
#!/bin/bash
#testing the read command
echo
-n "enter you name:"
#echo -n 讓使用者直接在後面輸入
read name #輸入的多個文字將儲存在乙個變數中
echo
"hello $name, welcome to my program."
執行:# ./read.sh
enter you name: wangtao
hello wangtao, welcome to my program.
#!/bin/bash
#testing the read -p option
read -p "please enter your age: " age
days=$[
$age
* 365 ]
echo
"that makes you over $days days old!"
執行:# ./age.sh
please enter your age: 23
that makes you over 8395 days old!
#!/bin/bash
# entering multiple variables
read -p "enter your name:" first last
echo
"checking data for $last, $first"
執行:# ./read1.sh
enter your name: a b
checking data
for b, a
#!/bin/bash
# testing the reply environment variable
read -p "enter a number: "
factorial=1
for(
( count=1; count< = $reply
; count++))
do factorial=$[
$factorial
*$count
]#等號兩端不要有空格
done
echo
"the factorial of $reply is $factorial"
執行:.
/read2.sh
enter a number: 6
the factorial of 6 is 720
#!/bin/bash
# timing the data entry
if read -t 5 -p "please enter your name: " name #記得加-p引數, 直接在read命令列指定提示符
then
echo
"hello $name, welcome to my script"
else
echo
echo
"sorry, too slow!"
fi執行:
# ./read3.sh
please enter your name:
sorry, too slow!
# ./read3.sh
please enter your name: wang
hello wang, welcome to my script
#!/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, goodbye"
exit;;
esac
執行:# ./read4.sh
do you want to continue
[y/n]? y
fine,
continue on...
./read4.sh
do you want to continue
[y/n]? n
ok, goodbye
#!/bin/bash
# hiding input data from the monitor
read -s -p "enter your passwd: " pass #-s 引數使得read讀入的字元隱藏
echo
echo
"is your passwd readlly $pass?"
~
執行:# ./read5.sh
enter your passwd:
is your passwd readlly osfile@206?
#!/bin/bash
# reading data from a file
count=1
cat test |
while read line
doecho
"line $count: $line"
count=$[
$count
+ 1 ]
done
echo
"finished processing the file"
執行: .
/read6.sh
line 1: the quick brown dog jumps over the lazy fox.
line 2: this is a test, this is only a test.
line 3: o romeo, romeo! wherefore art thou romeo?
finished processing the file
Lua教程(二十二) userdata
在lua中可以通過自定義型別的方式與c語言 更高效 更靈活的互動。這裡我們通過乙個簡單完整的示例來學習一下lua中userdata的使用方式。需要說明的是,該示例完全來自於programming in lua。其功能是用c程式實現乙個lua的布林陣列,以提供程式的執行效率。見下面的 和關鍵性注釋。複...
shell基礎教程
位置變數 特殊變數 陣列操作 字串測試 檔案判斷 邏輯運算 demo demo arg arg 1 2 表示函式的返回值 echo 配置服務服務測試 service test若這樣測試 service test start則會出現服務找不到錯誤 再次測試不出現列印結果,那是因為所有使用service...
python程式設計基礎之二十二
字典 字典屬於可變物件,但是不屬於序列,內部是通過雜湊方式儲存的,內部儲存的是乙個個鍵值對key value 字典的鍵是唯一的,字典查詢速度比較快 d1 括號裡面用鍵值對表示 d2 dict d3 dict 1,2 3,4 d4 dict 元素訪問 字典名 key 用鍵來訪問 字典名.get key...