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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
12-09-2016, 10:15 AM
|
#1
|
Member
Registered: Aug 2012
Posts: 789
Rep: 
|
Using sudo with cat and redirect?
How can the following be performed using sudo?
Code:
cat >> /etc/init.d/foo <<'EoT'
Hello
EoT
|
|
|
12-09-2016, 10:21 AM
|
#2
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Quote:
Originally Posted by NotionCommotion
How can the following be performed using sudo?
Code:
cat >> /etc/init.d/foo <<'EoT'
Hello
EoT
|
did you try this?
Code:
sudo cat >> /etc/init.d/foo <<'EoT'
Hello
EoT
|
|
|
12-09-2016, 10:23 AM
|
#3
|
Member
Registered: Aug 2012
Posts: 789
Original Poster
Rep: 
|
Quote:
Originally Posted by BW-userx
did you try this?
Code:
sudo cat >> /etc/init.d/foo <<'EoT'
Hello
EoT
|
Yes, I did so before asking the question. I think it doesn't work because I am applying sudo to cat and not the file in question?
Code:
michael@raspberrypi:~ $ sudo cat >> /etc/init.d/foo <<'EoT'
> Hello
> EoT
-bash: /etc/init.d/foo: Permission denied
michael@raspberrypi:~
|
|
|
12-09-2016, 10:32 AM
|
#4
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Quote:
Originally Posted by NotionCommotion
Yes, I did so before asking the question. I think it doesn't work because I am applying sudo to cat and not the file in question?
Code:
michael@raspberrypi:~ $ sudo cat >> /etc/init.d/foo <<'EoT'
> Hello
> EoT
-bash: /etc/init.d/foo: Permission denied
michael@raspberrypi:~
|
You're doing it right, the sudo comes before the command.
1. did you make sure the user had sudo privileges in the /etc/sudoers file, and has been added to the group, wheel or sudo, (one or the other) ?
Don't forget to logout then log back in so the group changes will take effect.
Last edited by BW-userx; 12-09-2016 at 10:35 AM.
|
|
|
12-09-2016, 10:34 AM
|
#5
|
Member
Registered: Aug 2012
Posts: 789
Original Poster
Rep: 
|
Quote:
Originally Posted by BW-userx
You're doing it right, the sudo comes before the command.
1. did you make sure the user had sudo privileges in the /etc/sudoers file, and has been added to the group, wheel or sudo, (one or the other) ?
|
This works:
Code:
michael@raspberrypi:~ $ sudo touch /etc/init.d/foo
[sudo] password for michael:
michael@raspberrypi:~ $
|
|
|
12-09-2016, 10:37 AM
|
#6
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Quote:
Originally Posted by NotionCommotion
This works:
Code:
michael@raspberrypi:~ $ sudo touch /etc/init.d/foo
[sudo] password for michael:
michael@raspberrypi:~ $
|
try going su
sometime even on my system it will not allow me to do certain things using sudo, so I just go su then do it.
|
|
|
12-09-2016, 10:54 AM
|
#7
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,827
|
The problem with your redirection is that the file is opened by your current shell prior to invoking sudo. If you want the opening to occur in the privileged environment, you have to write the command line that way:
Code:
sudo sh -c "cat >> /etc/init.d/foo" <<'EoT'
> Hello
> EoT
Now it's a privileged shell that is opening the output file.
|
|
|
12-09-2016, 11:02 AM
|
#8
|
Member
Registered: Aug 2012
Posts: 789
Original Poster
Rep: 
|
Thanks rknichols. I think you had a small typo, and this works perfect.
Code:
sudo sh -c "cat >> /etc/init.d/foo <<'EoT'
> Hello
> EoT"
Do you mind explaining why it works? man says the following about sh's -c flag. Also, what are the two > before Hello and EoT all about?
Quote:
Read commands from the command_string operand instead of from the standard input. Special parameter 0 will be set from the command_name operand and the positional parameters ($1, $2, etc.) set from the remaining argument operands.
|
|
|
|
12-09-2016, 11:15 AM
|
#9
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Code:
> # that one adds to a file, over writing everything in that file, replacing
it with whatever you do , whereas,
>> # adds the next line underneath whatever is already there. Appending.
To redirect standard output, use the > symbol.
Placing > after the cat command (or after any utility or application
that writes to standard output) directs its output to the file name
following the symbol. Press [Enter] to go to an empty line
and use the [Ctrl] - [D] key combination to quit cat.
more on that here
https://www.cyberciti.biz/faq/unix-l...ext-to-a-file/
Last edited by BW-userx; 12-09-2016 at 11:18 AM.
|
|
|
12-09-2016, 11:22 AM
|
#10
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,827
|
Quote:
Originally Posted by NotionCommotion
Thanks rknichols. I think you had a small typo, and this works perfect.
Code:
sudo sh -c "cat >> /etc/init.d/foo <<'EoT'
> Hello
> EoT"
|
No, I did not make a typo. The command is correct as I wrote it.
Code:
sh -c "cat >> /etc/init.d/foo"
"sh -c" spawns a shell that runs the command line that is in the quoted string that follows, in this case "cat >> /etc/init.d/foo". Running that shell under sudo gives it the elevated privilege it needs to open /etc/init.d/foo.
There is no need for elevated privilege for the stdin redirection from the HERE document. That can be done by the current shell, and the new shell will inherit that stdin.
The "> " ahead of "Hello" and "EoT" are not something you type. They are the prompt that the shell displays when reading the HERE document from the terminal. I suppose to be consistent I should have shown the "$ " prompt for the full command.
Last edited by rknichols; 12-09-2016 at 11:24 AM.
|
|
|
12-09-2016, 11:25 AM
|
#11
|
Member
Registered: Aug 2012
Posts: 789
Original Poster
Rep: 
|
Quote:
Originally Posted by rknichols
The "> " ahead of "Hello" and "EoT" are not something you type. They are the prompt that the shell displays when reading the HERE document from the terminal. I suppose to be consistent I should have shown the "$ " prompt for the full command.
|
I should have known that. What is funny is that it actually worked as I showed it.
|
|
|
12-09-2016, 11:33 AM
|
#12
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,827
|
Quote:
Originally Posted by NotionCommotion
What is funny is that it actually worked as I showed it.
|
It will work that way, too, with the privileged shell doing the HERE document redirection. It just gets messy trying to include all that in the quoted string, and if that "EoT" were not quoted there would security implications with the privileged shell doing the parameter expansion, command substitution, and arithmetic expansion of the HERE document lines. You generally should do only those things that need extra privilege in the elevated shell.
|
|
|
12-10-2016, 10:49 AM
|
#13
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Quote:
Use output redirection again for another file and call it home.txt.
For this example, type the command cat > home.txt, then [Enter] ,
followed by: bring the coffee home take off shoes put on sneakers make
some coffee relax! Now, on an empty line,
use the [Ctrl] - [D] key combination again to quit cat.
|
Code:
userx@voided1.what~>> cat >> test2>>catRedirect <<'EoT'
> g
> g
> g
> g
> bash: warning: here-document at line 21 delimited by end-of-file (wanted `EoT')
userx@voided1.what~>> cat >> test2>>catRedirect <<'EoT'
> EoT
userx@voided1.what~>> cat catRedirect
g
g
g
g
with sudo
Code:
userx@voided1.what~>> sudo sh -c "cat >> testCAtWithSuDo"
hey your whata yo
userx@voided1.what~>> sudo sh -c "cat >> testCAtWithSuDo <<'HO'"
userx@voided1.what~>>
userx@voided1.what~>> cat testCAtWithSuDo
hey your whata yo
by you doing this experiment by hand yourself, I hope you will better see what is the end point is with what you are trying to do. To gain you your actual needed results.
Last edited by BW-userx; 12-10-2016 at 10:54 AM.
|
|
|
All times are GMT -5. The time now is 09:19 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|