zoukankan      html  css  js  c++  java
  • Shell 示例:将指定的文件内容转换为大写

    程序代码如下:

    #!/bin/bash
    
    # 将一个指定的输入文件内容转换为大写
    
    E_FILE_ACCESS=70
    E_WRONG_ARGS=71
    
    if [ ! -r "$1" ] # 判断指定的输入文件是否可读
    then
    echo "Can't read from input file!"
    echo "Usage: $0 input-file output-file"
    exit $E_FILE_ACCESS
    fi
    
    if [ -z "$2" ] # 确保是有效的输出文件名
    then
    echo "Need to specify output file."
    echo "Usage: $0 input-file output-file"
    exit $E_WRONG_ARGS
    fi
    
    exec 4<&0 # 将文件描述符与 stdin 联系起来,保存了 stdin
    exec < $1 # 将会从输入文件中读取
    
    exec 7>&1 # 将文件描述符与 stdout 联系起来,保存了 stdout
    exec > $2 # 将写到输出文件中
    
    # -----------------------------------------------
    cat - | tr a-z A-Z # 转换为大写
    # 从 stdin 中读取然后写到 stdout 上,stdin 和 stdout 都被重定向了
    # -----------------------------------------------
    
    exec 1>&7 7>&- # 恢复 stdout.
    exec 0<&4 4<&- # 恢复 stdin.
    
    # 恢复之后, 下边这行代码将会如期望的一样打印到 stdout 上
    echo "File "$1" written to "$2" as uppercase conversion."
    
    exit 0
    

    用 tr 命令实现了小写到大写的转换。

    关闭文件描述符的方法

    n<&- 关闭输入文件描述符 n.
    0<&-, <&- 关闭 stdin.
    n>&- 关闭输出文件描述符 n.
    1>&-, >&- 关闭 stdout
    
  • 相关阅读:
    页眉插入图片,文字和页号(码)的设置
    MIT_JOS_Lab5
    MIT_JOS_Lab4_PartB_and_PartC
    MIT_JOS_Lab4_PartA
    Monte Carlo Integration
    A strategy to quantify embedding layer
    From DFA to KMP algorithm
    A problem of dimension in Vector Space and It's nullspace
    Pytorch 模型的存储与加载
    Jensen's inequality 及其应用
  • 原文地址:https://www.cnblogs.com/GyForever1004/p/8503549.html
Copyright © 2011-2022 走看看