-1

I have a set of variables (M1 to M20) with values 1 or 0 in them. And i want to loop through them and echo a message when one is 1.

#!/bin/sh
for r in {1..20}
do
    if [ ${"M$r"} -eq 1 ]
    then
        col=`expr 150 \+ $r`
        echo " M"$r" is here: "$col" "
    fi  
done

I can't figure out the substitution.

  • 1
    There's an extensive discussion in [BashFAQ #6](http://mywiki.wooledge.org/BashFAQ/006). BTW, it's generally better to start with trying to read documentation to find the right syntax to accomplish purpose-X, instead of making up some syntax and then asking about the error message it gives you if/when it isn't actually valid. – Charles Duffy Jun 01 '20 at 16:15
  • here you go mate ``` for r in {1..20} do v=M$r if [[ "${!v}" = "1" ]] then col=`expr 150 \+ $r` echo " M"$r" is here: "$col" " fi done``` – robert.baboi Jun 01 '20 at 17:22
  • Thank you both. @CharlesDuffy I did try to find what I was looking for in the topics but the 2 above didn't came up with as I didn't phrase that way. Thanks for the pointers and I'll look into that topic. – Ivan Ivanov Jun 02 '20 at 06:53
  • @IvanIvanov : Since you are interested in solutions, which don't work only with `bash`, but also, as in the example you posted, with `sh`, make sure you read the part saying _If you aren't using Bash or Korn shell,_ in the document Charles Duffy refers to. – user1934428 Jun 02 '20 at 07:47
  • @robert.baboi, `expr` is not part of the POSIX standard (it's an artifact from the 1970s that was excluded from the 1992 POSIX.2 standard), and it's very slow to run in comparison with native shell-builtin math. Use `col=$((150 + r))` and it's guaranteed to work in any POSIX-compliant shell. – Charles Duffy Jun 02 '20 at 14:49

1 Answers1

0

You really should use an array for this sort of thing, but the syntax you're looking for is:

#!/bin/bash

for i in {1..20}; do
        v=M$i
        test "${!v}" = 1 && echo M$i = 1
done

I would use = rather than -eq unless you know that all the values look like integers. Otherwise you'll probably want to suppress the error messages, but IMO it's easier just to do a string comparison.

William Pursell
  • 204,365
  • 48
  • 270
  • 300