本篇内容介绍了“shell的read命令怎么用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

1、read基本读取

1#!/bin/bash2#testingthereadcommand34echo-n"Enteryouname:"#echo-n让用户直接在后面输入5readname#输入的多个文本将保存在一个变量中6echo"Hello$name,welcometomyprogram."执行:#./read.shEnteryouname:wangtaoHellowangtao,welcometomyprogram.2、read -p (直接在read命令行指定提示符)

1#!/bin/bash2#testingtheread-poption3read-p"Pleaseenteryourage:"age4days=$[$age*365]5echo"Thatmakesyouover$daysdaysold!"执行:#./age.shPleaseenteryourage:23Thatmakesyouover8395daysold!3、read -p (指定多个变量)

1#!/bin/bash2#enteringmultiplevariables34read-p"Enteryourname:"firstlast5echo"Checkingdatafor$last,$first"执行:#./read1.shEnteryourname:abCheckingdataforb,a4、read 命令中不指定变量,那么read命名将它收到的任何数据都放在特殊环境变量REPLY中

1#!/bin/bash2#testingtheREPLYenvironmentvariable34read-p"Enteranumber:"5factorial=16for((count=1;count$REPLY;count++))7do8factorial=$[$factorial*$count]#等号两端不要有空格9done10echo"Thefactorialof$REPLYis$factorial"执行:./read2.shEnteranumber:6Thefactorialof6is7205、超时, 等待输入的秒数(read -t)

1#!/bin/bash2#timingthedataentry34ifread-t5-p"Pleaseenteryourname:"name#记得加-p参数,直接在read命令行指定提示符5then6echo"Hello$name,welcometomyscript"7else8echo9echo"Sorry,tooslow!"10fi执行:#./read3.shPleaseenteryourname:Sorry,tooslow!#./read3.shPleaseenteryourname:wangHellowang,welcometomyscript6、read命令对输入的字符判断

1#!/bin/bash2#gettingjustonecharacterofinput34read-n1-p"Doyouwanttocontinue[Y/N]?"answer5case$answerin6Y|y)echo7echo"fine,continueon...";;8N|n)echo9echo"OK,goodbye"10exit;;11esac执行:#./read4.shDoyouwanttocontinue[Y/N]?yfine,continueon..../read4.shDoyouwanttocontinue[Y/N]?nOK,goodbye7、隐藏方式读取(read -s)

1#!/bin/bash2#hidinginputdatafromthemonitor34read-s-p"Enteryourpasswd:"pass#-s参数使得read读入的字符隐藏5echo6echo"Isyourpasswdreadlly$pass?"~执行:#./read5.shEnteryourpasswd:Isyourpasswdreadllyosfile@206?8、从文本中读取

1#!/bin/bash2#readingdatafromafile34count=15cattest|whilereadline6do7echo"Line$count:$line"8count=$[$count+1]9done10echo"Finishedprocessingthefile"执行:./read6.shLine1:Thequickbrowndogjumpsoverthelazyfox.Line2:Thisisatest,thisisonlyatest.Line3:ORomeo,Romeo!WhereforeartthouRomeo?Finishedprocessingthefile

“shell的read命令怎么用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!