0

I am learning the shell language. I have creating a shell script whose function is to login into the DB and run a .sql file. Following are the contents of the script -

#!/bin/bash
set -x

echo "Login to postgres user for autoqa_rpt_production"
$DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT

echo "Running SQL Dump - auto_qa_db_sync"
\\i auto_qa_db_sync.sql

After running the above script, I get the following error

./autoqa_script.sh: 39: ./autoqa_script.sh: /i: not found

Following one article, I tried reversing the slash but it didn't worked.

I don't understand why this is happening. Because when I try manually running the sql file, it works properly. Can anyone help?

1 Answer 1

4
#!/bin/bash
set -x

echo "Login to postgres user for autoqa_rpt_production and run script"
$DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT -f auto_qa_db_sync.sql

The lines you put in a shell script are (moreless, let's say so for now) equivalent to what you would put right to the Bash prompt (the one ending with '$' or '#' if you're a root). When you execute a script (a list of commands), one command will be run after the previous terminates.

What you wanted to do is to run the client and issue a "\i ./autoqa_script.sh" comand in it.

What you did was to run the client, and after the client terminated, issue that command in Bash.

You should read about Bash pipelines - these are the way to run programs and input text inside them. Following your original idea to solving the problem, you'd write something like:

echo '\i auto_qa_db_sync.sql' | $DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT

Hope that helps to understand.

Sign up to request clarification or add additional context in comments.

2 Comments

@ mkf that really helped a lot. but just for curiosity what was going wrong with the approach that I was trying. Can you please explain?
@user1305398 I have added an explanation to my post.

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.