Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
    
you are missing . from your path, so the script cant see the executable. try which rds-delete-db-instance – Tom H Feb 27 '12 at 6:22
    
Run rds-delete-db-instance command in your script with the full path to executable. Where is binary/script "rds-delete-db-instance" is placed? – Navern Dec 29 '15 at 18:38

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.

share|improve this answer
    
I think this is generally the correct answer. One thing you should try doing is running your script with sh -x script and bash -x script and comparing the output. – Phil Hollenback Aug 12 '11 at 1:18
    
This 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. – alan Aug 14 '11 at 17:17
    
Huh. 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. – cjc Aug 15 '11 at 14:22

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.

share|improve this answer

Hrm, try enclosing the bit to the right of the = with double (") quotes? It worked both with and without on my little test here. Although it's not on CentOS, but bash should behave the same on different linuxes, I would think.

share|improve this answer

When you type

sh myscript.sh

you're spawning a new shell with a new set of environment variables. Inside that shell the variable $AWS_RDS_HOME will contain the value "/opt/aws/apitools/rds", but that context goes away when sh exits.

It sounds like you want to use

. myscript.sh

That's "dot", followed by "space", followed by the filename. The "dot" command interprets the argument file in the context of current shell, thus the exported variables are subsequently available in the current shell.

share|improve this answer
    
I 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). – flolo Aug 12 '11 at 5:07

This is the only way it worked for me in Centos/using bash.

source myscript.sh

or

. myscript.sh
share|improve this answer
1  
There 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. – kasperd Dec 29 '15 at 18:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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