Quantcast
Channel: Root gets permission denied when executing commands from script but not from shell - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 2

Root gets permission denied when executing commands from script but not from shell

0
0

I have a simple script that I'm using to sync a test environment for a couple of developers. It doesn't need to be any more complex than just taking a mysql dump, checking the hash over SSH, and then if changed, moving the dump to the new environment and undumping it.

I've rewritten it to obscure sensitive information, but here is the gist of this script I've written up to the point where I'm having the issue:

#!/bin/bash

mysqldump -h localhost testDB > dbPath/testdb.sql

hash1=$(md5sum dbPath/testdb.sql) | awk '{print $1}'

echo $hash1

When executing the script :

sudo ./testScript.sh

I see the mysqldump created with the permissions -rw-r--r-- 1 root root which seems correct to me, however, as the script continues to the md5 hash, I get this:

./testScript.sh: line 5: dbPath/testdb.sql: Permission denied

When I execute the md5sum command from the shell (not in the script), it works fine, even from my normal user without using sudo. When I change into root and execute the command from the shell, it works correctly. When I run the script in any capacity (from my user account, sudo from my user account, or from root directly), I get the permission denied error on the md5sum line.

I would think that somewhere my user account's permissions were bleeding over instead of root's permissions being used, except for the fact that the script cannot be executed by root from root's shell without also getting a permission denied error, and as far as I can tell, root shouldn't have permission denied to anything. As a test, I threw a whoami before and after the md5sum command and both commands output root as the user when executing with sudo or with root.

Root and my user account clearly both have permission to execute md5sum on the file; how is the fact that this is running from a script making a change to my or root's abilities to execute commands or manipulate the file?

Environment is RHEL 6.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images