LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 11-29-2010, 06:41 AM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
How write a literal bash command in a bash file?


Hi,

I create a bash script that writes another bash file. But in the generated bash file I want to write a bash command in the file and not executing it.

Here's my bash file:

Code:
#!/bin/bash

cat > ~/generateGridmix2data.sh << END

#!/usr/bin/env bash

GRID_DIR=`dirname "\$0"`

END
The file that is created is the following:
Code:
#!/usr/bin/env bash

GRID_DIR=.
This is not what I want.

What i want is a file with the following:
Code:
#!/usr/bin/env bash

GRID_DIR=`dirname "\$0"`
What I put in my bash file to write a literal command in the generated bash file?

thanks,
PSC
 
Old 11-29-2010, 07:18 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
To make a here document fully literal, put quotes around the starting string.
Code:
cat > ~/generateGridmix2data.sh << 'END'

#!/usr/bin/env bash

GRID_DIR=`dirname "\$0"`

END
http://www.tldp.org/LDP/abs/html/here-docs.html (assuming bash).

Also, $(..) is recommended over `..`
 
Old 11-29-2010, 10:51 AM   #3
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
And if I want to really put a value of a variable and a literal command in a Here Documents block? (Please notice that 'END' is between ' '.)

For example:
Code:
#!/bin/bash

value=5;

cat > ~/generateGridmix2data.sh << 'END'

#!/usr/bin/env bash

GRID_DIR=`dirname "\$0"`
VAR=$value

END
Thanks,

Last edited by xeon123; 11-29-2010 at 10:58 AM.
 
Old 11-29-2010, 11:40 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by pedrosacosta View Post
And if I want to really put a value of a variable and a literal command in a Here Documents block?
You can't. In alternative you can try something like this:
Code:
(
 echo '#!/usr/bin/env bash'
 echo
 echo 'GRID_DIR=`dirname "\$0"`'
 echo "VAR=$value"
) > ~/generateGridmix2data.sh
that uses echo statements in a subshell with proper quoting. Maybe it is a bit tedious to prepend each line with an echo statement, but I cannot see other ways, right now.
 
Old 11-29-2010, 11:43 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
At this stage I'd start looking into m4 ;}


Cheers,
Tink
 
Old 11-29-2010, 11:48 AM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Maybe I've missed something, but is the following output what you want?:
Code:
root@reactor: cat generateGridmix2data.sh

#!/usr/bin/env bash

GRID_DIR=`dirname "\$0"`
VAR=5

root@reactor:
If so, here's what I'm using:
Code:
#!/bin/bash

value=5;

cat << END > generateGridmix2data.sh

#!/usr/bin/env bash

GRID_DIR=\`dirname "\\\$0"\`
VAR=$value

END
 
Old 11-29-2010, 12:05 PM   #7
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
Thanks Celine.

That's what I wanted.
 
  


Reply



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, when I read sort write md5 file, the writen file have a space missing peter1234 Linux - General 3 09-21-2010 09:04 AM
[SOLVED] [bash] read file, modify, write - possible without tmpfile? hashbang#! Programming 2 02-11-2010 05:27 AM
Bash - is it possible to write to memory rather than a file basildon Linux - Newbie 7 09-24-2008 12:07 PM
best way to write a dynamic conf file in bash dkrysak Linux - General 6 02-13-2007 12:49 PM
Trying to write a bash script -- :Command not found? bpottle Linux - Software 7 06-10-2006 06:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:36 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
Open Source Consulting | Domain Registration