Find the answer to your Linux question:
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 ...
  1. #1
    Just 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

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    The trick to do this is a loop. For instance, to print out every file that matches 'let??', you could do:
    Code:
    for file in let??; do
        echo "$file"
    done
    Read more about loops at:
    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:
    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]}"
    You can read more about arrays at:
    http://www.tldp.org/LDP/abs/html/arrays.html

    Does this make sense?
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Apr 2007
    Posts
    12
    Quote Originally Posted by Cabhan View Post
    Does this make sense?
    Well sort of. I am just starting to learn c-shell scripting. been doing other types of scripting with other programs, but I am still a bit shakey in csh.

    I will see what I can make it do and go from there.

    C.

  4. #4
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    My advice is to use dialog to build menus. Such a sweet program. In most cases, it will make your life a lot easier.

  5. #5
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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

  6. #6
    Just 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.

    HTML Code:
    http://www.linuxforums.org/forum/linux-programming-scripting/102956-ok-now-i-almost-have-but-not-quiet.html#post506556
    Thanks

    Carl

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...