linux shell读取用户输入

来源:互联网 发布:大明1937 知乎 编辑:程序博客网 时间:2024/05/16 06:18

变量

1

[hadoop@master test]$ name=yunix[hadoop@master test]$ echo $nameyunix 

2

[hadoop@master test]$ name="yang yuan"[hadoop@master test]$ echo $nameyang yuan 

3

[hadoop@master test]$ name=yang yuan-bash: yuan: command not found

备注:字符串中含有空格,需在字符串外加双引号

 

4

定义全局变量

export name=yunix
 

read命令

read命令是一个内置命令,用于从终端或文件读取输入。read命令读取一个输入行,直至遇到换行符,

行尾的换行符在读入时将被转换成一个空字符。如果read命令后未跟变量名,读入的行将被赋值给

内置变量REPLY。如果带-r,read命令将忽略反斜杠/换行符对,而把反斜杠作为行的一部分。

read命令有4个控制选项:-a,-e,-p,-r

 

1

read answer:从标准输入读取一行并赋值给变量answer

example:

[hadoop@master test]$ read answerhello world[hadoop@master test]$ echo $answerhello world

2

read:标准输入读取一行并赋值给内置变量REPLY

example:

[hadoop@master test]$ readhello world[hadoop@master test]$ echo $REPLYhello world

3

read first last:从标准输入读取一行,直至遇到第一个空白符或换行符。把用户键入的第一个词存到first中,

把该行的剩余部分保存到变量last中

example:

[hadoop@master test]$ read first lasthello world my name[hadoop@master test]$ echo $firsthello[hadoop@master test]$ echo $lastworld my name 

4

read -a array:读入一组词,依次赋值给数组array

example:

[hadoop@master test]$ read -a arrayhello world i ok[hadoop@master test]$ echo $arrayhello[hadoop@master test]$ echo ${array[1]}world 

5

read -p prompt:打印提示符,等待输入,并将输入赋值给REPLY变量

example:

[hadoop@master test]$ read -p "enter you name:"enter you name:yunix[hadoop@master test]$ echo $REPLYyunix 

6

read -r name:指定读取命令把一个\(反斜杠)处理为输入行的一部分,而不是把它作为一个控制符

example:

[hadoop@master test]$ read name\yunix[hadoop@master test]$ echo $nameyunix[hadoop@master test]$ read -r name\yunix[hadoop@master test]$ echo $name\yunix

原创粉丝点击