1、read基本讀取
1 #!/bin/bash 2執行:#testing the read command 3
4echo -n "
enter you name:
" #echo -n 讓使用者直接在後面輸入
5read name #輸入的多個文字將儲存在乙個變數中 6
echo
"hello $name, welcome to my program.
"
# ./read.sh2、read -p (直接在read命令列指定提示符)enter you name: wangtao
hello wangtao, welcome to my program.
1 #!/bin/bash執行:2 #testing the read -p option
3 read -p "
please enter your age:
"age
4 days=$[ $age * 365]
5echo
"that makes you over $days days old!
"
# ./age.sh3、read -p (指定多個變數)please enter your age:
23that makes you over
8395 days old!
1 #!/bin/bash 2執行:# entering multiple variables 3
4 read -p "
enter your name:
" first last
5echo
"checking data for $last, $first
"
# ./read1.sh4、read 命令中不指定變數,那麼read命名將它收到的任何資料都放在特殊環境變數reply中enter your name: a b
checking data
for b, a
1 #!/bin/bash 2執行:# testing the reply environment variable 3
4 read -p "
enter a number:
"5 factorial=1
6for (( count=1; count<= $reply; count++)) 7
do8 factorial=$[ $factorial *$count ] #等號兩端不要有空格 9
done
10echo
"the factorial of $reply is $factorial
"
./read2.sh5、超時, 等待輸入的秒數(read -t)enter a number:
6the factorial of
6 is 720
1 #!/bin/bash 2執行:# timing the data entry 3
4if read -t 5 -p "
please enter your name:
"name #記得加-p引數, 直接在read命令列指定提示符 5
then
6echo
"hello $name, welcome to my script"7
else
8echo
9echo
"sorry, too slow!"10
fi
# ./read3.shplease enter your name:
sorry, too slow!
# ./read3.sh5、read命令對輸入的字元判斷please enter your name: wang
hello wang, welcome to my script
1 #!/bin/bash 2執行:# getting just one character of input 3
4 read -n1 -p "
do you want to continue [y/n]?
"answer 5
case $answer in
6 y | y) echo
7echo
"fine, continue on...";;
8 n | n) echo
9echo
"ok, goodbye"10
exit;; 11
esac
# ./read4.sh6、隱藏方式讀取(read -s)do you want to continue [y/n]?y
fine, continue on...
./read4.sh
do you want to continue [y/n]?n
ok, goodbye
1 #!/bin/bash 2執行:# hiding input data from the monitor 3
4 read -s -p "
enter your passwd:
"pass #-s 引數使得read讀入的字元隱藏 5
echo
6echo
"is your passwd readlly $pass?
"~
# ./read5.sh7、從文字中讀取enter your
passwd
: is your
passwd readlly osfile@206?
1 #!/bin/bash執行:2 # reading data from a file
34 count=1
5cat test | while
read line 6
do7echo
"line $count: $line
"8 count=$[ $count + 1]
9done
10echo
"finished processing the file
"
./read6.shline
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
摘自:
Linux中Shell的用法
本文結合大量例項闡述如何編寫乙個shell指令碼。為什麼要進行shell程式設計 在linux系統中,雖然有各種各樣的圖形化介面工具,但是sell仍然是乙個非常靈活的工具。shell不僅僅是命令的收集,而且是一門非常棒的程式語言。您可以通過使用shell使大量的任務自動化,shell特別擅長系統管理...
Linux下shell命令用法和例項總結 du
linux下shell命令用法和例項總結 du。it老兵部落格。du 選項 檔案 du disk usage 的簡稱 命令用於檢查計算機上檔案和目錄的磁碟使用情況,可以遞迴顯示檔案和目錄。顯示每個檔案和目錄的磁碟使用空間。例項 找出 root目錄樹及其每個子目錄的磁碟使用情況摘要 描述 以下命令的輸...
SHELL下date命令的用法
date命令如何獲得上星期的日期?date d 1 week y m d a 舉一反三 date命令獲得前天,昨天,明天,後天,上個月,下個月的日期 date d 2 day y m d a 或 date date 2 days ago y m d a date d 1 day y m d a 或 ...