1. 程式人生 > >Linux上read命令的使用

Linux上read命令的使用

文件描述符 repl num rgs 傾聽 word 目錄 linux linu

一:read傾聽是一種美德 1.傾聽鍵盤的輸入並保存到變量中 例如:#! /bin/bash echo "please input your name" read name echo "welcome $name !" exit 0 或是read自帶的顯示提示語 例如: #! /bin/bash read -p "please input two args" name place echo "welcome $name to $place" exit 0 2.如果不輸入變量,其值保存在REPLY變量中 例如: #! /bin/bash read -p "please input two args" echo "the content of you just input is $REPLY" exit 0 3.read加時間限制 #! /bin/bash # 5秒內不輸入則 if read -t 5 -p "please input two args" then echo "the content of you just input is $REPLY" else echo "sorry you are too late" exit 0 4.隱蔽輸入內容 #! /bin/bash read -s -p "please input your password" password echo "here,your password is $password" 5.使用 -u選項讀取文件 #! /bin/bash exec 3<~/文檔/test.txt # ~/文檔/test.txt代表的是讀取的文件目錄 count=0 while read -u 3 -r var #這裏使用-r預防讀入每行時未正常換行 do let count=$count+1 #let是執行計算的命令 echo "LIne $count:$var" done echo "finished!" echo "Line num is $count" exec 3<&- #關閉文件描述符

Linux上read命令的使用