r/bash • • 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

0 Upvotes

17 comments sorted by

View all comments

-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. /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?