Skip to content

Commit 34a91d4

Browse files
committed
feat(skip-exist): add -o for writing to stdout
1 parent fe2ae02 commit 34a91d4

1 file changed

Lines changed: 30 additions & 7 deletions

File tree

skip-exist

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ usage(){
1313
local c=$(basename $0)
1414
cat <<HERE
1515
USAGE:
16-
$c 'path/file/to/check' cmd
16+
$c [-o] 'path/ouput/file/to/check' cmd-to-run
17+
18+
EXAMPLES:
19+
$c -o /tmp/date.txt date
1720
1821
$c sub-1/mri/aseg.nii.gz recon-all -s sub-1
19-
$c sub-1/mri/aseg.nii.gz dryrun recon-all -s sub-1
20-
REDO=1 $c sub-1/mri/aseg.nii.gz recon-all -s sub-1
22+
DRYRUN=1 $c sub-1/mri/aseg.nii.gz recon-all -s sub-1
23+
REDO=1 $c sub-1/mri/aseg.nii.gz recon-all -s sub-1
2124
2225
$c /tmp/date.txt sh -c "date > __SKIPFILE"
2326
@@ -28,13 +31,18 @@ SYNOPSIS:
2831
2932
ENVIRONMENT:
3033
REDO=1 - overwrite (redo) even if output file exists
31-
SKIP_QUIET - dont echo when skipping
34+
DRYRUN=1 - only show what would be run
35+
SKIP_QUIET - dont report when skipping
3236
3337
NOTES:
3438
Watch out for redirection. DONT DO THIS
3539
$c /tmp/date.txt date > /tmp/date.txt # BAD BAD BAD
3640
37-
Instead, use an exported function
41+
Instead, use the -o or --output, like
42+
$c -o /tmp/date.txt date
43+
44+
45+
Alternatively, use an exported function
3846
3947
writedate() { date > \$1; }; export -f writedate
4048
$c /tmp/date.txt writedate /tmp/date.txt
@@ -55,10 +63,21 @@ HERE
5563

5664
[[ $# -eq 0 || "${1:-}" =~ ^-+h(help)?$ ]] && usage && exit 0
5765

66+
# check for argumetns
67+
write_stdout=0
68+
while [ $# -gt 0 ]; do
69+
case $1 in
70+
-o|--output) write_stdout=1; shift;;
71+
-*) echo "ERROR: unknown option $1. if '$1' is a file, use './$1''"; exit 1;;
72+
*) break;; # not an input argument to parse. skip to next
73+
esac
74+
done
75+
76+
5877
outfile="${1:?need two args. first is outfile}"; shift
5978

6079
[ $# -eq 0 ] &&
61-
usage "ERROR: no command to run if '$outfile' doesn't exist" >&2 &&
80+
usage "ERROR: not given a command to run if '$outfile' doesn't exist" >&2 &&
6281
exit 1
6382

6483
cmd=("${@//__SKIPFILE/"$outfile"}")
@@ -70,7 +89,11 @@ if [ -s "$outfile" ]; then
7089
fi
7190

7291
# run the thing that should make the input
73-
dryrun "${cmd[@]}"
92+
if [ $write_stdout -eq 0 ]; then
93+
dryrun "${cmd[@]}"
94+
else
95+
dryrun "${cmd[@]}" | drytee "$outfile"
96+
fi
7497

7598
# check that what we did worked
7699
cmd_status=$?

0 commit comments

Comments
 (0)