Find the answer to your Linux question:
Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By Irithori
  • 1 Post By JohnGraham
Is it possible to set up a catch-all for bash script's variables? For instance, imagine that $VAR could return either "abc-123" and "abc-456", but also "def-123" and "def-456". Is it ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! Pyrobisqit's Avatar
    Join Date
    May 2011
    Posts
    29

    Catch-all for bash variables?


    Is it possible to set up a catch-all for bash script's variables?

    For instance, imagine that $VAR could return either "abc-123" and "abc-456", but also "def-123" and "def-456".

    Is it possible to make a script that, depending on if $VAR returns something with "abc" in the beginning does something, but when $VAR returns something beginning with "def" does something different?

    Thanks!

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,348
    You are probably looking for the "case" statement
    Testing and Branching
    Pyrobisqit likes this.
    You must always face the curtain with a bow.

  3. #3
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    152
    Yes, you can use a case statement:

    Code:
    case "$1" in
        abc*)
            # Gets executed if first arg starts with "abc".
            echo "Saw abc-something"
            ;;
        def*)
            # Gets executed if first arg starts with "def".
            echo "Saw def-something"
            ;;
        *)
            # Gets executed if previous forms didn't match.
            echo "Dunnow"
            ;;
    esac
    Pyrobisqit likes this.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

  4. #4
    Just Joined! Pyrobisqit's Avatar
    Join Date
    May 2011
    Posts
    29
    Quote Originally Posted by JohnGraham View Post
    Yes, you can use a case statement:

    Code:
    case "$1" in
        abc*)
            # Gets executed if first arg starts with "abc".
            echo "Saw abc-something"
            ;;
        def*)
            # Gets executed if first arg starts with "def".
            echo "Saw def-something"
            ;;
        *)
            # Gets executed if previous forms didn't match.
            echo "Dunnow"
            ;;
    esac
    Thank you JohnGraham and Irithori for the near-instant response!

Posting Permissions

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