首页 > 代码库 > shell之read的使用

shell之read的使用

    read命令用于接收键盘的标准输入以及其它文件的输入,得到输入后,read命令会将数据放到一个标准变量中。

示例 1:

#!/bin/bash

read -s -p  "Enter you password: " password            #-s:不显示输入,-p:在read命令行指

echo "Hello,your password is $password"                定一个提示


示例 2:

#!/bin/bash

echo -n "Enter your name: "

read name                                              #从键盘输入

echo "Hello! $name"


#!/bin/bash

read -t 5 -p "Enter your name: " name                  #-t:设定时间限制

echo "This is $name"


示例 3:读行

#!/bin/bash

file=/etc/fstab

i=1

cat $file | while read line

do

  echo $i###$line

  ((i++))

done


本文出自 “一万年太久,只争朝夕” 博客,请务必保留此出处http://zengwj1949.blog.51cto.com/10747365/1924434

shell之read的使用