0

I am testing a small script in the DD-WRT Web interface that randomizes the router's MAC address. The script uses awk to do the randomization. The script works when awk is used without the shebang (#!/bin/bash), as well as vice versa (without awk but including the shebang). However, the script does not work when both the shebang and awk is used.

Works (uses awk, but no shebang):

nvram set mac_clone_enable=1;
nvram set def_hwaddr=$(awk 'function m(n){srand(systime()+n);return":"(10+int(rand()*99));}END{print "A4"m(1)m(2)m(3)m(4)m(5);}');
nvram commit;
rc restart;

Also Works (has shebang, but no awk):

#!/bin/bash

nvram set mac_clone_enable=1;
nvram set def_hwaddr="02:44:55:66:77:88";
nvram commit;
rc restart;

Doesn't Work (shebang and awk):

#!/bin/bash

nvram set mac_clone_enable=1;
nvram set def_hwaddr=$(awk 'function m(n){srand(systime()+n);return":"(10+int(rand()*99));}END{print "A4"m(1)m(2)m(3)m(4)m(5);}');
nvram commit;
rc restart;

I need the script to use awk and have the shebang, so it can be used in a cron job. What could be the problem?

user3163495
  • 2,425
  • 2
  • 26
  • 43
  • 2
    wrt `the script does not work` - tell us in what way it doesn't work - wrong output, no output, core dump, error messages, etc... When you ask a mechanic for help to diagnose a problem with your car hopefully you don't just point at your car and say "it doesn't work, what could be the problem?". – Ed Morton Nov 17 '17 at 22:46
  • @EdMorton the MAC address was not being changed when it wasn't working. User karakfa provided a solution. – user3163495 Nov 17 '17 at 23:57
  • If that was the problem then it is not possible that the solution you said works actually does work for the same awk version. I suspect the real problem is that you're calling a different version of awk in each script and that's something that will come back to bite you later if you don;t debug it now. – Ed Morton Nov 18 '17 at 00:10
  • @EdMorton Well it works with karakfa's answer (changing END{} to BEGIN{})). How would I call a different version of awk in the script? – user3163495 Nov 18 '17 at 04:08
  • Of course it does because that's not all he did, he also removed your call to the non-POSIX systime() function and so left you with a script that will work in ALL awks, not just those that have a systime() function and will immediately execute the END section given no input (which you must have on your system since you told us the first script you posted worked with no input file). You would call a different version of awk by having a different PATH setting in each case when your script reaches the call to awk. – Ed Morton Nov 18 '17 at 04:15

1 Answers1

1

awk is expecting an input file. You can rewrite instead using the BEGIN block

awk 'function r() {return ":"(10+int(rand()*99))} 
     BEGIN{srand(); print "A4" r() r() r() r() r()}'

returned

A4:72:63:62:91:102

you also do not need to reinitialize random seed each time; once is enough.

karakfa
  • 66,216
  • 7
  • 41
  • 56