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:


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.


The underlying problem is the parse error on this line:

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

which should almost certainly be

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

You can check for errors like this at https://shellcheck.net

  • Shellcheck.net seems like an awesome tool that I'm going to add that to my list of tools. I came from a Windows environment to this position, so everything I can get to add to my arsenal helps. From my understanding of Bash, though, which is clearly limited, shouldn't I be able to do $(command 1) | command 2 or $(command 1 | command 2) or $(command 1) | $(command 2)? Just made the change to my script and it works, I just don't have any idea why. – SSS Jan 29 '18 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.