bash variable (set via awk+sed) not working as expected

Hi!

Been working on a script and I've been having a problem. I've finally narrowed it down to this variable I'm setting:

servername=$(awk -v FS=\/ '{ print $7 } blah.txt | sed 's\/./-/g' | awk -v FS=\- '{print $1}')"

This will essentially pare down a line like this:

http://1.2.3.4/blah/moreblah/soo.blah/myserver1.domain.com/index.html#anchor>link

To simply this: myserver1

It's a bit involved, but it works.

The issue I'm having is that I've placed this within a loop (for x in 1 2... while 1 do ...) and the variable SERVERNAME parses through all the data on the first pass through so when the loop comes around a second time, there's no data and the script poops out saying "awk: can't open file XXXX" [where XXXX equals a myserver value]

If I set SERVERNAME=myserver1 manually everything works as expected.

Anyone know why setting this variable as command output is executing multiple times rather than just the single iteration I'm looking for? This behavior is showing up in awk and gawk.

Cheers!

-cd
ps. I've also used backticks, pushing the contents of this command to a temp file from which I "<temp.txt" the data into the variable, but it is always the same result.

Can you post your script..at least the loop portion..?

Hard to find out with just those few lines. Maybe post the whole script or/and use a set -x to show debugging output of the shell.
There seems a leading " missing at

servername="$(awk
           ^

Here's the loop bit of the script: (The missed quotation mark was a typo..I wasn't able to cut/paste.. had to manually type the example in)


for x in warn crit

do
echo "- - - - - - - - - - -"
curl -s | grep $x > temp-data.txt

awk -v FS=\" '{ for (i = 1; i <= NF; i++)
       if ($i ~ /'"$x"'/)
          print $i,$(i+3),$(i+2) }' temp-data.txt > blah.txt

# Let's strip off all the malformed HTML from the results (blah.txt)...
sed 's/>//g' blah.txt | sed 's/<\/a>//g' | sed 's/<\/a//g' > blah2.txt

# Now let's add properly formatted HTML to the results so we get valid weblinks to each server.
sed 's/<a   href=\ /<a href=http:\/\/1.2.3.4\/directory\/archive\//g' blah2.txt | sed 's/$/>link<\/a>/' > blah3.txt

# Strip out a servername from the URL to make for easy readability... 

This is the line that seems to go into its own nested loop before proceeding to the next step below which prints the single line of output I'm looking for.

servername=$(awk -v FS=\/ '{ print $8 }' blah3.txt | sed 's/\./-/g' | awk -v FS=\- '{ print $1 }')

# Now printing data out again with our new column added.
awk -v serv=$servername -v mtime=$mtime -v mdate=$mdate '{ print mdate,mtime,$1,$2,serv,$3,$4 }' blah3.txt

done

Thanks!

-cd

Just to clarify, if I set SERVERNAME like this, everything works. If I set it as shown in the previous post, it errors out.

servername=1234

Still looking for a hand with this if anyone has ideas...

Thanks!
-cd

Are you sure that your printing the right column. The above awk command prints the 8th column.Also the command could be shortened as

servername=$(awk -F\/ '{ print $7 }' blah3.txt | awk  FS=\. '{ print $1 }')

Also as you have said that there is no value while it loops the 2nd time check the for loop values' 2nd iteration i.e crit and additionally before running the whole script try each command after curl manually and check if it gives the expected output or not. Alternatively you can also use set -x on top of the script to view the script output in de-bug mode.

Thanks for the tip on shortening the command.

It seems that the variable declaration evaluates all data on the first time through. I was only looking for a single iteration so I could nicely insert the variable in the last awk command. It's looking like I'll need to store all these iterations in an array and call it as a variable in the last awk statement, but I'm weak on arrays and I'm not sure it'll actually work.

If it won't work that way, perhaps I can just save all the SERVERNAME variable values to another text file then attempt to join them?

I solved my problem by going another route.

I piped the servername output to a separate file rather than an in-script variable, then used the 'paste' command to combine the servername file with the rest of the output that I'd dumped into a file as well (for the purpose of combining).

It's not as clean as it could be, but we're all about results right? :slight_smile:

Thanks Michael and Zaxxon for the replies. I appreciate your time.

-cd