LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How write a literal bash command in a bash file? (https://www.linuxquestions.org/questions/linux-newbie-8/how-write-a-literal-bash-command-in-a-bash-file-847195/)

xeon123 11-29-2010 06:41 AM

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

David the H. 11-29-2010 07:18 AM

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 `..`

xeon123 11-29-2010 10:51 AM

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,

colucix 11-29-2010 11:40 AM

Quote:

Originally Posted by pedrosacosta (Post 4174845)
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.

Tinkster 11-29-2010 11:43 AM

At this stage I'd start looking into m4 ;}


Cheers,
Tink

GrapefruiTgirl 11-29-2010 11:48 AM

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


xeon123 11-29-2010 12:05 PM

Thanks Celine.

That's what I wanted.


All times are GMT -5. The time now is 10:11 AM.