简体   繁体   中英

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. 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:

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'.

Edit 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

#!/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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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