LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-04-2011, 09:04 AM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
dynamic variables and heredocuments in bash


Hi,

I've created a script that put in dynamic variables the value HELLO 1, HELLO 2, ..., HELLO 5. And I put the values of this variables in the file text.txt. Here's the script:

Code:
for i in $(seq 1 5); do eval ${i}=$(cat << EOF "HELLO" $i EOF); done

cat  > text.txt << EOF 
$1
$2
$3
$4
$5
EOF
But my script isn't working. Can someone help me, please?

Here's the error:

Code:
bash: warning: here-document at line 102 delimited by end-of-file (wanted `EOF')
bash: warning: here-document at line 102 delimited by end-of-file (wanted `EOF')
bash: warning: here-document at line 1 delimited by end-of-file (wanted `EOF')
cat: HELLO: No such file or directory
cat: 1: No such file or directory
cat: EOF: No such file or directory
1=: command not found
bash: warning: here-document at line 102 delimited by end-of-file (wanted `EOF')
bash: warning: here-document at line 102 delimited by end-of-file (wanted `EOF')
bash: warning: here-document at line 1 delimited by end-of-file (wanted `EOF')
cat: HELLO: No such file or directory
cat: 2: No such file or directory
cat: EOF: No such file or directory
2=: command not found
bash: warning: here-document at line 102 delimited by end-of-file (wanted `EOF')
bash: warning: here-document at line 102 delimited by end-of-file (wanted `EOF')
bash: warning: here-document at line 1 delimited by end-of-file (wanted `EOF')
cat: HELLO: No such file or directory
cat: 3: No such file or directory
cat: EOF: No such file or directory
3=: command not found
bash: warning: here-document at line 102 delimited by end-of-file (wanted `EOF')
bash: warning: here-document at line 102 delimited by end-of-file (wanted `EOF')
bash: warning: here-document at line 1 delimited by end-of-file (wanted `EOF')
cat: HELLO: No such file or directory
cat: 4: No such file or directory
cat: EOF: No such file or directory
4=: command not found
bash: warning: here-document at line 102 delimited by end-of-file (wanted `EOF')
bash: warning: here-document at line 102 delimited by end-of-file (wanted `EOF')
bash: warning: here-document at line 1 delimited by end-of-file (wanted `EOF')
cat: HELLO: No such file or directory
cat: 5: No such file or directory
cat: EOF: No such file or directory
5=: command not found

Thanks,
 
Old 04-04-2011, 11:20 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Well, for one thing, the final limit string of a here document needs to exist on its own line, without any spaces or other things in front of it. There may be some cases where bash will accept other things on the line after it, but you shouldn't count on that. So it's best to give it its own line.

There are also quoting problems with your eval line. Since the evaluation of the line will devour the ones that exist, you'll need to insert a second level of them in order to protect the string you actually want to run.

Third, you can't use $1, $2, etc. as variables, as they are reserved parameters.

Finally, seq is unnecessary, as bash has it's own built-in version, called brace expansion.

Here's what I came up with that appears to do what you want:

Code:
string=$(cat <<EOF
HELLO
EOF
)

for i in {1..5}; do
     eval x${i}="\"$string $i\""
done

cat > text.txt <<EOF
$x1
$x2
$x3
$x4
$x5
EOF
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 09:37 AM
[SOLVED] Q: Dynamic Variables - How The Heck ? jakekatz Linux - Newbie 6 04-23-2010 09:47 AM
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 07:09 AM
Dynamic / Auto-Updating Variables in BASH hackop Programming 1 04-25-2009 04:22 PM
C++, when to go for static or dynamic variables? linuxlah Programming 21 02-05-2003 09:38 AM


All times are GMT -5. The time now is 12:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration