Using script variable in awk subtraction
1 Attachment(s)
I have the following script:
Code:
#!/bin/bash Code:
int=$(echo $i |awk -v val=$adj '{print ($1 - $val)}') What am I doing wrong with this line that it isn't subtracting the value properly? *I had to use a screenshot because the VMware console window doesn't support copying/pasting text. |
I think I solved this. Instead of using the '-v val=$adj' assignment I used
Code:
int=$(echo $i |awk '{print ($1 - '$adj')}') |
Firstly, your issue was the you used $adj in awk where you call variables with their name, ie. just 'adj'.
Secondly, you previously use bash math: Code:
adj=$(($ifaces - 3)) Also you can simplify your current usage further: Code:
(( adj = ifaces - 3 )) |
the problem with that awk is that you do not need $ inside the script:
int=$(echo $i |awk -v val=$adj '{print ($1 - $val)}') you need to remove it - but of course, (( int = i - adj )) is much better in line: MAC[$i]=$(grep eth$i $udevfile |awk -F ',' '{print $4}' |awk -F "==" '{print $2}') only one awk would be enough instead of grep|awk|awk |
What would the one awk in the MAC line look like?
|
awk -F, " /eth$i/ "' { split ($4, a, "=="); print a[2] }'
I have not tested because I have no input file, but probably works, after testing: MAC[$i]=$( this is the awk ) if you give a sample file we can try to test it also _____________________________________ If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post. Happy with solution ... mark as SOLVED (located in the "thread tools") |
I would also point out that your for loop is not going to work either as you are trying to assign the value 0 to the value of 'i' by using $i.
Also, you asked previously how to get the MAC values in your question here, which showed you a number of better methods. Code:
ifaces=$(grep eth $udevfile |wc -l) As with the math examples above you could also change your tests from the older [] form to also use (()): Code:
if [ $i -lt 3 ] |
Thanks for the additional input. I discovered the assignment of 0 to $i when I started putting this into full test mode.
Thanks for the heads up with the -c option for grep. That should provide some more elegant lines in the future. |
All times are GMT -5. The time now is 07:23 PM. |