ref:http://bash.cyberciti.biz/guide/Getting_User_Input_Via_Keyboard

You can accept input from the keyboard and assign an input value to a user defined shell variable using read command.

read Command Syntax

read -p "Prompt" variable1 variable2 variableN

Where,

  • -p "Prompt" : Display prompt to user without a newline.
  • variable1 : The first input (word) is assigned to the variable1.
  • variable2 : The second input (word) is assigned to the variable2.

Handling Input

Create a script called greet.sh as follows:

#!/bin/bash
read -p "Enter your name : " name
echo "Hi, $name. Let us be friends!"

Save and close the file. Run it as follows:

chmod +x greet.sh
./greet.sh

Sample Outputs:

Enter your name : Vivek Gite
Hi, Vivek Gite. Let us be friends!

Examples

Try the following examples.

Multiple Input (number.sh)

#!/bin/bash
# read three numbers and assigned them to 3 vars
read -p "Enter number one : " n1
read -p "Enter number two : " n2
read -p "Enter number three : " n3
 
# display back 3 numbers - punched by user.
echo "Number1 - $n1"
echo "Number2 - $n2"
echo "Number3 - $n3"

Display Domain Owner Information

A shell script to display the Internet domain name owner information (domain.sh):

#!/bin/bash
read -p "Enter the Internet domain name (e.g. nixcraft.com) : " domain_name
whois $domain_name

Timeout Input

You can time out read command using the -t option. It causes read to time out and return failure if a complete line of input is not read within TIMEOUT seconds. For example, if no input provided within 10 second, program will be aborted (domain2.sh):

#!/bin/bash
read -t 10 -p "Enter the Internet domain name (e.g. nixcraft.com) : " domain_name
whois $domain_name

Handling Passwords

The -s option causes input coming from a terminal do not be displayed on the screen. This is useful for password handling (readpass.sh):

#!/bin/bash
read -s -p "Enter Password  : " my_password
echo
echo "Your password - $my_password"

Handling multiple values

Consider the following example:

read -p "Enter directory to delete : " dirname
echo "$dirname"

Sample outputs:

Enter directory to delete : foo bar /tmp/data
foo bar /tmp/data

The user supplied three values instead of one. The string is now made of three different fields. All three words are assigned to dirname using $IFS internal field separator. The $IFS determines how shell recognizes fields.

$IFS

To display default value of $IFS, enter:

echo "$IFS"

You will see a whitespace which is nothing but a space, a tab, and a newline (default). You can print actual values of IFS using the following command (see Here strings):

cat -etv <<<"$IFS"

Sample outputs:

 ^I$
$

Where,

  • $ - end of line i.e. newline
  • ^I$ - tab and newline

But how do I use $IFS and read command together?

Create a variable called nameservers and give it total 3 values as follows (note all values are separated by a whitespace):

nameservers="ns1.nixcraft.net ns2.nixcraft.net ns3.nixcraft.net"

Display the value of a variable nameservers with echo command or printf command:

echo "$nameservers"

OR

printf "%s" $nameservers

Now, you can simply split $nameservers using the read command as follows (see Here strings):

read -r ns1 ns2 ns3 <<< "$nameservers"

Where,

  • The read command reads input from $nameservers variable.
  • The default value of $IFS is used to assign values to three separate variables. Your input is broken into tokens using $IFS and assigned to three variables.
  • In other words, the IFS variable worked as token delimiter or separator.
  • The first token (ns1.nixcraft.net) is saved as the value of the first variable ($ns1)
  • The second token (ns2.nixcraft.net) is saved as the value of the second variable ($ns2).
  • The third token (ns3.nixcraft.net) is saved as the value of the third variable ($ns3).
  • To display the value of each variable use echo command or printf command as follows:
echo "DNS Server #1 $ns1"
echo " #2 $ns2"
echo " #3 $ns2"

OR use the printf command

printf "DNS Server #1 %s\n #2 %s\n #3 %s\n" $ns1 $ns2 $ns3

Sample outputs:

DNS Server #1 ns1.nixcraft.net
#2 ns2.nixcraft.net
#3 ns3.nixcraft.net

How do I change the IFS separator value?

Consider the following /etc/passwd line:

gitevivek:x:1002:1002::/home/gitevivek:/bin/sh

Assign the above line to a variable called pwd:

pwd="gitevivek:x:1002:1002::/home/gitevivek:/bin/sh"

Save the Internal Field Separator to a variable called old:

old="$IFS"

Set the Internal Field Separator to a colon (i.e. change the Internal Field Separator):

IFS=:

Read $pwd and generate tokens using $IFS and store them into respective fields:

read -r login password uid gid info home shell <<< "$pwd"
printf "Your login name is %s, uid %d, gid %d, home dir set to %s with %s as login shell\n" $login $uid $gid $home $shell

Sample outputs:

Your login name is gitevivek, uid 1002, gid 1002, home dir set to /home/gitevivek with /bin/sh as login shell

Finally, restore the Internal Field Separator value using $old:

IFS="$old"

Where,

  • : - act as token separator on $pwd i.e. the contents of the IFS variable are used as token delimiters.
  • login - Field # 1 is generated using the first token and is saved as the value of the first variable ($login)
  • password - Field # 2 is generated using the second token and is saved as the value of the second variable ($password)
  • uid - Field # 3 and so on...
  • gid - Field # 4
  • info - Field # 5
  • home - Field # 6
  • shell - Field # 7

linux shell read command-Getting User Input Via Keyboard--ref的更多相关文章

  1. linux shell basic command

    Learning basic Linux commands Command Description $ ls This command is used to check the contents of ...

  2. Linux shell command学习笔记(一)

    Shell的种类有很多种,例如CSH,Bourne Shell,Korn Shell.在现在的大多数Linux发行版中,默认的Shell一般都是Bourne again shell(bash). &l ...

  3. Linux Shell系列教程之(十六) Shell输入输出重定向

    本文是Linux Shell系列教程的第(十六)篇,更多Linux Shell教程请看:Linux Shell系列教程 Shell中的输出和输入的重定向是在使用中经常用到的一个功能,非常实用,今天就为 ...

  4. Linux Shell编程参考大全

    本文记录Linux Shell编程中常用基本知识,方便快速入门以及查询使用. 本文主要分为以下几个部分: 一.Shell中的变量 任何编程语言中,有关变量的定义,作用范围,赋值等都是最最基础的知识. ...

  5. Linux shell基础知识(上)

    Linux shell基础知识(上) 目录 一.shell介绍 二.命令历史 三.命令补全和别名 四.通配符 五.输入输出重定向 六.管道符和作业控制 七.shell变量 八.环境变量配置文件 九.b ...

  6. 常用linux shell脚本记录

    遍历目录下所有的文件是目录还是文件 for file in ./* do if test -f $file then echo $file 是文件 fi if test -d $file then e ...

  7. LINUX SHELL脚本攻略笔记[速查]

    Linux Shell脚本攻略笔记[速查] 资源 shell script run shell script echo printf 环境变量和变量 pgrep shell数学运算 命令状态 文件描述 ...

  8. java使用Runtime.exec()运行windwos dos或linux shell命令

    使用Runtime.exec()运行windwos dos或linux shell命令,按实际情况具体测试     实例代码: package com.bookoo.test.command; imp ...

  9. Linux shell脚本基础学习详细介绍(完整版)二

    详细介绍Linux shell脚本基础学习(五) Linux shell脚本基础前面我们在介绍Linux shell脚本的控制流程时,还有一部分内容没讲就是有关here document的内容这里继续 ...

  10. Linux shell脚本编程(二)

    Linux shell脚本编程(二) 练习:求100以内所有偶数之和; 使用至少三种方法实现; 示例1: #!/bin/bash # declare -i sum=0 #声明一个变量求和,初始值为0 ...

随机推荐

  1. 我的Photoshop第一个作品

    想学Photoshop好久了, 作为一个想成为"将计算机技术和艺术结合起来的人", 不会Photoshop感觉说不过去. 先选了一个图尝试做, 结果发现太难了. 我给背景上色, 画 ...

  2. 启动Activity时显示空白界面的问题

    问题描述: 启动activity时,先显示一个空白的界面,带标题栏的,1秒左右的时间后才显示activity对应 layout上的内容. 解决办法: 将activity的windows设置为透明的就可 ...

  3. VJP1193 扫雷(状压)

    链接 保存当前行和前一行两行的状态 #include <iostream> #include<cstdio> #include<cstring> #include& ...

  4. WPF——绑定数据库数据(Listview)

    一.首先先画一个窗体,放进一个Listview 然后给每列起好名字,并且绑定的数据是临时表的列名 二.造一个临时表用来存储数据,并且将扔进去的Listview绑定到这个临时表DataTable上面 p ...

  5. -_-#【JS 优化】把 JS 放到底部

    一开始为以为Javascript脚本尽量放到页面底部加载,是指所有的JS脚本都要放到底部,后来才发现, 并不完全是这样,这里所指的脚本是指那些在加载过程中要执行的脚本,所以一般的处理办法还是页面头部引 ...

  6. WCF 配置服务 (02)

    配置服务概述 • 在设计和实现服务协定后,即可配置服务. 在其中可以定义和自定义如何向客户端公开服务,包括指定可以找到服务的地址.服务用于发送和接收消息的传输和消息编码,以及服务需要的安全类型. • ...

  7. udhcpc和udhcpd移植

    实现DHCP自动获取IP地址 前提:系统已经实现DNS(即使用ping www.baidu.com测试时能ping通). 1.  在内核中添加以下选项: Networking  ---> [*] ...

  8. Windows中APACHE开启fastcgi后无法连接数据库

    环境:Windows server 2003 x64Apache 2.2.14mod_fcgid-2.2b-w32.zipPHP VC9 x86 Non Thread Safe(用Visual C++ ...

  9. uniq linux下去除重复行命令

    一,uniq干什么用的 文本中的重复行,基本上不是我们所要的,所以就要去除掉.linux下有其他命令可以去除重复行,但是我觉得uniq还是比较方便的一个.使用uniq的时候要注意以下二点 1,对文本操 ...

  10. 修改ruby gem源为ruby.taobao.org

    由于网络原因,导致从rubygems.org下载gem文件较慢或者间歇性的连接失败,所以可以修改gem源为ruby.taobao.org.具体可以用 gem install rails -V 来查看执 ...