Variable in bash script not working as expected
I have this script:
#!/bin/bash
VAR="eric.sql"
sudo mysqldump -c -u username -p1234 dbname > $VAR
But if I run this script, I get this error:
: Protocol error 3: mysql-export.sh: cannot create eric.sql
But if I don't use this variable, but only this:
#!/bin/bash
VAR="eric.sql"
sudo mysqldump -c -u username -p1234 dbname > eric.sql
... it works well. What am I wrong?
+3
source to share
2 answers
sudo
may change the $ PATH variable, depends on your security policy.
-E The -E (preserve environment) option will override the env_reset
option in sudoers(5)). It is only available when either the match-
ing command has the SETENV tag or the setenv option is set in sudo-
ers(5).
You can add the full path to the file or remove it sudo
in your script. This should work as well:
sudo PATH="$PATH" mysqldump -c -u username -p1234 dbname > $VAR
-2
source to share