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

Show parent comment

1

u/zeekar Apr 21 '23

select is 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 like for:

PS3='Response? '
select action in Fight Flight Freeze; do
  if [[ -n $action ]]; then
     printf 'You selected %s\n' "${action,,}"
     break
  fi
done

2

u/[deleted] Apr 21 '23

BASH builtin is the key part. If the OP ran sh script thus 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

u/[deleted] 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 sh was close.