0

I am getting command not found error for code below:

#!/bin/sh
#set -x
for i in `cat  output`;
do
eval "MASTER_$i = $(/usr/efm-2.0/bin/efm cluster-status $i 2>/dev/null |grep "Master"  |head -1 |awk -F " " {'print$2'})";
echo -e "$MASTER_$i";
done

Debug output is:

./test.sh
++ cat output
+for i in '`cat  output`'
++ /usr/efm-2.0/bin/efm cluster-status abc
++ grep Master
++ head -1
++ awk -F ' ' '{print$2}'
+eval 'MASTER_abc = 10.x.x.x'
++ MASTER_abc = 10.x.x.x
**./test.sh: line 5: MASTER_abc: command not found**
+echo -e abc
abc
2
  • Btw.: I suggest to use a while loop. Commented Aug 24, 2017 at 18:36
  • You aren't iterating over the lines of your file; you are iterating over the whitespace-separated words resulting from the contents of your file being subjected to pathname expansion. See Bash FAQ 001. Commented Aug 24, 2017 at 22:22

2 Answers 2

2

You have spaces around the =... If you try running eval "i = 1", it will try running command i, with parameters = and 1 instead of setting i to be 1.

Sign up to request clarification or add additional context in comments.

Comments

1

Strongly recommend you use a bash associative array instead of dynamic variable names:

#!/bin/bash
#set -x
declare -A master
while IFS= read -r line; do
    master["$line"]=$( /usr/efm-2.0/bin/efm cluster-status "$line" 2>/dev/null |awk '/Master/ {print $2; exit}' )
done < output.file

for line in "${!master[@]}"; do
    printf "%s\t%s\n" "$line" "${master[$line]}"
done 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.