But the strange thing happens when I move this line outside the IF statement:
sudo bash "$0" "$@"; stops the code from running and only blinking dot stays with no progress further.
Not seeing from here what "$0" (the running programs name) and "$@" (a list of all comand line arguments to the running program) contains or any other parts of the environment you execute the snippet above in i can only speculate. The variable "EUID" is perhaps holding the "effective user ID", so the above reads: if you're not root already issue "sudo ...", which makes sense, because you need sudo to switch to root only if you aren't already root.
So my first guesses would be: either
- you try to use sudo as root and you have some sudo rule in place (or missing) that prevents correct execcution of whatever you try to execute, or
- your environment (especially "$0" and "$@" do not contain what you expect them to contain, maybe because of a switched context: switching to another user may cause settings to change, etc.) or
- There might be a reason for the following exit-statement. Leaving it out may prevent ending of the execution.
But to debug your script i'd have to see it and probably know a bit about your surroundings (OS, shell, version, ....) to do so. As long as you don't provide that you will be on your own.
If you move the sudo outside the if (and therefore run it every time) the code will call itself every time, and call itself, and call itself..........
The exit is necessary because otherwise you will drop through and try to run the remainder of the script as a non-root user. An alternate to the exit might be to drop the current script and re-start it with sudo like this:-
If you move the sudo outside the if (and therefore run it every time) the code will call itself every time, and call itself, and call itself..........
fi[/CODE]
I hope that this helps,
Robin
Yep, that's infinite loop, thanks for pointing that out. That might be the case.
The final version looks like this(only added comments that explains bahaviour):
(Any suggestions to make it more readable would be great)
For starters, you almost never need to use $?, a normal if-statement will do. Written this way, you can kind of see the logic. "If not sudo, then ..."
Not sure if it's really a simplification but I'd add >&2, the traditional place for error messages. That way if someone uses this in a stream they can direct data output and error output separately.
You should have an exit after exec, in case the exec fails for whatever reason (i.e. path problems or something).
Then I'd label the section which is always intended to run as root.
The "replace and re-run" bit comes down to how exec really works: It causes the running program to replace itself with what you ask. Whether it succeeds or fails, after an
The >&2's make more likely that these messages get printed to the terminal, where these messages usually belong.
More could probably be done with apt-get, like checking its return value rather than just assuming it operates correctly.
Last edited by Corona688; 11-02-2018 at 12:56 PM..
Hi 2 all,
i have had AIX 7.2
:/# /usr/IBMAHS/bin/apachectl -v
Server version: Apache/2.4.12 (Unix)
Server built: May 25 2015 04:58:27
:/#:/# /usr/IBMAHS/bin/apachectl -M
Loaded Modules:
core_module (static)
so_module (static)
http_module (static)
mpm_worker_module (static)
... (3 Replies)
Hello.
System : opensuse leap 42.3
I have a bash script that build a text file.
I would like the last command doing :
print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt
where :
print_cmd ::= some printing... (1 Reply)
Hi All,
i am trying to ssh to a remote machine and execute certain command to remote machine through script.
i am able to ssh but after its getting hung at the promt and after pressing ctrl +d i am gettin the out put as
expect: spawn id exp5 not open
while executing
"expect "$" {... (3 Replies)
How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address
and column 3 contains “cc” e-mail address to include with same email.
Sample input file, email.txt
Below is an sample code where... (2 Replies)
Hi All,
I'm completely new to bash scripting and still learning my way through albeit vey slowly.
I need to know where to insert my server names', my ip address numbers through out the script alas to no avail.
I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
The system don't boot.
on the screen appears following:
press enter to maintenance (or type CTRL-D to continue)...I checked with format command.
... the slices "0-root","1-swap","2-backup" exist.
...the slises "3-var","6-usr" -unassigned. :( (16 Replies)
Hi,
I have line in input file as below:
3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL
My expected output for line in the file must be :
"1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL"
Can someone... (7 Replies)
Hi Friends,
Can any of you explain me about the below line of code?
mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`
Im not able to understand, what exactly it is doing :confused:
Any help would be useful for me.
Lokesha (4 Replies)