Skip to content

Commit 7cf2b2e

Browse files
committed
fix(skip-exist): messages have template sub cmd; use __SKIPFILE
1 parent 58b4f6b commit 7cf2b2e

2 files changed

Lines changed: 15 additions & 14 deletions

File tree

skip-exist

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,39 @@ REDO=${REDO:-0} # default: skip if exists. REDO !=0 means overwrite
1111

1212
usage(){
1313
[ $# -gt 0 ] && echo "$*"
14+
local c=$(basename $0)
1415
cat <<HERE
1516
USAGE:
16-
$0 'path/file/to/check' cmd
17+
$c 'path/file/to/check' cmd
1718
18-
$0 sub-1/mri/aseg.nii.gz recon-all -s sub-1
19-
$0 sub-1/mri/aseg.nii.gz dryrun recon-all -s sub-1
20-
REDO=1 $0 sub-1/mri/aseg.nii.gz recon-all -s sub-1
19+
$c sub-1/mri/aseg.nii.gz recon-all -s sub-1
20+
$c sub-1/mri/aseg.nii.gz dryrun recon-all -s sub-1
21+
REDO=1 $c sub-1/mri/aseg.nii.gz recon-all -s sub-1
2122
22-
$0 /tmp/date.txt sh -c "date > _SKIPFILE"
23+
$c /tmp/date.txt sh -c "date > __SKIPFILE"
2324
2425
SYNOPSIS:
2526
'test -s' wrapper with REDO and verbose messaging support
2627
27-
_SKIPFILE in cmd will be replaced by first argument
28+
__SKIPFILE in cmd will be replaced by first argument
2829
2930
ENVIRONMENT:
3031
REDO=1 - overwrite (redo) even if output file exists
3132
3233
NOTES:
3334
Watch out for redirection. DONT DO THIS
34-
$0 /tmp/date.txt date > /tmp/date.txt # BAD BAD BAD
35+
$c /tmp/date.txt date > /tmp/date.txt # BAD BAD BAD
3536
3637
Instead, use an exported function
3738
3839
writedate() { date > \$1; }; export -f writedate
39-
$0 /tmp/date.txt writedate /tmp/date.txt
40+
$c /tmp/date.txt writedate /tmp/date.txt
4041
4142
or a dedicated shell
4243
43-
$0 /tmp/date.txt sh -c 'date > /tmp/date.txt'
44+
$c /tmp/date.txt sh -c 'date > /tmp/date.txt'
4445
45-
But maybe '$0' is overkill for your usage. Consider:
46+
But maybe '$c' is overkill for your usage. Consider:
4647
4748
test -s /tmp/date.txt || date > \$_
4849
@@ -60,10 +61,10 @@ outfile="${1:?need two args. first is outfile}"; shift
6061
usage "ERROR: no command to run if '$outfile' doesn't exist" >&2 &&
6162
exit 1
6263

63-
cmd=("${@//_SKIPFILE/"$outfile"}")
64+
cmd=("${@//__SKIPFILE/"$outfile"}")
6465
if [ -s "$outfile" ]; then
6566
[ "$REDO" == 0 ] &&
66-
echo "# SKIP: Already have '$outfile', not running '$*'; set REDO=1 to overwrite." >&2 &&
67+
echo "# SKIP: Already have '$outfile', not running '${cmd[*]}'; set REDO=1 to overwrite." >&2 &&
6768
exit 0
6869
echo " # Already have '$outfile' but overwritting: REDO=$REDO." >&2
6970
fi
@@ -76,6 +77,6 @@ cmd_status=$?
7677

7778
# if there is an error (cmd_status > 0), should we still warn?
7879
test $cmd_status -eq 0 -a -s "$outfile" ||
79-
echo "# WARNING: '$outfile' DNE or is empty after '$*'" >&2
80+
echo "# WARNING: '$outfile' DNE or is empty after '${cmd[*]}'" >&2
8081

8182
exit $cmd_status

t/skip-exist.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ test_skipexist_space() { # @test
6565

6666
test_skipexist_SKIPFILE() { # @test
6767
o=$BATS_TEST_TMPDIR/"a b"
68-
run skip-exist "$o" writedate _SKIPFILE
68+
run skip-exist "$o" writedate __SKIPFILE
6969
test -s "$o"
7070
dt=$(cat "$o")
7171
run skip-exist "$o" writedate "$o"

0 commit comments

Comments
 (0)