Find the answer to your Linux question:
Results 1 to 6 of 6
how would i have a menu system w/ case, and pass a variable through input, like having a delete listing like case "$var" in "$D" ) rm whatever ;; where ...
  1. #1
    Just Joined!
    Join Date
    Nov 2006
    Posts
    90

    case, menus, and passing strange var's

    how would i have a menu system w/ case, and pass a variable through input, like having a delete listing like

    case "$var" in
    "$D" )
    rm whatever
    ;;
    where they would just type d (whatever) to use rm (whatever)
    the reason i am asking is i just created a huge menu system for my vscanner (with you guy's help) but i still need my find and remove command to be able to be passed variables and still go back to the main screen. can i make it ask for them, like call "D" and have it ask "what file?" and remove it and then return to menu?

  2. #2
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    echo "To remove filename press D"
    case $var in
    D)
    echo -n "File to delete? "
    read FILENAME
    rm $FILENAME
    ;;


    Remember to set a path though, or else it will look in the current directory. Not so nice if it's running in /bin

  3. #3
    Just Joined!
    Join Date
    Nov 2006
    Posts
    90
    it is a seek and destroy command. will this work?

    echo "To remove filename press D"
    case $var in
    D)
    echo -n "File to delete? "
    read FILENAME
    find / -iname $filename -exec rm -rv {} \;
    ;;

  4. #4
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    For a more efficient deletion I recommend this as last line:

    rm `find / -name $FILENAME`

    Happy hunting

  5. #5
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52
    Quote Originally Posted by jwmw1023 View Post
    how would i have a menu system w/ case, and pass a variable through input, like having a delete listing like

    case "$var" in
    "$D" )
    rm whatever
    ;;
    where they would just type d (whatever) to use rm (whatever)
    the reason i am asking is i just created a huge menu system for my vscanner (with you guy's help) but i still need my find and remove command to be able to be passed variables and still go back to the main screen. can i make it ask for them, like call "D" and have it ask "what file?" and remove it and then return to menu?

    If you are using bash:

    Code:
    # print menu here
    
    read -n1 -p "d|q: " var
    case $var in
         d) rm whatever ;;
         q) exit ;;
    exit
    Or do you want:

    Code:
    # print menu here
    
    read -p ":: " var
    case $var in
         d*) [ -f "${var#* }" ] &&  rm "${var#* }" ;;
         q) exit ;;
    exit


  6. #6
    Just Joined!
    Join Date
    Nov 2006
    Posts
    90
    thanks guys that works great

Posting Permissions

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