test2.shġ1496 pts/1 ubuntu \_ ps -fo pid,tty,user,argsġ1500 pts/1 ubuntu \_ ps -fo pid,tty,user,argsġ1503 pts/1 root \_ ps -fo pid,tty,user,argsĪnd a simpler test using sudo -s $. Here is another test using sudo -s $ ps -fo pid,tty,user,args. # exec replaces the current process effectively ending execution so no exit is needed.Įxec sudo "$0" echo "Take 'exec' out of the command and this script would get cat-ed twice. (Try it.)Įcho echo "User $(whoami) is running." Take 'exec' out of the command and this script would get cat-ed twice. test.sh -o foo -p barġ415 pts/1 root \_ ps -t /dev/pts/1 -fo pid,tty,user,args test.sh -o foo -p barġ412 pts/1 root \_ bash. test.sh -o foo -p barġ411 pts/1 ubuntu \_ ps -t /dev/pts/1 -fo pid,tty,user,argsġ337 pts/1 root \_ sudo. I insist on replacing "execute" with "call" because the former has a meaning that includes creating a new process and ID, where the latter is ambiguous and leaves room for creativity, of which I am full.Ĭonsider this test case and look closely at pid 1337 # Don't worry, the content of this script is cat'ed belowġ408 pts/1 ubuntu \_ bash. Replace the shell with a given program (executing it, not as new process) If you really want to " ExecuteCall a shell script in current shell with sudo permission" you can use exec to. Make it executable with chmod x script.sh. Make the file script.sh in your current directory with the contents: #!/bin/bash I'm not sure what you actually wanted, but hopefully this will clear it up for you. It isn't possible to source a file running as root, without creating a new subprocess, as you cannot change the runner of a program (in this case, bash) after it has started. script.sh do however? Remember source isn't a program (rather a shell builtin)? Sudo expects a program name though, so it searches for a program named source. script.sh executes script.sh in a sub process but running as root. How does sudo interact with this? Well sudo takes a program, and executes it as root. So to source a file script.sh in the current directory, you just use source script.sh. Search /bin and /usr/bin - you won't find a source program there. Note that source isn't a program - otherwise it wouldn't be able to change environmental variables in the current shell. Sourcing a script doesn't use the PATH, and just searches for the path. script.sh (unless the current directory is in the PATH, eg you can run ls while in the /bin dir). ![]() ![]() Note that the current directory isn't in PATH, so you can't execute a file script.sh in the current directory by running script.sh, you need to run. If the command is a single file (eg script.sh), it will check all the folders in the PATH variable to find the script. script.sh will execute the file script.sh in the current directory. ![]() Running a script is simple, you just type in the path to the script. This is useful as it lets a script change environmental variables in the shell. It effectively types the commands in as if you did them. Sourcing a script can only be used with a bash script (if you are running bash). As it is a sub process, any environmental variables changed in the program will not affect the shell. The program can be a shell script, or any other type of program. I think you are confused about the difference between sourcing and executing a script.Įxecuting a script means creating a new process, and running the program.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |