Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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,759
    You are probably looking for the "case" statement
    Testing and Branching
    You must always face the curtain with a bow.

  3. #3
    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
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

  4. $spacer_open
    $spacer_close
  5. #4
    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
  •