Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

3 Answers 3

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

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

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.