Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate script in bash and save it to location requiring sudo

Tags:

bash

heredoc

sudo

In bash I can create a script with a here-doc like so as per this site: http://tldp.org/LDP/abs/html/abs-guide.html#GENERATESCRIPT

( cat <<'EOF' #!/bin/bash #? [ ] / \ = + < > : ; " , * |  #/ ? < > \ : * | ” #Filename="z:"${$winFn//\//\\} echo "This is a generated shell script." App='eval wine "C:\Program Files\foxit\Foxit Reader.exe" "'$winFn'"' $App EOF ) > $OUTFILE 

If my $OUTFILE is a directory requiring sudo privileges where do I put the sudo command or what else can I do to make it work?

like image 972
D W Avatar asked Dec 10 '10 18:12

D W


People also ask

How do I give a sudo permission to a script?

To give root privileges to a user while executing a shell script, we can use the sudo bash command with the shebang. This will run the shell script as a root user. Example: #!/usr/bin/sudo bash ....

How do I run a bash script in sudo mode?

Therefore, to run a shell script or program as root, you need to use sudo command. However, sudo only recognizes and runs commands that exist in directories specified in the secure_path in the /etc/sudoers, unless a command is present in the secure_path, you'll counter an error such as the one below.

Why is sudo command used in Linux?

The sudo command allows you to run programs with the security privileges of another user (by default, as the superuser). It prompts you for your personal password and confirms your request to execute a command by checking a file, called sudoers , which the system administrator configures.

What is #!/ Bin bash?

#!/usr/bin/bash is a shebang line used in script files to set bash, present in the '/bin' directory, as the default shell for executing commands present in the file. It defines an absolute path /usr/bin/bash to the Bash shell.


1 Answers

This is how I would do it:

sudo tee "$OUTFILE" > /dev/null <<'EOF' foo bar EOF 
like image 109
Dennis Williamson Avatar answered Sep 21 '22 15:09

Dennis Williamson