How to use functions in bash? [duplicate] How to use functions in bash? [duplicate] unix unix

How to use functions in bash? [duplicate]


Awk only:

$ awk '{ c="echo " $1 "|openssl base64 -A"         c | getline r         print r }' fileZmI3ZTBhNDQwOGU0NmZkNTU3M2ZmYjllNzNhZWMwMjFhOWRjZjQyNjIzNWMwY2NmYzM3ZDJmNWUwOWE2OGEyMwo=MjM3ZTBhNDQwOGU0NmZlMzU3M2YyMzllNzNhZWMwMjFhOWRjZjQyNjIzNWMwMjNmYzM3ZDJmNWUwOWE2OGExMgo=

For the tight one-liner version see @123's comment below.

... and @EdMorton's super-tight (read: super-proof) version.


Because you especially asking for how to use functions, I divided the problem to several small functions. It is a good practice in all (bigger) bash programs.

The basic rule is: functions behaves like any other commands:

  • you can redirect their input/output
  • you can call them with arguments
  • and like.

The best functions are like common unix executables, e.g. reads from stdin and prints to stdout. This allows you use them in pipelines too.

So, now the rewrite:

# function for create base64 - reads from stdin, writes to stdoutbase64Encode() {        openssl base64 -A}# function for dealing with your file# e.g. reads lines "hash path" and prints "base64 path"convert_hashes() {        while read -r hash path; do                b64=$(base64Encode <<< "$hash")                echo "$b64 $path"        done}#the "main" programconvert_hashes < your_file.txt

output

ZmI3ZTBhNDQwOGU0NmZkNTU3M2ZmYjllNzNhZWMwMjFhOWRjZjQyNjIzNWMwY2NmYzM3ZDJmNWUwOWE2OGEyMwo= /path/to/some/fileMjM3ZTBhNDQwOGU0NmZlMzU3M2YyMzllNzNhZWMwMjFhOWRjZjQyNjIzNWMwMjNmYzM3ZDJmNWUwOWE2OGExMgo= /path/to/another/file

Yes, i know, i want only the base64 without the attached path. Ot course, you can modify the above convert_hashes and remove the path from the output, e.g. instead of the echo "$b64 $path" you could use the echo "$b64" and the output will be just the b64 string only - but youre loosing information in the function - which string belongs to which path - imho, not the best practice.

Therefore, you can leave the function as-is, and use another tool, for getting the first column - and only when needed - e.g. in the "main" program. This way you have designed a function for later more universal way.

convert_hashes < your_file.txt | cut -d ' ' -f1

output

ZmI3ZTBhNDQwOGU0NmZkNTU3M2ZmYjllNzNhZWMwMjFhOWRjZjQyNjIzNWMwY2NmYzM3ZDJmNWUwOWE2OGEyMwo=MjM3ZTBhNDQwOGU0NmZlMzU3M2YyMzllNzNhZWMwMjFhOWRjZjQyNjIzNWMwMjNmYzM3ZDJmNWUwOWE2OGExMgo=

Now imagine, that you extending the script, and want not use files, but the input is coming from another program: Let simulate this with the following get_data function (of course, in the real app it will do something other, not just cat:

get_data() {cat <<EOFfb7e0a4408e46fd5573ffb9e73aec021a9dcf426235c0ccfc37d2f5e09a68a23 /path/to/some/file237e0a4408e46fe3573f239e73aec021a9dcf426235c023fc37d2f5e09a68a12 /path/to/another/fileEOF}

now you can use the all above as:

get_data | convert_hashes

the output will be the same as above.

of course, you can do something with the output too, let say

get_data | convert_hashes | grep another/file | cut -d ' ' -f1MjM3ZTBhNDQwOGU0NmZlMzU3M2YyMzllNzNhZWMwMjFhOWRjZjQyNjIzNWMwMjNmYzM3ZDJmNWUwOWE2OGExMgo=

Of course, if you have such "modular" structure, you can easily replace any parts, without need touch the other parts, let say going to replace the openssl with the base64 command.

base64Encode() {        base64}

And everything will continue work, without any other changes. Of course, in real app is (probably) pointless to have function which calls only one program - but I especially doing this because you asked about the functions.

Otherwise, the above could be done in simple:

while read -r hash path; do    openssl base64 -A <<<"$hash"    echo    #or echo $(openssl base64 -A <<<"$hash")    #or printf "%s\n" $(openssl base64 -A <<<"$hash")done < your_file.txt

or even

cut -d ' ' -f1 base  | xargs -I% -n1 bash -c 'echo $(openssl base64 -A <<<"%")'

You need the echo or print because the openssl doesn't prints newlines by default. Output:

ZmI3ZTBhNDQwOGU0NmZkNTU3M2ZmYjllNzNhZWMwMjFhOWRjZjQyNjIzNWMwY2NmYzM3ZDJmNWUwOWE2OGEyMwo=MjM3ZTBhNDQwOGU0NmZlMzU3M2YyMzllNzNhZWMwMjFhOWRjZjQyNjIzNWMwMjNmYzM3ZDJmNWUwOWE2OGExMgo=

Ps: to be honest, i do not understand why do you need base64 encode some already encoded hash - but YMMV. :)