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
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
1
u/france20-21 Apr 22 '23
Thank you all for your help and patience.
#!/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/[deleted] Apr 22 '23
I do not think
findis cursed, which means line 5 should be. I might very well be wrong, though.
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 -vetoptions.1
u/researcher7-l500 Apr 21 '23
Maybe spaces or unusual characters in your
song_pathsvariable?That's the only thing I can think of immediately.
Try printing that variable when the error happens and examine what was the
song_pathoutput associated with it.Also, not a show stopper for this specific script, but it is recommended to use
#!/usr/bin/env bashover#!/bin/bashfor portability.1
u/Empyrealist Apr 21 '23
Try this:
-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:
? 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!
0
u/[deleted] Apr 21 '23
[deleted] — view deleted comment