beets
beets copied to clipboard
Script bash
I'm trying to make some bash scripts to perform some operations with beets.
But with this I can't find a way to make it work and I don't really understand why, if anyone has any more knowledge....
This is the script, just a test to see how it works:
#!/bin/bash
set -e
repeats=1
clear=false
while (( $# >= 1 ))
do
key="$1"
case $key in
-a)
genre="$2"
shift # past argument
;;
-c)
exgenre="$2"
shift # past argument
;;
*)
# unknown option
;;
esac
shift # past argument or value
done
if [ ${#exgenre} -eq 0 ]; then
exgenre=
else
exgenre="^genre:$(echo \"$exgenre | sed 's/,/\" \^genre:\"/g' | sed 's/$/"/')"
fi
echo "variable:"
echo $exgenre
echo
#beet test
echo "command with variable:"
beet ls -f '$path' genre:"$genre" $exgenre | wc -l
echo
echo "command normal:"
beet ls -f '$path' genre:Jazz ^genre:"Art Rock" ^genre:"Rock" | wc -l
Running the script in this way should give me the same result for the two 'beets' commands, instead it gives two different results, and I don't really understand why...
./test.sh -a Jazz -c "Art Rock","Rock"
this is the result:
variable:
^genre:"Art Rock" ^genre:"Rock"
command with variable:
20
command normal:
17339
they should both be 17339
I do not understand how the variable is interpreted by beets
"Splicing" shell commands using shell variables like this is notoriously difficult! Especially when the arguments you're trying to expand include shell-escape characters like ". You would like the value of $exgenre, which is ^genre:"Art Rock" ^genre:"Rock", to be passed as two arguments to beets. However, the shell doesn't split up strings like that when they come from variables: it's split only on whitespace, as controlled by the IFS variable. So it will pass three arguments to beets.
You can see this behavior even without beets, as in this small shell script:
#!/bin/sh
exgenre='^genre:"Art Rock" ^genre:"Rock"'
sh -c 'echo $#' -- $exgenre
sh -c 'echo $#' -- ^genre:"Art Rock" ^genre:"Rock"
sh -c 'echo $#' is a way to count the arguments. This prints 3 and then 2, indicating that the argument grouping is different for the two commands.
Anyway, shell scripting is painful, and I think the best solutions here either escaping with printf or using bash's arrays. Or give up on shell scripting altogether, in favor of something like zx…
Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.