user2052255
user2052255

Reputation: 41

comparing $var to

I have a test script the needs to read the variable 'LAB' and e-mail the correct company. I've looked but can't find anything that has worked yet.

Any thoughts?

#!
#
LAB=3
#
if [ "$LAB" = "$1" ];then
    echo "Got Zumbrota" && ./mailZ 
fi
#
if [ "$LAB" = "$2" ];then
    echo "Got Barron" && ./mailB 
fi
#
if [ "$LAB" = "$3" ];then
    echo "Got Stearns" && ./mailS
fi

Upvotes: 0

Views: 83

Answers (2)

glenn jackman
glenn jackman

Reputation: 246744

These cascading if statements can be condensed into a case statement:

case "$LAB" in
1)  echo "Got Zumbrota" && ./mailZ 
    ;;
2)  echo "Got Barron" && ./mailB 
    ;;
3)  echo "Got Stearns" && ./mailS
    ;;
*)  echo "don't know what to do with $LAB"
    ;;
esac

Upvotes: 1

sr01853
sr01853

Reputation: 6121

If this a bash script, start your file with

#!/bin/bash

and use -eq for integer comparison and since LAB is an integer in your script

if [ $LAB -eq $1 ]

Upvotes: 1

Related Questions