简体   繁体   English

接收文件名作为参数或使用标准输入的 bash 函数

[英]bash function that either receives file name as argument or uses standard input

I want to write some wrappers around the sha1sum function in bash.我想围绕 bash 中的sha1sum函数编写一些包装器。 From the manpage:从联机帮助页:

SHA1SUM(1)                                          User Commands                                          SHA1SUM(1)

NAME
       sha1sum - compute and check SHA1 message digest

SYNOPSIS
       sha1sum [OPTION]... [FILE]...

DESCRIPTION
       Print or check SHA1 (160-bit) checksums.

       With no FILE, or when FILE is -, read standard input.

How can I set up my wrapper so that it works in the same way?我如何设置我的包装器以使其以相同的方式工作? Ie: IE:

my_wrapper(){
  # some code here
}

that could work both as:这可以作为:

my_wrapper PATH_TO_FILE

and

echo -n "blabla" | my_wrapper

I think this is somehow related to Redirect standard input dynamically in a bash script but not sure how to make it 'nicely'.我认为这与在 bash 脚本中动态重定向标准输入有某种关系,但不确定如何使它“很好”。

Edit 1编辑 1

I program in a quite defensive way, so I use in my whole script:我以一种非常防御性的方式编程,所以我在我的整个脚本中使用:

# exit if a command fails
set -o errexit
# make sure to show the error code of the first failing command
set -o pipefail
# do not overwrite files too easily
set -o noclobber
# exit if try to use undefined variable
set -o nounset

Anything that works with that?有什么可以与之配合的吗?

You can use this simple wrapper:您可以使用这个简单的包装器:

args=("$@")         # save arguments into an array
set -o noclobber nounset pipefail errexit
set -- "${args[@]}" # set positional arguments from array

my_wrapper() {
   [[ -f $1 ]] && SHA1SUM "$1" || SHA1SUM
}

my_wrapper "$@"

Note that you can use:请注意,您可以使用:

my_wrapper PATH_TO_FILE

or:或者:

echo -n "blabla" | my_wrapper

This code works for me, put it in a file named wrapper这段代码对我有用,把它放在一个名为 wrapper 的文件中

#!/bin/bash


my_wrapper(){
    if [[ -z "$1" ]];then
        read PARAM
    else
        PARAM="$1"
    fi

    echo "PARAM:$PARAM"
}

Load the function in your environment在您的环境中加载函数

. ./wrapper

Test the function with input pipe使用输入管道测试功能

root@51ce582167d0:~# echo hello | my_wrapper
PARAM:hello

Test the function with parameter用参数测试函数

root@51ce582167d0:~# my_wrapper bybye
PARAM:bybye

Ok, so the answers posted here are fine often, but in my case with defensive programming options:好的,所以这里发布的答案通常很好,但在我的情况下有防御性编程选项:

# exit if a command fails
set -o errexit
# exit if try to use undefined variable
set -o nounset

things do not work as well.事情也不行。 So I am now using something in this kind:所以我现在正在使用这种东西:

digest_function(){
    # argument is either filename or read from std input
    # similar to the sha*sum functions

    if [[ "$#" = "1" ]]
    then
        # this needs to be a file that exists
        if [ ! -f $1 ]
        then
            echo "File not found! Aborting..."
            exit 1
        else
            local ARGTYPE="Filename"
            local PARAM="$1"
        fi
    else
        local ARGTYPE="StdInput"
        local PARAM=$(cat)
    fi

    if [[ "${ARGTYPE}" = "Filename" ]]
    then
        local DIGEST=$(sha1sum ${PARAM})

    else
        local DIGEST=$(echo -n ${PARAM} | sha1sum)
    fi
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM