Syntax when using the 'exec' command


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Syntax when using the 'exec' command
# 1  
Old 11-08-2016
Power Syntax when using the 'exec' command

Hey gang,

I'm confused about the 'exec' command. I get the basics. I can redirect stdout and stderr to one log file with:

Code:
exec >> $logFile 2>&1

However, that code requires admin rights. In other scripts, when faced with this, I'll call 'sudo' and then pass a hashed password like this:

Code:
sudo -S touch $logFile <<< "${password}"

While the above code works great to run a command with admin rights, it doesn't work with the 'exec' command:

Code:
sudo -S exec >> $logFile 2>&1 <<< "${password}"

I'm guessing that either:
  1. my syntax is wrong or
  2. I can't use sudo in a redirect

With no assistance from the man page, I could use some help from the hive mind here. Thoughts?

Last edited by rbatte1; 11-10-2016 at 05:32 AM.. Reason: Converted to formatted number-list
# 2  
Old 11-08-2016
Running the command:
Code:
sudo exec >> $logFile 2>&1

would setup a set-UID environment in which it will run the command:
Code:
exec >> $logFile 2>&1

and exit. Doing that seems pointless (the redirections will disappear when the command sudo ran completes and no redirections will have occurred in the current shell execution environment that your script will return to when the sudo is done).

If you want to run a script in a set-UID environment so it can open files to be used by that script, you need to use something more like:
Code:
sudo script_name

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 11-08-2016
I slightly disagree.

The command:
Code:
sudo exec >> $logFile 2>&1

will have the current, likely non privileged shell, redirect both stdout and stderr of the sudo command to the file named "$logfile", then sudo will (try to) run the regular exec command with no argument.

As it is very unlikely for a regular exec command to exist in the first place, being meaningless as an external command and only implemented as a builtin, this triggers an error message stating "sudo: exec: command not found" or similar written in "$logfile".

This log file will belong to the regular user running the command (at least if it wasn't already existing), not root.

Last edited by jlliagre; 11-08-2016 at 05:43 PM..
This User Gave Thanks to jlliagre For This Post:
# 4  
Old 11-08-2016
The debate over what my previous code may or may not do is worthwhile but not the point of my post. The point was to ask: how can i automate -- inside of the script -- for stdout and stderr to be re-directed to the same file, something that usually requires entering an admin password.

Thank you for helping to focus on that.

Quote:
Originally Posted by jlliagre
I slightly disagree.

The command:
Code:
sudo exec >> $logFile 2>&1

will have the current, likely non provileged shell, redirect both stdout and stderr of the sudo command to the file named "$logfile", then sudo will (try to) run the regular exec command with no argument.

As it is very unlikely for a regular exec command to exist in the first place, being meaningless as an external command and only implemented as a builtin, this triggers an error message stating "sudo: exec: command not found" or similar in the "$logfile".

This log file will belong to the regular user running the command (at least if it wasn't already existing), not root.
# 5  
Old 11-08-2016
The simplest approach would be to use ACLs and grant the current user the right to write to the logfile. That would require using an OS and file system that support ACLs though and the syntax will vary depending on the OS and the kind of ACLs supported.
# 6  
Old 11-08-2016
Quote:
how can i automate -- inside of the script -- for stdout and stderr to be re-directed to the same file, something that usually requires entering an admin password.
That is easy.

Code:
exec 1>filename
exec 2>&1

echo "from stderr" >&2
echo "from stdout"

None of the above requires root... I think you will discover that the problem is not exec -- the problem is the file itself. Perhaps the file is owned by root, preventing you from writing to it? Show ls -l filename please.

If your script runs with insufficient permissions, it cannot get more. Period. Full stop.

Either run it with sufficient permissions or fix the file so it doesn't need extra permissions.
# 7  
Old 11-09-2016
Quote:
Originally Posted by jlliagre
I slightly disagree.

The command:
Code:
sudo exec >> $logFile 2>&1

will have the current, likely non privileged shell, redirect both stdout and stderr of the sudo command to the file named "$logfile", then sudo will (try to) run the regular exec command with no argument.

As it is very unlikely for a regular exec command to exist in the first place, being meaningless as an external command and only implemented as a builtin, this triggers an error message stating "sudo: exec: command not found" or similar written in "$logfile".

This log file will belong to the regular user running the command (at least if it wasn't already existing), not root.
Hi jlliagre,
Yes. You are correct. To get what I was describing in post #2, you would need to use:
Code:
sudo sh -c "exec >> $logFile 2>&1"

Quote:
Originally Posted by hungryd
The debate over what my previous code may or may not do is worthwhile but not the point of my post. The point was to ask: how can i automate -- inside of the script -- for stdout and stderr to be re-directed to the same file, something that usually requires entering an admin password.

Thank you for helping to focus on that.
We are sorry that you are not interested in understanding why what you have asked cannot be done. That doesn't alter the fact that IT CANNOT BE DONE. Either you have permission to open the file to which you want to redirect output for writing without extended privileges, or you run the entire script with extended privileges. Redirecting output to a file in a sudo command will not grant other parts of that script that are not being run with extended privileges the ability to write to a file that it can't open for writing.

You could use sudo in your script to make the log file writeable by everyone using chmod. You could use sudo to run chown or chgrp to change the file's owner and/or group so you can write to it. You could use sudo to set an ACL allowing your user-ID or group-ID write access (if your system supports ACLs). Or, as I suggested in post #2, you could elevate the privileges of your entire script by running it with sudo. But, you cannot redirect output to a file that you don't have permission to write to in a sudo command to make that file writeable by the remainder of that script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux find command seems to not transmit all the result to the '-exec command'

Hello. From a script, a command for a test is use : find /home/user_install -maxdepth 1 -type f -newer /tmp/000_skel_file_deb ! -newer /tmp/000_skel_file_end -name '.bashrc' -o -name '.profile' -o -name '.gtkrc-2.0' -o -name '.i18n' -o -name '.inputrc' Tha command... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Exec command - what is it doing here?

Hi all, Forgive me for asking for help with my first post, but I am struggling here. I've been asked to translate a bash script into a Windows script (probably batch or powershell, not sure yet), so the first step is obviously understand what the bash script is doing. But I have no experience in... (5 Replies)
Discussion started by: el_foz
5 Replies

3. Shell Programming and Scripting

exec command help

Hi, I have the following lines in a script : . . exec < some_file . . . I have very little idea about exec command. I would like to know what this does and what will happen if the file some_file does not exist. Specifically, I would like to know whether the lines following this... (5 Replies)
Discussion started by: elixir_sinari
5 Replies

4. UNIX for Dummies Questions & Answers

Exec command

Hi can some one explain the following command , It would really help if some can really elloborate on what is happening out here export PATH | exec /bin/sh ./auto_approve :q P.S: This is the first time i am using exec ,so an elloboration what does it do and what is the use of the :q will be... (1 Reply)
Discussion started by: Sri3001
1 Replies

5. Shell Programming and Scripting

exec command

How can I use the exec command to log my korn shell session to the screen and the log file? Currently I have this command: $exec 1> ${LOG} 2>&1 This logs the output to the log file only. I want it to go to the screen also. Is this possible with this command? thanks. (10 Replies)
Discussion started by: djehresmann
10 Replies

6. Shell Programming and Scripting

exec command help

All, I am using below shell script to output the content to outputfile.txt. What I am looking for is in addition to outputfile.txt, I want the output to be on standard output too. exec > outputfile.txt echo "Starting " echo "ending" (5 Replies)
Discussion started by: basic_shell
5 Replies

7. Shell Programming and Scripting

exec command

can any one pls explain the meaning of exec 1<&5 ?? its urgent (2 Replies)
Discussion started by: santosh1234
2 Replies

8. UNIX for Dummies Questions & Answers

Need help with -exec cp command.

I have a ksh script that contains the following: find /dir1/dir2 -type f -name "FILE.*" -newer /dir1/dir2/afterme.txt -exec cp /dir1/dir2/dir3 {} \; When I run it from the cli, it runs fine. When I run it from the ksh script I get find: missing argument to `-exec' I also tried -exec cp... (40 Replies)
Discussion started by: bbbngowc
40 Replies

9. Shell Programming and Scripting

exec command

hai i want know the difference between two shell scripts those are 1) a=2004 echo $a #output------2004 exec < inputfile while read line do echo $a #output-------2004 a=2005 echo $line echo $a ... (1 Reply)
Discussion started by: g_s_r_c
1 Replies

10. UNIX for Dummies Questions & Answers

using the -exec command

linux redhat 8.0 I am getting accustomed to using the -exec command to get around my databse.. and use it to edit and update files..! is this more apllicable than jumping from one directory to the other.. I have set up the databse so that the inode #'s are accessable and can get me from one... (0 Replies)
Discussion started by: moxxx68
0 Replies
Login or Register to Ask a Question