当前位置:   article > 正文

Shell中Read用法简介

shell read id
一.  read读取单个词. $read name
$tony ma
$echo $name
tony ma

$read id passwd
$allen passwd
$echo $id
$allen
$echo $passwd
$passwd

$read id passwd
$allen kaka gogo
$echo $id
$allen
$echo $passwd
$kaka gogo 二.  read读取整行.      利用while循环来使read读取整行。      以下是代码清单: 1  #!/bin/bash
2  lineno=13  cat $* |4  while read -r line
5  do
6          echo "$lineno: $line"
7          lineno=$((lineno + 1))
8  done 三.   read的嵌套.       1)while外层的read依次读取的是文件pbook中的每一行,而内层的read是从console读取的内容。      2)要实现while read的嵌套,可以使用exec命令,将I/O重定向。        注意exec的用法,以及括号的使用。  1  #!/bin/bash
2  echo -n "Enter name to be removed: "
3  read name
4  p=$(grep "$name " phonebook | wc -l)
5  echo $p
6  if [ "$p" -gt 1 ]
7  then
8          echo "More than one match ; Please select the one to remove;"
9          grep  "$name " phonebook > pbook
10          cat pbook | while read line
11          do (
12                echo -n  "$line  Remove(y/n)?"
13                exec < /dev/tty
14                read answer
15                if [ "$answer" = y ]
16                then
17                          grep -v "^$line$" phonebook > /tmp/phonebook.tmp
18                          mv /tmp/phonebook.tmp phonebook
19                else
20                          continue
21                fi22             )
23          done
24  else
25          grep -v "^$line$" phonebook > /tmp/phonebook.tmp
26          mv /tmp/phonebook.tmp phonebook27  fi28  cat phonebook

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/11411056/viewspace-733472/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/11411056/viewspace-733472/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/308746
推荐阅读
相关标签
  

闽ICP备14008679号