@@ -11,38 +11,39 @@ REDO=${REDO:-0} # default: skip if exists. REDO !=0 means overwrite
1111
1212usage (){
1313 [ $# -gt 0 ] && echo " $* "
14+ local c=$( basename $0 )
1415 cat << HERE
1516USAGE:
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
2425SYNOPSIS:
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
2930ENVIRONMENT:
3031 REDO=1 - overwrite (redo) even if output file exists
3132
3233NOTES:
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 " } " )
6465if [ -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
6970fi
@@ -76,6 +77,6 @@ cmd_status=$?
7677
7778# if there is an error (cmd_status > 0), should we still warn?
7879test $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
8182exit $cmd_status
0 commit comments