Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

how to bring together case with 2 inputs?

case inputs bash

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1 memad

memad

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 24 April 2015 - 04:10 AM

Hi guys,
 
im newbie, im trying something, but cannot figure out how to bring together case with 2 inputs.
Something like "enter your name" and "enter your age".
 
I had idea but dont now how to finish it as I said i'm just a beginner.
 
#!/bin/bash
echo "Name"
echo -n "Enter your name: "
read name
echo -n "enter your age: "
read age
echo
case $name,$age in
"Thomas Muller"|35) echo "you are too old" ;;
"Luka modric"|28) echo "you are boring" ;;
esac
 
when i try it nothing happens, I had idea how i can solve it in pascal nad converted it to bash, but i really dont know if it is possible.
 
if [ "$name" == "name" ] && [ "$age" == "age" ]; then 
  echo "$age";

I appreciate any help!

 


#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 24 April 2015 - 10:32 AM

You'd want to do something like:

case $name in
    "Thomas Muller")
        if [[ $age -eq 35 ]]; then
            echo "You are too old"
        fi
    ;;
    "Luka Modric")
        if [[ $age -eq 28 ]]; then
            echo "You are boring"
        fi
    ;;
esac

Either that, or you can concatenate your inputs together into one string:

case "$name,$age" in
    "Thomas Muller,35")
        echo "You are too old"
    ;;
    "Luka Modric,28")
        echo "You are boring"
    ;;
esac

Just make sure that a comma won't be entered in $name or $age because that could mess up your case statement.


sudo rm -rf / && echo $'Sanitize your inputs!'