BASH scripting question

wetcat007

Diamond Member
Nov 5, 2002
3,502
0
0
#!/bin/bash
MESSAGES=$(awk -F'from | port....' '/Invalid user/{printf $2 "\n" }' messages.htm)
echo -e $MESSAGES

Alright so the problem I have is I need that to output as a list(one IP address per line). However instead it throws all the IP address's it pulls out of a file into a space separated mess. If I run that awk command normally without assigning it to a variable it works perfectly fine and they each get their own line.

Anyways I need it to show one item per line, so that this will in theory work with it as well.

#!/bin/bash
MESSAGES=$(awk -F'from | port....' '/Invalid user/{printf $2 "\n" }' messages.htm)
DONE=$(echo -e $MESSAGES | sort -u )

So it will sort them and remove duplicates.

Any help would be greatly appreciated :)
 

LintMan

Senior member
Apr 19, 2001
474
0
71
Unfortunately I don't have access to a unix system to twiddle with the syntax and experiment now, and my scripting knowledge blurs ksh and bash together sometimes, but there's a number of ways you should be able to get that stuff to work. I can't test it, but here's some suggestions to try:

- Send the output to a temp file instead of a variable, then cat the file:
#!/bin/bash
awk -F'from | port....' '/Invalid user/{printf $2 "\n" }' messages.htm | sort -u > ipaddrs.txt
cat ipaddrs.txt
rm ipaddrs.txt

- Loop through all the entries in MESSAGES:
#!/bin/bash
MESSAGES=$(awk -F'from | port....' '/Invalid user/{printf $2 "\n" }' messages.htm)
for IPADDR in MESSAGES; do
echo $IPADDR
done
#NOTE: on the for line above, MESSAGES might need to be $MESSAGES

- Try using `` to run awk instead of $() - it might keep the output as one string:
#!/bin/bash
MESSAGES=`awk -F'from | port....' '/Invalid user/{printf $2 "\n" }' messages.htm`
echo $MESSAGES

At least one of those should work for you, I hope.
 

LintMan

Senior member
Apr 19, 2001
474
0
71
I got a chance to try some stuff out:

- The temp file method should work fine.

- The for loop should also work (and you do need $MESSAGES).

- Using `` instead of $() has the same behavior.