forked from gvtulder/ccrbm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-with-output.sh
executable file
·45 lines (38 loc) · 941 Bytes
/
run-with-output.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/bash
# Runs a command and writes the output to a file, but only if this file
# does not already exist.
#
# Usage:
# ./run-with-output.sh output_file command <argument 1> ...
#
# This script will first write the output to a temporary file. If the
# command is successful, the temporary file is moved to the destination.
#
# Trailing arguments are passed to the command.
#
# Copyright (c) 2016 Gijs van Tulder / Erasmus MC, the Netherlands
# This code is licensed under the MIT license. See LICENSE for details.
set -e
output_file=$1
shift
mkdir -p $( dirname $output_file )
if [ -f $output_file ] ; then
echo "Already exists: $output_file"
exit
fi
if [[ -z $TMPDIR ]] ; then
tmp_file=/tmp/output-eval-$$
else
tmp_file=$TMPDIR/output-eval-$$
fi
rm -rf $tmp_file
echo $@ >> $tmp_file
$@ >> $tmp_file
result=$?
if [[ $result == 0 ]] ; then
mv $tmp_file $output_file
exit $result
else
rm $tmp_file
exit $result
fi