Results 1 to 6 of 6
Ok here is a simple question. I am writing a script that requires a user select something from a list. This in itself is not difficult, however it is the ...
- 09-08-2007 #1Just Joined!
- Join Date
- Apr 2007
- Posts
- 12
Menu's built from Variables.
Ok here is a simple question. I am writing a script that requires a user select something from a list. This in itself is not difficult, however it is the "list" it's self that is giving me issues.
ok I will generate a list of files "ls -lt let??" (note that the name and total number of files will very from hour to hour) that the user must select which one s/he would like to view. Now getting this list printed out to the screen is no problem and generating a menu "option 1, 2, 3, ...ect" is no problem. But where the problem starts is how to I get the script to display the first file "let10" as option 1 and "let11" s option 2 and so on? In some cases there will be 1 file and others there will be 20 files.
Any help would be appreciated.
Thanks
Carl
- 09-08-2007 #2
The trick to do this is a loop. For instance, to print out every file that matches 'let??', you could do:
Read more about loops at:Code:for file in let??; do echo "$file" done
http://www.tldp.org/LDP/abs/html/loops1.html
Slightly more difficult will be the menu. To do this, you will need to store the filenames in an array. An array is a collection of many values, indexed by some number. For instance, array[0] is the first element, array[4] is the 5th element, etc.
You could, for instance, do something like this:
You can read more about arrays at:Code:i=0 for file in let??; do files[i]=$file echo "$i. $file" i=$[i+1] done echo -n "Enter the number of the file you want: " read chosen_index echo "You chose ${files[chosen_index]}"
http://www.tldp.org/LDP/abs/html/arrays.html
Does this make sense?DISTRO=Arch
Registered Linux User #388732
- 09-08-2007 #3Just Joined!
- Join Date
- Apr 2007
- Posts
- 12
- 09-08-2007 #4
My advice is to use dialog to build menus. Such a sweet program. In most cases, it will make your life a lot easier.
- 09-09-2007 #5
dialog can be MAJOR overkill depending on what he needs. In any case, he still needs to store the information somewhere to access it with the menu choice, so the array thing still applies.
Also, cdslaughter, csh is not a great shell to be scripting in. Its syntax is more familiar, granted, but it is not as widely used as Bash, and is missing some important features that Bash contains. For some info, you can check out:
Csh Programming Considered Harmful
A good guide for beginning Bash scripters is:
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
The links I gave you before are from the Advanced Bash Scripting Guide, which goes into more detail and is an excellent reference:
http://www.tldp.org/LDP/abs/html/
Since you've done programming before, picking up these concepts shouldn't be too tough: hopefully it's just an environment/syntax thing.DISTRO=Arch
Registered Linux User #388732
- 09-11-2007 #6Just Joined!
- Join Date
- Apr 2007
- Posts
- 12
Thank you, THe menu is working great with a little tweaking.
Now by chance can you look at this thred and give me your input? It would really help me out.
ThanksHTML Code:http://www.linuxforums.org/forum/linux-programming-scripting/102956-ok-now-i-almost-have-but-not-quiet.html#post506556
Carl


Reply With Quote
