0

i am trying something which make my work easier.So here i have a script which wl collect IPs from diff file. The problem is that output of awk is not storing/wrong syntax is wrong.i am using bash.

MGR${count_IP}=$(awk -F '[=;]' '{print $2}' "${MgrFile}")
echo "Test=MGR${count_IP}"

for (( count=1;count<=$Manager_Count;count++))
do
    MasterFile=/etc/ansible/tmp/tmp_list
    MgrFile=/etc/ansible/tmp/tmp_mgr$count
    echo "$count-$MgrFile"

    grep -r "Manager_$count" $MasterFile > "${MgrFile}"
    echo "Copy List of IP and name Info to temp file"

    echo "Get MGR$count IP"
    MGR${count_IP}=$(awk -F '[=;]' '{print $2}' "${MgrFile}")
    echo "MGR${count_IP}"
done

Issue Part

    echo "Get MGR$count IP"
    MGR${count_IP}=$(awk -F '[=;]' '{print $2}' "${MgrFile}")
    echo "MGR${count_IP}"

Log:

 1-/etc/ansible/tmp/tmp_mgr1
 Copy List Info to temp file
 Get MGR1 IP
 ./2.sh: line 47: MGR=172.16.16.11: command not found
 MGR
 2-/etc/ansible/tmp/tmp_mgr2
 Copy List Info to temp file
 Get MGR2 IP
 ./2.sh: line 47: MGR=172.16.16.14: command not found
 MGR
anubhava
  • 761,203
  • 64
  • 569
  • 643
samir
  • 345
  • 2
  • 6
  • 15
  • 2
    A shell is an environment from which to call tools. It is not a tool to manipulate text. If you'd like help doing whatever you are trying to do the right way, then post a [mcve] including concise, testable sample input and expected output so we can help you. – Ed Morton Aug 24 '16 at 23:37
  • @EdMorton I imagine Ken Thompson, Steve Bourne, David Korn, Bill Joy, Brian Fox, and every other UNIX shell implementor since 1970 will be very discouraged to learn that they wasted all that time and effort on string manipulation features! I guess Microsoft got the memo, though. ;-) – Larry McQueary Aug 25 '16 at 02:36
  • Not at all, string manipulation functionality is extremely useful and necessary in the context of manipulating files and processes and sequencing calls to tools which is what a shell is for. Where would we be if we couldn't do `cmd "$file" > "${file%.csv}.txt"`, etc.? Since there's so many ways with UNIX tools that you can produce the desired output from a given sample input, its very important to learn the right tools for each job rather than hammering in your nails with a screwdriver and driving your screws with a chisel. – Ed Morton Aug 25 '16 at 05:54
  • 1
    @LarryMcQueary you might be interested in this answer written by the guy who discovered [the Shellshock bug](https://en.wikipedia.org/wiki/Shellshock_(software_bug)) everyone was stunned by a couple of years ago: [why-is-using-a-shell-loop-to-process-text-considered-bad-practice](http://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice). Although the question was about loops, the answer also gives some insight into issues related to manipulating text with shell in general. – Ed Morton Aug 25 '16 at 06:14
  • @EdMorton I can get behind your second comment. I just found your original statement rather broad. Certainly we can all agree to 'use the right tool for the job', and we benefit form many good choices in the modern era. Though I don't think OP is misusing the shell, _per se_, he would almost certainly benefit from expanding his use of `awk` to solve the rest of his problem, if we knew what that was. – Larry McQueary Aug 25 '16 at 06:57

2 Answers2

-1

Without any other direction, the shell will only evaluate each line/expression once. However, you're doing something special: you're assigning a value to a variable name that is not constant (i.e. its name contains a variable that must be expanded before assignment can occur). This means that you have to tell the shell to evaluate the expression twice; once to expand the variable name, and a second time to assign a value that variable. Therefore, you need an eval in front of the line you're having trouble with.

eval MGR${count_IP}=$(awk -F '[=;]' '{print $2}' "${MgrFile}")

The first pass will produce e.g.

MGR47=172.16.16.11 (or whatever)

The second pass (which is what eval is actually doing) will actually execute that assignment.

EDIT: There are numerous other ways the OP could write his script, but I limited my answer to something portable, simple and harmless in the context of the OP's example that would solve his problem. It has been pointed out by a commenter that the indiscriminate use of eval can result in unintended consequences, like the rise of Skynet, swarms of locusts appearing, or your entire hard drive being deleted. If you want to educate yourself further on this issue, please read the linked SO question:

Why should eval be avoided in Bash, and what should I use instead?

Community
  • 1
  • 1
Larry McQueary
  • 416
  • 3
  • 9
  • @EdMorton ... it might, in the same way that holding a carving knife might remove all your limbs :) Googling `eval is evil` yields nothing relevant to `bash`, but plenty relevant to javascript. A more thoughtful SO answer here, though: [Why should eval be avoided in Bash, and what should I use instead?](http://stackoverflow.com/questions/17529220/why-should-eval-be-avoided-in-bash-and-what-should-i-use-instead) – Larry McQueary Aug 25 '16 at 00:21
  • 1
    @EdMorton Thanks for that link, I did read it. Indiscriminate use of `eval` can be a bad thing, but the specific and limited use of `eval` in the context of OP's question and my answer is unlikely to cause any hard drives to be deleted. Ed, I have much bigger problems, though: _I use the C Shell_. Since 1988! Even **I** believe that nothing good can come of that! Have a good day :) – Larry McQueary Aug 25 '16 at 01:25
-2

This looks like a simple mix-up:

  • your variable is called $count until the first line of the issue part
  • after that it is called $count_IP

So

echo "Get MGR$count IP"
eval "MGR$count=\$(awk -F '[=;]' '{print \$2;}' '$MgrFile')"
echo "MGR${count}"

should work

Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • There's a `$count_IP` on the first line of the script though, but definitely some kind of mixup. – Jason C Aug 24 '16 at 22:07
  • I can say its partial as echo "MGR${count}" does not carry the output of awk. – samir Aug 24 '16 at 22:15
  • Issue Pats description is as follows.Here with echo "Get MGR$count IP" i am getting "Get MGR1 IP". In 2nd line awk will return me the ip which i am trying to store in a new variable with count variable.variable name will be like MGR1_IP.By 3rd line i am expecting MGR1_IP=10.10.10.10 – samir Aug 24 '16 at 22:17
  • works with this.Donot know why it need something after MGR$count in 2nd line echo "Get MGR$count IP" eval "MGR$count_IP=\$(awk -F '[=;]' '{print \$2;}' '$MgrFile')" echo "$MGR$count_IP – samir Aug 24 '16 at 22:37