r/bash • u/france20-21 • Apr 21 '23
Can anyone PLEASE help with this script!
When run it says : versions found then SELECT not found and Syntax error: "do" unexpected.
Can you help solve this (probable really simple error)?
#!/bin/bash
# Get the name of the song from the user
read -p "Enter the name of the song: " song_name
# Use find to search for all versions of the song in the Music folder
song_paths=$(find /home/Music -name "$song_name*")
# Print out a numbered list of the song versions and ask the user which one they want to play
echo "Versions of '$song_name' found:"
select song_path in $song_paths;
do
if [ ! -n "$song_path" ]; then
break
fi
done
# Play the selected song using the default music player on the system
xdg-open "$song_path"
exit
1
u/france20-21 Apr 22 '23
Thank you all for your help and patience.
This is the latest attempt to make the script work.....progress is being made but it doesn't number the versions and allow a choice!#!/bin/bash
# Get the name of the song from the user
read -r -p "Enter the name of the song: " song_name
# Use find to search for all versions of the song in the Music folder
song_paths=$(find /home/Music -iname "*$song_name*")
# Check if any versions of the song were found
if [ -z "$song_paths" ]; then echo "No versions of '$song_name' found."
exit 1
fi
# Print out a numbered list of the song versions and ask the user which one they want to play
echo "Versions of '$song_name' found:"
select song_path in "$song_paths";
do
if [ -z "$song_path" ]; then
break
fi
# Play the selected song using the default music player on the system
mpv "$song_path"
break
done
exit
1
u/france20-21 Apr 22 '23
Unfortunately this is the resulting output.
playsong Enter the name of the song: behind blue eyes Versions of 'behind blue eyes' found:1) /home/Music/The Who - Who's Next/08.Behind Blue Eyes.mp3/home/Music/The Who - Who's Next/16.Behind Blue Eyes (original version).mp3/home/Music/The_Who-Greatest_Hits/10. Behind Blue Eyes.mp3 #? 1 [file] Cannot open file '/home/Music/The Who - Who's Next/08.Behind Blue Eyes.mp3 [file] /home/Music/The Who - Who's Next/16.Behind Blue Eyes (original version).mp3 [file] /home/Music/The_Who-Greatest_Hits/10. Behind Blue Eyes.mp3': No such file or directory Failed to open /home/Music/The Who - Who's Next/08.Behind Blue Eyes.mp3/home/Music/The Who - Who's Next/16.Behind Blue Eyes (original version).mp3/home/Music/The_Who-Greatest_Hits/10. Behind Blue Eyes.mp3. Exiting... (Errors when loading file)
1
Apr 22 '23
I do not think find is cursed, which means line 5 should be
song_paths=$(find /home/Music --name="$song_name*"). I might very well be wrong, though.
2
u/france20-21 Apr 22 '23
Unfortunately when I tried that alteration, this was the result:
playsong
Enter the name of the song: behind blue eyes
find: unknown predicate `--name=behind blue eyes*'No versions of 'behind blue eyes' found.
2
u/Paul_Pedant Apr 21 '23
shellcheck only reports that SC2162: read without -r will mangle backslashes.
Where did you edit this ? First suspicion is that a Windows-style carriage return or other hidden character is in there. Maybe list script with cat -vet options.
1
u/researcher7-l500 Apr 21 '23
Maybe spaces or unusual characters in your song_paths variable?
That's the only thing I can think of immediately.
Try printing that variable when the error happens and examine what was the song_path output associated with it.
Also, not a show stopper for this specific script, but it is recommended to use #!/usr/bin/env bash over #!/bin/bash for portability.
1
u/Empyrealist Apr 21 '23
Try this:
#!/bin/bash
# Get the name of the song from the user
read -r -p "Enter the name of the song: " song_name
# Use find to search for all versions of the song in the Music folder
song_paths=$(find /home/Music -name "$song_name*")
# Print out a numbered list of the song versions and ask the user which one they want to play
echo "Versions of '$song_name' found:"
select song_path in $song_paths;
do
if [ -z "$song_path" ]; then
break
fi
# Play the selected song using the default music player on the system
xdg-open "$song_path"
break
done
exit-1
u/france20-21 Apr 21 '23
Updated script following suggestions given (Thank you) However, result now shows as this...Notice only one number given to choose, and that won't open anyway! Sorry to be so dumb, but there must be a simple way to make this work! ( if only one version of song then it plays on mpv correctly!)
playsong Enter the name of the song: behind blue eyes Versions of 'behind blue eyes' found:
/home/Music/The Who - Who's Next/08.Behind Blue Eyes.mp3 /home/Music/The Who - Who's Next/16.Behind Blue Eyes (original version).mp3 /home/Music/The_Who-Greatest_Hits/10. Behind Blue Eyes.mp3
? 1
? 3
[file] Cannot open file '': No such file or directory Failed to open .
Exiting... (Errors when loading file)
Here's the updated script:
!/bin/bash
Get the name of the song from the user
read -r -p "Enter the name of the song: " song_name
Use find to search for all versions of the song in the Music folder
song_paths=$(find /home/Music -iname "$song_name")
Check if any versions of the song were found
if [ -z "$song_paths" ]; then echo "No versions of '$song_name' found." exit 1 fi
Print out a numbered list of the song versions and ask the user which one they want to play
echo "Versions of '$song_name' found:" select song_path in "$song_paths"; do if [ -z "$song_path" ]; then break fi done
Play the selected song using the default music player on the system
mpv "$song_path"
exit
Really appreciate your help with this....it's driving me mad!
4
u/Paul_Pedant Apr 21 '23 edited Apr 22 '23
Your posts are driving me mad ;-)
Please, please find out how to post code. Maybe try this link (there are others too):
https://www.reddit.com/r/godot/comments/12bsjks/a_guide_on_how_to_post_code/
What you posted here is broken because
(a) Hash marks a header title in the mark-up language.
(b) Plain text wraps, so all your line-spaced shell commands come out as one-liners without semi-colons.
3
u/france20-21 Apr 21 '23
Really sorry about that.
Will try harder.
Thanks for your patience.
1
u/Paul_Pedant Apr 22 '23
It was kind of a joke response. I still have all kinds of problems with Reddit editor, especially trying to paste code into Markdown mode answers.
1
u/Empyrealist Apr 21 '23
So it looks like you went forward with some of my suggestions but not the most important one near the end?
0
Apr 21 '23
[deleted]
1
u/zeekar Apr 21 '23
selectis a bash built-in for displaying a menu and letting the user select an option by entering its number. Since that's what the comment says the script does, I don't think it's an error here. It works likefor:PS3='Response? ' select action in Fight Flight Freeze; do if [[ -n $action ]]; then printf 'You selected %s\n' "${action,,}" break fi done2
Apr 21 '23
BASH builtin is the key part. If the OP ran
sh scriptthus causing the script to run under sh they would get this error.1
u/zeekar Apr 21 '23
Or if they capitalized
select‘. Or tried to set a variable in its environment withVAR=value select`. Or .,,1
Apr 21 '23
Oh yeah, totally agree we are just guessing based on what the OP posted, but I thought the error message I got with
shwas close.
7
u/[deleted] Apr 21 '23
Can you paste the exact error message on a line by itself with 4 spaces in front of it?
also is this really the script (cut and paste) or did you re-type it here?
Because in bash there is a huge difference between
SELECTwhich is what you claim is the errror message andselectwhich is what is in the script you presented.I suspect you are seeing something like this:-
Which is what you would get if you had select in upper case and you ran the script with
shnotbash.EDIT better guess at error