RicoRicochet
RicoRicochet

Reputation: 2289

Error while running a .sh script via QProcess

I have written a QT GUI program where pressing a button will execute a .sh script. The contents of the script is-

echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import ora_exported.csv qt_ora_exported' | sqlite3 testdatabase.db

basically the script will import a .csv to an sqlite database. And when the script file (script.sh) is run manually from linux terminal ($./script.sh) it successfully imports the .csv file into the database table.

But, when I call the script from my QT program

void MainWindow::on_importButton_clicked()
{    
    QProcess process;
    process.startDetached("/bin/sh",QStringList()<<"/home/aj/script.sh");
}

it compiles successfully but gives an error message in console when the button is pressed at runtime.

Error: near line 1: near "-": syntax error Error: cannot open "ora_exported.csv"

what could be causing this ???

EDITED

I have changed my .sh script now to--

echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import /home/aj/ora_exported.csv qt_ora_exported' | sqlite3 testdatabase.db

Thus providing the path to my ora_exported.csv. As a result the runtime error [Error: cannot open "ora_exported.csv"] has gone but the other message [Error: near line 1: near "-": syntax error] is still coming.

Same as was observed in previous case, using ./script.sh is successfully importing data to sqlite3 db table file but QProcess is unable to.

Upvotes: 2

Views: 1678

Answers (2)

Simon Warta
Simon Warta

Reputation: 11408

echo is a built in command of a shell that may behave differently.

E.g. take this test script: echotest.sh

echo -e "123"

Now we can compare different results:

$ bash echotest.sh
123
$ zsh echotest.sh
123
$ dash echotest.sh
-e 123

You are probably on some Ubuntu-like OS, where /bin/sh redirects to dash. That would explain the error around "-". So if you are using echo, set you shell specificially or ensure that your script works on all common shells.


Additionally, you are messing up your quotations

echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import /home/aj/ora_exported.csv qt_ora_exported'

results in (no quotations in the first line)

attach database testdatabase.db as aj;
.separator ","
.import /home/aj/ora_exported.csv qt_ora_exported

but you pobably want

echo -e "attach database 'testdatabase.db' as 'aj';\n.separator ','\n.import /home/aj/ora_exported.csv qt_ora_exported"

Upvotes: 4

Marek R
Marek R

Reputation: 37697

  1. It looks strange that you are using external script to update database!
  2. why you don't pass "ora_exported.csv" file name as a script argument? This would help solve the problem.
  3. I was talking (typing) about this solution:
void MainWindow::on_importButton_clicked()
{    
    QProcess::startDetached("/bin/sh",
                            QStringList()<<"/home/aj/script.sh",
                            "<location of: 'ora_exported.csv' file>");
}

Upvotes: 3

Related Questions