第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Linux 基礎(chǔ)教程 45-read命令

標(biāo)簽:
Linux

基本用法

    read命令主要用于从标准输入读取内容或从文件中读取内容,并把信息保存到变量中。其常用用法如下所示:

read [选项] [文件]
选项解释
-a array将内容读取到数值中,变量默认为数组且以空格做为分割符
-d delimiter遇到指定的字符即停止读取
-n nchars指定最多可以读入的字符数,即定义输入文本的长度
-r屏蔽转义符
-p prompt显示提示信息
-s静默模式,在输入字符时不在终端中显示,常用于密码输入等
-t timeout指定超时时间
-u FD从文件描述符中读入,该FD可以由exec开启

用法示例

1、从标准输入读入

[root@localhost test]# cat read.sh#!/bin/bashecho -n "Please input your name:"read nameecho "Hello $name"[root@localhost test]# bash read.shPlease input your name:Jack
Hello Jack

2、指定显示信息从标准输入读入

[root@localhost test]# cat read.sh#!/bin/bash# echo -n "Please input your name:"read -p "Please input your name:" name# read nameecho "Hello $name"[root@localhost test]# bash read.shPlease input your name:Jack
Hello Jack

在以上示例中,read是一次可以接受多个参数的,如下所示:

read -p "Please input your name:" firstName secondName lastName

但需要注意的事项如下:

  • 如果输入的数据少于变量个数,则多余的变量不会获取到数据,即变量值为空

  • 如果输入的数据多于变量个数,则超出的数据将全部赋给最后一个变量

  • 如果在read命令后面没有定义任何变量,脚本在执行时,如果用户输入数据,此时数据则保存到环境变量$REPLY

3、指定超时时间

[root@localhost test]# cat read.sh#!/bin/bashif read -t 3 -p "Please input your name:" firstName secondName lastNamethen
  echo "variable is $firstName - $secondName - $lastName"else
   echo -e  "\ntimeout\n"fi[root@localhost test]# bash read.shPlease input your name:
timeout

4、从指定文件中读取内容

[root@localhost test]# cat -n test.txt
     1  this is test text.
     2  this is second line.
     3  Hello world
     4  C# Main
     5  Python# 使用-u选项[root@localhost test]# cat readtest.sh#!/bin/bashexec 5< ~/test/test.txt
count=0while read -u 5 vardo
 let count=$count+1 echo "Line $count is $var"doneecho "Total line count is $count"exec 5<&-
[root@localhost test]# bash readtest.shLine 1 is this is test text.
Line 2 is this is second line.
Line 3 is Hello world
Line 4 is C# MainLine 5 is Python
Total line count is 5# 使用管道[root@localhost test]# cat readtest.sh#!/bin/bashcount=1
cat ~/test/test.txt |  while read linedo
 echo "Current line $count - $line "
 let count=$count+1doneecho "Total line count is $count"[root@localhost test]# bash readtest.shCurrent line 1 - this is test text.
Current line 2 - this is second line.
Current line 3 - Hello world
Current line 4 - C# MainCurrent line 5 - Python
Total line count is 1# 使用重定向[root@localhost test]# cat readtest.sh#!/bin/bashcount=0while read linedo
 let count=$count+1 echo "Current line $count - $line "done < ~/test/test.txtecho "Total line count is $count"[root@localhost test]# bash readtest.shCurrent line 1 - this is test text.
Current line 2 - this is second line.
Current line 3 - Hello world
Current line 4 - C# MainCurrent line 5 - Python
Total line count is 5

原文出处:https://www.cnblogs.com/surpassme/p/9576464.html

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消