LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-17-2012, 06:22 AM   #1
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 632

Rep: Reputation: 33
bash : getting text into file


Hello,

I need to create file 121212-flb with first line of text [121212-flb] with bash script :
Code:
#!/bin/bash

FLBfile=$1-flb
touch /etc/addons/$FLBfile

FLBheading="[$1-flb]"
makeFLB=$(/etc/addons/$FLBfile << $FLBheading)
My command :
[root@p2 admin]# sh new.sh 121212

Output :
Quote:
new.sh: line 9: warning: here-document at line 9 delimited by end-of-file (wanted `$FLBheading')
new.sh: line 8: warning: here-document at line 8 delimited by end-of-file (wanted `$FLBheading')
new.sh: line 8: /etc/addons/121212-flb: Permission denied
File is created, but without text inside.
 
Old 03-17-2012, 06:34 AM   #2
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 632

Original Poster
Rep: Reputation: 33
Solved : makeFLB=$(echo $FLBheading > /etc/addons/$FLBfile)
 
Old 03-17-2012, 01:21 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
No, this doesn't work the way you think it does:

Code:
makeFLB=$(echo $FLBheading > /etc/addons/$FLBfile)
"variable=$( command )" sets the value of variable to the standard out of command. But "command > filename" redirects the stdout of command into filename, and so there's no output available to set the variable with.

Your makeFLB variable will always come out empty.


So to start with, do the redirection on its own, without the variable setting:
Code:
echo "$FLBheading" > "/etc/addons/$FLBfile"
Then tell us what the makeFLB variable is supposed to contain, so we can help you set it properly.

(P.S.: You also don't need to touch the filename first. > and >> will create the file if necessary.)

Next, for the record, your first error message resulted from here:

Code:
makeFLB=$(/etc/addons/$FLBfile << $FLBheading)
The section inside $() is considered a separate command environment, but here there's no executable command name inside it; only a filename. Since file redirections like "<<" only work in the context of executed commands, in this case it's instead treated as the beginning of a here document.

And since there's no matching closing string for the heredoc in the script, it chokes and coughs up the error given.

Last edited by David the H.; 03-17-2012 at 01:28 PM. Reason: fixes
 
  


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
[SOLVED] Bash command to 'cut' text into another text file & modifying text. velgasius Programming 4 10-17-2011 04:55 AM
variable from text file bash drumbum Programming 1 07-30-2009 09:50 AM
bash script to create text in a file or replace value of text if already exists knightto Linux - Newbie 5 09-10-2008 11:13 PM
how to change some text of a certain line of a text file with bash and *nix scripting alred Programming 6 07-10-2006 11:55 AM
send text to file bash bendeco13 Linux - General 6 11-01-2004 06:26 PM


All times are GMT -5. The time now is 06:08 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