Find the answer to your Linux question:
Results 1 to 9 of 9
I have a question, I want to be able to do the following, but not sure if it is possible. I want to create a case statement script that will ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2005
    Posts
    8

    Advanced Case Statement question ...


    I have a question, I want to be able to do the following, but not sure if it is possible.

    I want to create a case statement script that will generate the options that the user can pick by certain files in a directory listing.

    For example ...

    In the directory I have

    report-Addition
    report-Subtraction
    report-cat
    report-dog

    so once the script starts, they will have the 4 following choices (addition,subtraction,cat,dog). However, if another file is added into the directory ... report-fish ... the next time the script is run, they will now have 5 choices (addition,subtraction,cat,dog,fish)

    is it possible to make a case statement such as described above?

    And if it is possible, then I could I use awk to cut away the "report-" before each word before it is displayed as options in the case statement?

    I hope this all makes sense ...

    I am not a linux expert so I am calling out for some help :P

    Thanks!

  2. #2
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    I just read ur post and a quick thought that occured to me was that

    u can run the command "ls" at the beginning of the script
    redirect the output of ls to a file "ls > log.txt"
    and the use the awk command and make ur case structure
    possible.
    but note just before exiting the script delete the file usnig the "rm" command. so that the next time it does not append the output again to the same file

    so even if u have any new file added. the next time u run the script it will get the new files

    this is just a thought. u can improvise on this and try to do this without the use of the temporary file

  3. #3
    Just Joined!
    Join Date
    Sep 2005
    Posts
    8
    Yeah thats what I was thinking of doing ... but my question would then be, how do I get the list of files from the temp file to be the choices of my case statement

    do I need another loop to do that?
    etcetc

    thanks

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    oh ho
    so it looks like the case structure should be generated dynamically according to the number of files in the directory right?

    let me think. right now only one thought is coming to my mind. Its like a round about way i think.

    You can create another script file dynamically (at the beginning of the first script file) with the syntax u want and the case structure needed by reading the temp file line by line and echoing it into the dynamic script file

    do a chmod 777 after updating the dynamically created script file

    Then if the user selects "addition" you can try running the dynamically created script file from the first script file with the input as addition and the no's to be added etc....


    I have not tried this out( will try it out later and let u know how it works out) . I just thought of this logic just now. But there must be some other logic by which we can do it in a simpler way

  6. #5
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    if u have a better method please tell me also since it will be useful for me also anytime in future when i come across a situation like this

  7. #6
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    158
    I would forget the case statement:
    Code:
    ls -1 report* | awk '{ print NR,substr($0,index($0,"-")+1) }'; echo "Q Quit"
    read choice
    if [ $choice = "Q" ]; then
       exit
    fi
    let count=1
    for file in `ls -1 report*1
    do
        if [ $count -eq $choice ];
        then
               workfile=$file
               break;
        fi
    done
    # at this point workfile is the name of the file
    if [ ! -f $workfile ]; then
        echo "invalid choice"
        exit
    fi
    # you now have a filename = workfile

  8. #7
    Just Joined!
    Join Date
    Sep 2005
    Posts
    8
    Awesome, thank you
    I will test it out tomorrow

    Thanks again

  9. #8
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    that was really cool , jim

  10. #9
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Use select:
    Code:
    #!/bin/sh
    select file in "Quit" $(ls)
    do
        if [ "$file" = "Quit" ]
        then
            exit 0
        fi
        if [ "x$file" = "x" ]
        then
            echo "Please select a file"
        else
            # Do stuff with the selected file.
            echo "You have chosen file \"$file\""
        fi
    done
    Isn't that easier?

Posting Permissions

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