日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux命令之read命令

發(fā)布時間:2023/12/20 linux 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux命令之read命令 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、命令簡介

??Linux read命令用于從標(biāo)準(zhǔn)輸入讀取數(shù)值。read 內(nèi)部命令被用來從標(biāo)準(zhǔn)輸入讀取單行數(shù)據(jù)。這個命令可以用來讀取鍵盤輸入,當(dāng)使用重定向的時候,可以讀取文件中的一行數(shù)據(jù)。可以直接在命令行下直接,但常用于在shell腳本中輸入自定義變量值。

二、使用示例

0、直接執(zhí)行

[root@test1 scripts]# read
this is a test

1、-p參數(shù)給定輸入提示

  • shell腳本

#!/bin/bash
read -p “輸入姓名:” name
echo “你輸入的姓名是:$name”
exit 0

  • 執(zhí)行結(jié)果

[root@test1 scripts]# sh readshow.sh
輸入姓名:吳先生
你輸入的姓名是:吳先生

2、-s參數(shù)隱藏輸入內(nèi)容

  • shell腳本

#!/bin/bash

read -s -p “輸入密碼:” passwd
echo -e “\n你輸入的密碼是:$passwd”
exit 0

  • 執(zhí)行結(jié)果

[root@test1 scripts]# sh readshow.sh
輸入密碼:
你輸入的密碼是:Test123

3、-t參數(shù)指定等待輸入秒數(shù)

  • shell腳本

#!/bin/bash

if read -t 5 -p “輸入姓名:” name
then
echo “你輸入的姓名是 $name”
else
echo -e “\n抱歉,你輸入超時了?!?br /> fi
exit 0

  • 執(zhí)行結(jié)果

[root@test1 scripts]# sh readshow.sh
輸入姓名:
抱歉,你輸入超時了。

4、-n參數(shù)控制輸入字符,達(dá)到數(shù)目自動結(jié)束輸入

  • shell腳本

#!/bin/bash

read -n1 -p “Do you want to continue [Y/N]?” answer
case $answer in
Y | y)
echo -e “\nfine ,continue”;;
N | n)
echo -e “\nok,good bye”;;
*)
echo -e “\nerror choice”;;
esac
exit 0

  • 執(zhí)行結(jié)果

[root@test1 scripts]# sh readshow.sh
Do you want to continue [Y/N]?y
fine ,continue
[root@test1 scripts]# sh readshow.sh
Do you want to continue [Y/N]?N
ok,good bye
[root@test1 scripts]# sh readshow.sh
Do you want to continue [Y/N]?1
error choice

5、讀取文件

  • shell腳本

[root@test1 scripts]# cat name.list
wuhs
sunru
bluesky
[root@test1 scripts]# cat readshow.sh
#!/bin/bash
\
count=1
cat name.list | while read line # cat命令的輸出作為read命令的輸入,read讀到>的值放在line中
do
echo -e “Line $count:$line”
count=$[ $count + 1 ] # 注意中括號中的空格。
done
exit 0

  • 執(zhí)行結(jié)果

[root@test1 scripts]# sh readshow.sh
Line 1:wuhs
Line 2:sunru
Line 3:bluesky

三、命令語法及參數(shù)說明

1、命令語法

用法:: read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name …]

2、參數(shù)說明

  • -a 后跟一個變量,該變量會被認(rèn)為是個數(shù)組,然后給其賦值,默認(rèn)是以空格為分割符。
  • -d 后面跟一個標(biāo)志符,其實(shí)只有其后的第一個字符有用,作為結(jié)束的標(biāo)志。
  • -p 后面跟提示信息,即在輸入前打印提示信息。
  • -e 在輸入的時候可以使用命令補(bǔ)全功能。
  • -n 后跟一個數(shù)字,定義輸入文本的長度,很實(shí)用。
  • -r 屏蔽\,如果沒有該選項(xiàng),則\作為一個轉(zhuǎn)義字符,有的話 \就是個正常的字符了。
  • -s 屏蔽回顯,在輸入字符時不再屏幕上顯示,例如login時輸入密碼。
  • -t 后面跟秒數(shù),定義輸入字符的等待時間。
  • -u 后面跟fd,從文件描述符中讀入,該文件描述符可以是exec新開啟的。

總結(jié)

以上是生活随笔為你收集整理的Linux命令之read命令的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。