Terminal: line of code to ask user to select name from a list?

Joined
Jul 16, 2017
Messages
2
Reaction score
0
Points
1
Hi all,
In my script I use the ls command to display the list of files in the directory. I then want the user to choose and input a single file name from the list (using the read command). If they enter a name not listed above I want the terminal to ask them to retype. The files in the directory are not always going to be the same every time the script is run, so the script will have to somehow compare the user input to all files in that directory at that current time. Can I compare input to ls somehow?

Thank you in advance
(P.s. I'm new to this :) )
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,762
Reaction score
2,100
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
First, what language are you typing the script in? The ls command is just going to list the contents of a directory in various ways, so for any comparison, you'll have to keep track of the files in an array or something and then compare that against subsequent ls commands.
 
OP
T
Joined
Jul 16, 2017
Messages
2
Reaction score
0
Points
1
I'm making a simple bash script, i'm writing it in text editor and then using 'chmod 700' to turn it into an exe file. Oh i see, how do i go about doing that? I don't exactly know what to search to find a relevant guide, perhaps you could point me in the right direction :)
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,762
Reaction score
2,100
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
I wouldn't suggest BASH as the language, you're better of with something like Python which has access to many packages that will aid you as your script gets more complicated.

However, if you want to stick with BASH, then checkout BASH Beginners Guide which explains the basics..

Beyond that, how you do what you need to do depends on how good you are at programming..
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
You haven't stated what you want to do with any selected files.

But I would use a simple AppleScript to select the Folder and Files to process.

After all, that's what AppleScript was designed for.

Regards Mark
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top