help using EOF inside a for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help using EOF inside a for loop
# 1  
Old 10-30-2012
help using EOF inside a for loop

I'm having problems with this example:

Code:
#!/bin/bash

for i in server1 server2 server3
do
    ssh $i <<EOF >> logfile.txt
      dat1=`date '+%m/%d/%y'`
      hst=`hostname`
      df -Ph | awk -v OFS=',' -v dat="$dat1,$hst" ' {print dat,$1,$6,$3,$4 } '
      exit
    EOF
done

I'm trying to get the disk information and output it to logfile.txt.

I'm getting the error:

./disk_check.sh: line 12: syntax error: unexpected end of file

I've tried many different permutations, but no luck.
# 2  
Old 10-30-2012
The terminating EOF needs to appear at the very beginning of the line. If you want to indent it, you must the <<- redirection operator, which strips all leading tabs (not spaces) from all of the lines.

Regards,
Alister

---------- Post updated at 12:53 AM ---------- Previous update was at 12:46 AM ----------

You'll also need to backslash escape the dollar symbols in the awk script. The quotes are not special to the here-document, so they do not prevent the shell expansion of $1,$6,$3,$4.

Or, if you don't want any of the expansions (including the command substitutions) to occur locally, in the redirection operation you'll want to quote EOF, <<'EOF' (or <<-'EOF' if you want to strip leading tabs).

Regards,
Alister

Last edited by alister; 10-30-2012 at 01:58 AM..
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Write a while loop inside for loop?

I'm taking a unix class and need to countdown to 0 from whatever number the user inputs. I know how to do this with a while or until loop but using the for loop is throwing me off.... I know I can use an if-then statement in my for loop but can I include a while loop in my for loop? (3 Replies)
Discussion started by: xxhieixx
3 Replies

2. Shell Programming and Scripting

If inside If loop

Hi All, Below is the very simple code snippet but it si giving me syntax error #!/bin/bash #To ensure If JMS directory exists or not ServerName=$(hostname) #To ensure If JMS directory exists or not echo $ServerName if ; then echo "Inside First If" if ; then echo 'JMS... (4 Replies)
Discussion started by: sharsour
4 Replies

3. Shell Programming and Scripting

Testing for EOF during a while loop

I'm a complete UNIX newbie trying to write a simple shell script. The pseudo-code for the part I'm having trouble with is as such: read something while ; do this The loop terminates at EOF (like when Ctrl+D is pushed) and displays the number of lines the user entered (kept in the loop as a... (17 Replies)
Discussion started by: Sovereign110
17 Replies

4. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

5. Shell Programming and Scripting

Using 'su' inside a loop

Hi, I am using su within a for loop. As you might expect, it prompts for password during each loop execution. Here is my piece of code: for i in $LIST do if then DATABASE=`echo $i | awk -F "|" '{ print $1 }'` USER_ID=`echo $i | awk -F "|" '{ print $2 }'` su - apstage -c... (1 Reply)
Discussion started by: sugan
1 Replies

6. Shell Programming and Scripting

ssh inside a for loop

Hi, The requirement is to ssh to unix servers and oracle databases, to perform some monitoring activity. I'm using shell script to perfom this. I pass the server details and database to a variable ... SERVERS="SERVER1 SERVER2 SERVER3" DATABASE="DB1 DB2 DB3" for i in $SERVERS do ssh... (2 Replies)
Discussion started by: senthil3d
2 Replies

7. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

8. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

9. Shell Programming and Scripting

"unexpected end of file" when Iīm use EOF inside block if

I have a trouble in my script when i use EOF inside block if. If i use EOF whitout block if I donīt have problem. Guys any ideas? Sorry for my terrible English. #!/bin/sh set -xv HOST='ftp.fiction.com.br' USER='fictionuser' PASS='fictionpass' FILE='ftpteste.txt' busca=`find... (4 Replies)
Discussion started by: ricardo.ludwig
4 Replies
Login or Register to Ask a Question