From 7effa12c59964915b9ded16415f428696f1c0dbe Mon Sep 17 00:00:00 2001 From: bth Date: Sun, 30 Jun 2024 15:44:36 -0700 Subject: [PATCH] um rename to handle piped input --- shell/rename.sh | 25 ++++++++++++++++++++----- shell/test/um.bats | 15 +++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/shell/rename.sh b/shell/rename.sh index 419f42c..8faa2c0 100755 --- a/shell/rename.sh +++ b/shell/rename.sh @@ -2,16 +2,31 @@ # Usage: -# > rename 100.md bar +# > um rename 100.md foo # or -# > rename 100.foo.md bar +# > um rename 100.foo.md bar +# or +# > um last | um rename baz # is equivalent to: -# > mv 100.md 100.bar.md +# > mv 100.md 100.foo.md # > mv 100.foo.md 100.bar.md +# > mv 100.bar.md 100.baz.md + +# accept piped input +# https://unix.stackexchange.com/a/537581 +if [[ ! -t 0 ]]; then + FROM=$(cat -) + TO=$1 +else + FROM=$1 + TO=$2 +fi + +echo from: $FROM to: $TO # split the args by a newline so that awk reads them as separate "records" -mvcmd=`echo "$1 -$2" | awk -f $UMBASEPATH/rename.awk` +mvcmd=`echo "$FROM +$TO" | awk -f $UMBASEPATH/rename.awk` mv $mvcmd diff --git a/shell/test/um.bats b/shell/test/um.bats index a3159b7..84c1f7d 100644 --- a/shell/test/um.bats +++ b/shell/test/um.bats @@ -89,6 +89,21 @@ um_next_awk() { + foo' } +@test "um rename" { + run um rename 001.md blah + run ls 001* + assert_output '001.blah.md' + mv 001.blah.md 001.md +} + +# this doesn't work because bats doesn't handle stdin the way a tty does. +# @test "um rename piped" { +# run um last | um rename gloo +# run ls 099* +# assert_output '099.gloo.md' +# mv 099.gloo.md 099.poo.md +# } + um_rename_awk() { echo "$1 $2" | awk -f $DIR/../rename.awk