0

This is my bash script.

#!/bin/bash
oo="`cat /etc/httpd/conf/httpd.conf`";
cat > /a.txt << EOF
$oo
EOF

it simply reads /etc/httpd/conf/httpd.conf and writes it to /a.txt, the problem has been when this is executed via web scripts with sudo command.. for some reason not everything is written to the a.txt file

but when executed via command line.. everything is written just fine to a.txt

so perhaps i should send this to a background process by adding:

> /dev/null 2>&1

to it. but how can this be done? i tried

#!/bin/bash
oo="`cat /etc/httpd/conf/httpd.conf`";
cat > /a.txt << EOF
$oo
EOF > /dev/null 2>&1

this did not work.

  • What do you mean by “not everything is written”? Is the end missing, or bits in the middle? How is the script invoked? There's nothing wrong with the script you've shown. – Gilles 'SO- stop being evil' Jun 17 '14 at 23:09
  • > /dev/null 2>&1 doesn't have anything to do with background processes. >/dev/null cancels the previous >/a.txt and 2>&1 hides errors, neither is desirable. – Gilles 'SO- stop being evil' Jun 17 '14 at 23:11
  • You can not redidrect with sudo, you need to use tee or other methods. This is a FAQ - stackoverflow.com/questions/82256/… . See also the other answers as I think there are simpler ways of coding this. – Panther Jun 17 '14 at 23:41
1

Maybe this example is just extremely oversimplified, but I'm having trouble seeing why you wouldn't just run:

cp /etc/httpd/conf/httpd.conf /a.txt

That is, there's already a command that simply reads from one file and creates another with its contents, and it's called cp. The only real difference would be if /a.txt already existed and you were trying to retain its permissions, or some such - but even then, you'd want to just do:

cat /etc/httpd/conf/httpd.conf >/a.txt
  • Agreed. And I guess we had the same trouble at the same time. – mikeserv Jun 17 '14 at 22:32
0

To run a command with a HERE-DOC in background, specify & on the first line:

command << EOF &
blah blah
EOF

In this particular case, I don't see what the point is. You already redirect stdout to a.txt, why do you want to redirect it to /dev/null at the same time?

0

I think you should do:

cat </etc/httpd/conf/httpd.conf >/a.txt

Try as I might I can't see that the code in your question should have any other effect.

When you:

cat <file 

...or...

cat <<FILE
    $(shell generated file contents)
FILE

You just copy cat's standard-in to its standard-out. So in this scenario:

<<FILE
    `cat <file`
FILE

<<FILE and <file are identical.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.