I need to check for a directory in the home directory of another user. Normally I would sudo but that forks another process and I also lose my environment.
For example, I have:
if [[ -d "/home/otheruser/svn" ]];
then
echo "SVN exists"
else
echo "SVN does not exist"
fi
I need the the test condition to run with root permissions.
In most Linux distributions, the sudo package is installed by default. To use sudo, let's just type sudo and press enter. If sudo is installed, the sudo package usage details will be displayed. If it's not, a “command not found” message will be displayed.
If you are just starting to explore the Bash coding language, you will soon find yourself wanting to create conditional statements. Conditional statements, in other words, define 'if a condition is true or false, then do this or that, and if the opposite is true, do something else'.
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 ....
if sudo test -d "/home/otheruser/svn"; then
You need to run it under a subshell. Example:
if sudo bash -c '[[ -d "/home/otheruser/svn" ]]'
then
echo "SVN exists"
else
echo "SVN does not exist"
fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With