Shell Script | AWK | Variable inside FOR-Loop

Hi!

I am new to OSX Shell Script and am trying to work my way into it. I don't get the expected outcome with the following snippet of my script code and I have no idea, why. I read most of the Apple Shell Script Pimer and googled a lot about it, but to no avail.

I want to check the current username against some given usernames and set the value of a variable depending on the outcome. I used awk inside the shell script to get this done, but for some reason the for loop inside the awk part seems to change the value of the current username with every loop.

This is the code:

#!/bin/zsh

awk -v u=$USER 'BEGIN {
       print "Username outside loop is " u;

      count[0] = "Anton";
      count[1] = "Bert";
      count[2] = "Carl";                          

      for ( c_num in count ) {
             print "Username inside loop is " u;
             print "Compare to "count[c_num];                                    
             if (u = count[c_num]) {
                  test_var = "X";
             } else {
                  test_var = "Y";
             }
            print test_var;
      }
}'

And this is what I get after I run it in terminal:

Username outside loop is Zac
Username inside loop is Zac
Compare to Carl
X
Username inside loop is Carl
Compare to Anton
X
Username inside loop is Anton
Compare to Bert
X

I don't see why 'u' is changed every loop.

I'm thankful for every help / advice!

Markus

Awk is an open sourced language available on various platforms. Although awk may be included in most versions of macOS, it is provided as is, no support. You should better find a better place to learn awk. (And in fact, there are so many sites about awk.) By the way, I think = (a single equal sign) is an assignment operator in awk, not a comparison operator.

This isn’t really an answer but…

My experience with shell scripts is that they’re fine for simple stuff but once you start doing anything complex, like loops, it’s better to switch to a more modern scripting language. These days I use Swift for this sort of thing but I wouldn’t necessarily suggest that to other folks [1]. Historically I used to use Python, and I think that strikes the right balance between a ‘real’ programming language and easy integration with other tools. Python, Perl, Ruby, and more are all built in to macOS, so choose one that works for you.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Primarily because Swift isn’t very good at running other command-line tool. See this post for some background on that.

Hi OOPer,

thanks for your reply. You are right in both your answers: I read a lot about awk on the web and my mistake was putting = where ~ would have been right.

Hi eskimo and thank you for your reply. Learning swift is on my bucket list for ages… Just wanted an easy solution to be able to mount network shares to mount points of my choice on Mac OSX in my firm and the only way that seemed to work for me was a shell script so far.

Shell Script | AWK | Variable inside FOR-Loop
 
 
Q