Bash script for checking multi variable in if and for loop
1
0
Entering edit mode
23 months ago

Hi all,

I would like to create a small script for a particular detection and reporting but I can't reach my goal in bash, as a starter in shell scripting it is hard. Please correct or suggest it.

for i in VAR1 VAR2 VAR3 VAR4 VAR5 VAR6
do
  if [ "$i" == "$varab" ] 
  then
    VAR1="Haploid"
    VAR2="Diploid"
    VAR3="Triploid"
    VAR4="Tetraploid"
    VAR5="Pentaploid"
    VAR6="Hexaploid"
      echo "$i is the result"
      break
  else
    echo "$i is not working"
  fi
done;

varab - can be anything from VAR1 to VAR6

In short, I need to find what is the variable among 6 and report it properly.

bash shell • 598 views
ADD COMMENT
1
Entering edit mode
23 months ago
barslmn ★ 2.3k

You need a single equal sign to do the comparison. Try it with if [ "$i" = "$varab" ]. In shell [ is the program that does the test. So you can learn more about it from its man page man [.

ADD COMMENT

Login before adding your answer.

Traffic: 2721 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6