LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 07:41 AM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 367

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, 08:18 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: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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, 11:51 AM   #3
xeon123
Member
 
Registered: Sep 2006
Posts: 367

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 11:58 AM.
 
Old 11-29-2010, 12:40 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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, 12:43 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

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


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

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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, 01:05 PM   #7
xeon123
Member
 
Registered: Sep 2006
Posts: 367

Original Poster
Rep: Reputation: 16
Thanks Celine.

That's what I wanted.
 
  


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


All times are GMT -5. The time now is 01:50 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration