Title: bash script interpreting awk correctly
Tags: linux;bash;shell;awk
Question: I have a for loop script that needs to log into hosts and get their hostname and the value of eth0 interface. Im using the code below, but the awk command is not being read correctly when running it on a bash script
#!/bin/bash
for i in `cat test.txt`;
store_number=$(ssh -q -A -o userknownhostsfile=/dev/null -o stricthostkeychecking=no -o batchmode=yes -o connecttimeout=5 "$i" "hostname | cut -c4-7");
eth0_ip=$(ssh -q -A -o userknownhostsfile=/dev/null -o stricthostkeychecking=no -o batchmode=yes -o connecttimeout=5 "$i" "sudo ifconfig eth0 | awk 'FNR==2 {print $2}'");
output="${store_number}: ${eth0_ip}"; echo $output &gt;&gt; /home/eth0status.txt;
The output is as follow:
0021: inet addr:151.154.11.224 Bcast:151.154.11.224 Mask:151.154.11.224
0022: inet addr:151.154.11.224 Bcast:151.154.11.224 Mask:151.154.11.224
0023: inet addr:151.154.11.224 Bcast:151.154.11.224 Mask:151.154.11.224
I need the output to be something like:
0021: addr:151.154.11.224
0022: addr:151.154.11.224
0023: addr:151.154.11.224
Thanks for your help
Comment: I recommend you only do what's absolutely necessary in the `ssh` and do anything else on your local machine. For example, instead of `eth0_ip=$(ssh ... "sudo ifconfig eth0 | awk 'FNR==2 {print $2}'")` do `eth0_ip="$(ssh ... "sudo ifconfig eth0")"` and then `eth0_ip=$(echo "$eth0_ip" | awk 'FNR==2 {print $2}'")` or similar.

Comment: idk of any "best practice" specification for ssh but It's for simplicity and clarity which IMHO win out over every other consideration.

Comment: I don't see any problems with your script, looks good to me. However, if you want a quick and dirty fix, you can always do, at the end, `echo $output | cut -d' ' -f1,3` >> /home/eth0status.txt`

Comment: @jframone: Glad that helped. Also, Ed's right. When I said "I don't see any problems with your script", it was more like saying "I don't see why are you getting those results and not the correct ones". That `awk` should only keep the IP addresses, not the whole row.

Comment: @fanton Looks like adding the cut command did the trick, thanks.

Comment: @EdMorton Thanks i'll keep that in mind. Is that for best practice?  Im new to bash.


Here is another answer: Quoting is a hard problem. When you write:
eth0_ip=$(ssh ... "$i" "sudo ifconfig eth0 | awk 'FNR==2 {print $2}'");
The internal single quotes '' do nothing to prevent expansion of $2. The shell sees $2 in double quotes. It will perform variable expansion, and it's highly likely your second argument ($2 for the script) is unset. Test it out:
$ echo "sudo ifconfig eth0 | awk 'FNR==2 {print $2}'"
sudo ifconfig eth0 | awk 'FNR==2 {print }'
Either escape $, or use single quotes outside, or as others have recommended, use awk outside the SSH command.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 6, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:02/16/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<linux><bash><shell><awk>', 'Title': 'bash script interpreting awk correctly', 'ViewCount': 127, 'id': 35437115, 'user_AboutMe': None, 'user_AccountId': 7582413.0, 'user_DisplayName': 'jframone', 'user_Reputation': 1.0, 'user_Views': 3.0, 'user_id': 5752718.0}}