Linux/Unix面试题

当前位置: 面试问题网 > Linux/Unix面试题 > Shell如何接收变量输入

Shell如何接收变量输入

在shell中使用read 命令接收变量输入,
   语法:
   read variable [variable......]
   例:
   $ cat color6
   echo This program prompts for user input
   echo “please enter your favorite two colors -> c”
   read color_a color_b
   echo The colors you entered are: $color_b $color_a
   $ chmod +x color6
   $ color6
   This program prompts for user input
   Please enter your favorite two colors -> red blue
   The colors you entered are: blue red
   $ color6
   This program prompts for user input
   Please enter you favorite two colors -> red blue tan
   The color you enterd are :blue tan red
  
   用户使用命令行参数传递信息进程序,在命令执行之前,用户必须知道正确的语法。有一种情况,你想要在用户执行程序的时候提示他输入这些参数。read命令就是用来在程序执行的时候收集终端键入的信息。
   通常使用echo命令来提供用户一个提示,让用户知道程序正在等待一些输入,同时通知用户应该输入的类型。因此,每一个read命令应该在echo命令前面。
   read命令会给出一个变量名的列表,这些变量会被用户在提示符下输入的词赋值(以空格分隔)。如果read命令定义的变量比输入的词要多,剩余变量会被赋空值。如果用户输入的词要比变量多,剩余的数据会赋给列表中的最后一个变量。
   一旦被赋值,你就可以象其他的shell变量一样存取这些变量。
  
   以下例子提示用户输入要被安装的文件名:
   $ cat my_install3
   echo $0 will install files into your bin directory
   echo “Enter the names of the files -> c”
   read filenames
   mv $filenames $HOME/bin
   echo Instllation is complete
   ctrl + d
   $ chmod +x my_install13
  
   $ my_install13
   my_install13 will install files into your bin directory
   Enter the names of the files -> f1 f2
   Installaton is complete
  
   这个安装会提示用户输入chmod和移动到$HOME/bin的文件名。这个程序给用户更多的关于应该输入数据情况的指引。而不像install2中用户必须在命令行中提供文件名。用户使用程序不需要特殊的语法。程序让用户确切地知道要输入什么。所有的输入的文件名都会被赋值给变量filenames。

【Shell如何接收变量输入】相关文章

1. Shell如何接收变量输入

2. shell程序如何生命变量?shell变量是弱变量吗?

3. ShellScript面试题一则-ShellScript编程

4. shell变量的作用空间是什么

5. static全局变量与普通的全局变量有什么区别?static局部变量和普通局部变量有什么区别?static函数与普通函数有什么区别?

6. shell程序中如何注释

7. 如何在Shell脚本中使用函数

8. Shell脚本如何向终端输出信息

9. 如何执行一个shell程序

10. 几个Shell Script面试题

本文来源:https://www.mianshiwenti.com/a13568.html

点击展开全部

《Shell如何接收变量输入》

将本文的Word文档下载到电脑,方便收藏和打印

推荐程度:

进入下载页面

﹝Shell如何接收变量输入﹞相关内容

「Shell如何接收变量输入」相关专题

其它栏目

也许您还喜欢