yokie
yokie

Reputation: 153

redirect the result of a program

There is a program that I run with command line. The output is a file. I have to run the program with various parameters so I always have to change the output filename (otherwise it will always be the same and the older will automatically be deleted) and run the program again and again. I tried :

./program param1 param2  > result1.txt

but not surprisingly

cat result1.txt

run the program. I need a command line that will automatically rename the output file at the end of the program. I can not change the program code.

Thanks

Upvotes: 2

Views: 48

Answers (2)

ColOfAbRiX
ColOfAbRiX

Reputation: 1059

You can enclose your line in another script that does something like:

PARAM_1="$1"
PARAM_2="$2"

CMD="./program"

$CMD $PARAM_1 $PARAM_2 > "result-${PARAM_1}-${PARAM_2}"

The scripts calls your command and redirects the output to a filename with a name that depends on the input parameters

This works with 2 parameters, but it can be easily generalised

UPDATE: I just though of a different version that uses MD5 for the output filename, so that it will be consistent even with long, messy parameters and it's also valid for any number of params:

#!/bin/bash

HASH="$(echo "$@" | md5sum | cut -f1 -d' ')"
CMD="./program"

"$CMD" "$@" > "result-$HASH.txt"

Upvotes: 4

anubhava
anubhava

Reputation: 785991

Just rename the output filename using nanosecond date value as:

mv result.txt "result-$(date --rfc-3339=ns).txt"

at the end of your script.

Upvotes: 2

Related Questions