1

Okay, so I am new to bash scripting, about 2 whole hours and I'm banging my head against a wall. I need to carry some variables from one script to another and can't seem to get it to work.

This works fine

script1.sh

echo "Enter your name"
read name
export name
./script2.sh

sript2.sh

echo $name

This does not

script1.sh

echo "Enter your name"
read name
export name
ssh $user@$domain "bash -s" < ./script2.sh

sript2.sh

echo $name

The code is dumb, I get that but basically what I want to do is pass database variables to script 2 to use on the server I've SSH'd into. I would do this all in one script but it seems the only way that I can get the server commands to run is to call a second script after the login, otherwise it runs the server commands locally and fails. I'm doing this from a Mac if that makes any difference at all.

Help?

** Update with more legitimate code ****

login.sh

#!/bin/bash
echo "Enter server username:"
read user
echo "Enter server domain"
read domain
echo "Enter your password"
read password
echo "Enter database name"
read database
export database
export user
export domain
export password
ssh $user@$domain "bash -s" < ./create_files.sh 

create_files.sh

#!/bin/bash
cd public_html
tar -zcvf test.tar.gz test.html
echo "db:"$database
mysqldump -u $user -p"$password" $database > $database.sql

The goal here is to use the details from login.sh in create_files.sh after this is done I also need to use the login details to scp the created files back to my local machine. Hope this clarifies the problem.

1 Answer 1

2

When invoking ssh, it creates a new shell process which is not inheriting the new variables that you exported in the first script. Instead, try adding them as commandline parameters to the second script, like so:

#!/bin/bash
echo "Enter server username:"
read user
echo "Enter server domain"
read domain
echo "Enter your password"
read password
echo "Enter database name"
read database
ssh $user@$domain "bash -s" < ./create_files.sh "$user" "$password" "$database"

Then modify your second script to look like this:

#!/bin/bash
cd public_html
tar -zcvf test.tar.gz test.html
echo "db:$3"
mysqldump -u "$1" -p"$2" "$3" > "$3".sql
Sign up to request clarification or add additional context in comments.

10 Comments

I'm trying to make it a little easier to retrieve files from client servers. So basically what the script is supposed to do is ask for server details ie the database name, user and password. Then log in via SSH and mysqldump the database.
Ah, right. I think you might want to look into expect scripting. The code above will likely try and dump the $name variable into the password field when trying to log on, unless you're using key based authentication?
Nope no key based, just user/pass. I'll read about expect then. Thanks!
If you update your question with these details, I'll try and update the answer somewhat.
I'll play a little more and come back if I stay stuck. It's a side project so I can muck about in downtime. Thanks for your answer, I upped it rather than accepting just because I don't actually have a solution yet (my fault lol)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.