Use of Export in Bash Script on CentOS
Solution 1
You're executing the script by running "sh myscript.sh". If you invoke bash with "sh", bash will try "to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well" (according to the man page for bash, in the Invocation section).
The historic Bourne shell doesn't recognize "export VARIABLE=value". The typical way of exporting variables in Bourne shell is "VARIABLE=value ; export VARIABLE", i.e., the setting of the value and the export are separate commands. Doing it all as one command is a bash-ism.
So, you can try invoking your script as "bash myscript.sh", which should work (it works for me, though admittedly it also works with the "sh myscript.sh" invocation; there might be something funny with my shell settings somewhere, or something funny in yours). You can also make the file executable with "chmod +x myscript.sh" and just run it directly with "./myscript.sh" since it should invoke bash, according to the "#!" in the first line.
Solution 2
It appears your script has an erroneous carriage-return at the end of each line. This can happen if at some point in its lifetime the script has been edited on a Windows system. You can try to repair it using this command:
perl -pi -e 's/\r//' myscript.sh
Because of the erroneous carriage-return it is not referencing the directory rds
like you intended but instead rds
followed by a carriage-return. You presumably don't have a directory with that name, and assuming rds-delete-db-instance
lives inside the rds
directory, that explains why you get command not found
.
There are a few other issues with your script which I have tried to fix here:
#!/bin/bash
# script to restore the cognos rds from snapshot
export AWS_RDS_HOME=/opt/aws/apitools/rds
export PATH="$PATH:$AWS_RDS_HOME/bin"
export AWS_CREDENTIAL_FILE="$AWS_RDS_HOME/credential-file-path.template"
echo "$AWS_RDS_HOME"
echo "$PATH"
echo "$AWS_CREDENTIAL_FILE"
rds-delete-db-instance mydb --final-db-snapshot-identifier "mydb-daily-$(date +%Y-%m-%d)" --force -region eu-west-1
I removed the space after #!
because it is usually not used, and I believe the script is slightly more portable without the space.
I added "
around strings in which $
characters are used. The "
are significant in case the expansion produced using $
contains any special characters including spaces.
Additionally, I changed the three date
commands into a single. Using a single date
command is shorter and more robust in case the script happens to be started just before midnight.
Related videos on Youtube
alan
Updated on September 18, 2022Comments
-
alan over 1 year
I am very new to Linux and am having problems with what should be a very simple Bash script in CentOS.
#! /bin/bash # script to restore the cognos rds from snapshot export AWS_RDS_HOME=/opt/aws/apitools/rds export PATH=$PATH:$AWS_RDS_HOME/bin export AWS_CREDENTIAL_FILE=$AWS_RDS_HOME/credential-file-path.template echo $AWS_RDS_HOME echo $PATH echo $AWS_CREDENTIAL_FILE rds-delete-db-instance mydb --final-db-snapshot-identifier mydb-daily-$(date +%Y)-$(date +%m)-$(date +%d) --force -region eu-west-1
I added the echo statements so I could see what was going on. The output when I run with
sh myscript.sh
is:
/opt/aws/apitools/rds /bin/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin:/opt/aws/apitools/rds /credential-file-path.template rds-delete-db-instance: command not found
So it would appear that the export commands are not working when I reference other environment variables which have been set in the same script.
Where am I going wrong?
Thanks
Update: I've tried the suggestions below but stll no joy. Running bash -x was interesting. Could the "\r"s below whenever I try to concatenate be behind this? e.g.
#! /bin/bash # test script export AWS_RDS_HOME=/opt/aws/apitools/rds export PATH=$PATH:$AWS_RDS_HOME/bin export AWS_CREDENTIAL_FILE=$AWS_RDS_HOME/credential-file-path.template echo $AWS_RDS_HOME echo $PATH echo $AWS_CREDENTIAL_FILE
Results in:
+ export $'AWS_RDS_HOME=/opt/aws/apitools/rds\r' + AWS_RDS_HOME=$'/opt/aws/apitools/rds\r' + export $'PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin:/opt/aws/apitools/rds\r/bin\r' + PATH=$'/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin:/opt/aws/apitools/rds\r/bin\r' + export $'AWS_CREDENTIAL_FILE=/opt/aws/apitools/rds\r/credential-file-path.template\r' + AWS_CREDENTIAL_FILE=$'/opt/aws/apitools/rds\r/credential-file-path.template\r' + echo $'/opt/aws/apitools/rds\r\r' /opt/aws/apitools/rds + echo $'/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin:/opt/aws/apitools/rds\r/bin\r\r' /usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home//binuser/bin:/opt/aws/apitools/rds + echo $'/opt/aws/apitools/rds\r/credential-file-path.template\r' /credential-file-path.template
I'm using the default Amazon Linux AMI on EC2 so there shouldn't be anything funky with my settings
-
gokva about 12 yearsyou are missing . from your path, so the script cant see the executable. try
which rds-delete-db-instance
-
Navern over 8 yearsRun rds-delete-db-instance command in your script with the full path to executable. Where is binary/script "rds-delete-db-instance" is placed?
-
-
Phil Hollenback over 12 yearsI think this is generally the correct answer. One thing you should try doing is running your script with
sh -x script
andbash -x script
and comparing the output. -
flolo over 12 yearsI also thought first that it could be this, but the poster is executing his command, which misses the exports, in the last line of the script itself. (Btw. that "dot" is short for the
source
command). -
alan over 12 yearsThis didn't solve it. The bash -x command revealed something interesting though, which I'm hoping may point to a solution. See the edit above.
-
cjc over 12 yearsHuh. So, if you type "file myscript.sh" tell you that it's a binary file rather than a bash script? What did you edit this file with? In any case, strip out the hidden characters. If you used a Windows machine to create the file, you might be able to use "dos2unix myscript.sh". Alternatively, if you use "vi -b myscript.sh", vi will show you the hidden characters.
-
kasperd over 8 yearsThere appears to be no connection between the question and your answer. I suspect you are trying to solve an unrelated problem. I don't think this answer adds any value because we don't know which problem it is supposed to apply to.