zoukankan      html  css  js  c++  java
  • 代码块重定向

    象while, until, 和for循环代码块, 甚至if/then测试结构的代码块, 都可以对stdin进行重定向. 即使函数也可以使用这种重定向方式(请参考例子 23-11). 要想做到这些, 都要依靠代码块结尾的<操作符.

    while循环的重定向
    while [ "$name" != Smith ] # 为什么变量$name要用引号?
    do
    read name # 从$Filename文件中读取输入, 而不是在stdin中读取输入. 
     echo $name
    let "count += 1"
    done <"$Filename" # 重定向stdin到文件$Filename.
    重定向while循环的另一种形式
    exec 3<&0 # 将stdin保存到文件描述符3. 
    exec 0<"$Filename" # 重定向标准输入. 
    count=0
    while [ "$name" != Smith ]
    do
    read name # 从stdin(现在已经是$Filename了)中读取. 
    echo $name
    let "count += 1"
    done #  从文件$Filename中循环读取
    
    重定向for循环
    for name in `seq $line_count` # "seq"打印出数字序列.  # while [ "$name" != Smith ] --  比"while"循环更复杂--
    do
    read name # 从$Filename中, 而非从stdin中读取. 
    echo $name
    if [ "$name" = Smith ] # 因为用for循环, 所以需要这个多余测试. 
    then
    break
     fi 
    done <"$Filename"
    
    重定向for循环(stdin和stdout都进行重定向)
    for name in `seq $line_count`
     do
     read name
     echo "$name"
     if [ "$name" = "$FinalName" ]
     then
     break
     fi 
     done < "$Filename" > "$Savefile" # 重定向stdin到文件$Filename,  #   并且将它保存到备份文件中.
  • 相关阅读:
    [51nod] 1301 集合异或和
    [BZOJ] 1088: [SCOI2005]扫雷Mine
    [LUOGU] P4251 [SCOI2015]小凸玩矩阵
    8.21模拟赛
    [BZOJ] 3163: [Heoi2013]Eden的新背包问题
    [BZOJ] 1001: [BeiJing2006]狼抓兔子
    【NOIP2017提高A组冲刺11.8】好文章
    [BZOJ] 1520: [POI2006]Szk-Schools
    [BZOJ] 1877: [SDOI2009]晨跑
    day23(事务管理)
  • 原文地址:https://www.cnblogs.com/fly-xiang-zhao/p/4018989.html
Copyright © 2011-2022 走看看