Pass Bash argument in sqlite3 string command [duplicate] - bash

This question already has answers here:
Expansion of variables inside single quotes in a command in Bash
(8 answers)
Closed 4 years ago.
I have the following command which does not work:
sqlite3 my_db.sqlite "SELECT name FROM sqlite_master WHERE type = 'table';" | for i in $(cat) ; do sqlite3 my_db.sqlite 'SELECT * FROM "${i}"'; done
To explain it quickly: the first part below is supposed to retrieve the table names from a sqlite file that I have:
sqlite3 my_db.sqlite "SELECT name FROM sqlite_master WHERE type = 'table';"
And this part is supposed to display the entire content of each table recursivelyin the stdout:
for i in $(cat) ; do sqlite3 my_db.sqlite 'SELECT * FROM "${i}"'; done
The problem is that I have no idea how I am supposed to pass i to the sqlite command. I tried with "${i}" but obviously it is interpreted as a classic string to find a matching table name, and just return Error: no such table: ${i}
How should I pass i ?
Thank you in advance for your help.

You must use double quotes around the query to allow bash to recognize the variable. This implies that you have to correctly escape the double quotes that are part of the SQL statement:
... | for i ... ; do sqlite3 my_db.sqlite "SELECT * FROM \"${i}\""; done

Related

Store a SQLite query with parameters in bash variable [duplicate]

This question already has answers here:
How to prepare a statement from the CLI interpreter?
(2 answers)
How to escape single quotes within single quoted strings
(25 answers)
Closed 1 year ago.
I am trying to store a list of rows inside of a variable like in this example:
list=$(bash -c 'sqlite3 /home/ubuntu/myplace/mydb.db "SELECT row1 FROM table1"')
But when I want to add parameters like in this example:
list=$(bash -c 'sqlite3 /home/ubuntu/myplace/mydb.db "SELECT row2 FROM table2 WHERE row2='somevalue'"')
It does not work because the command given to bash inside the '' ends after the WHERE row2=.
I tried some other variants where I first wrote the results of
sqlite3 /home/ubuntu/myplace/mydb.db "SELECT row2 FROM table2 WHERE row2='somevalue'"
to some txt, csv or json file just to read it out one line later but that just came out to be not practical.
Is there any other practical possibility?

Executing sqlite3 query in bash

Executing in bash
sqlite3 database.db 'select * from databases'
gives me good output, but unfortunately when I would like to create a query from a variable sqlite3 doesn't want to cooperate.
For example:
cmd="select * from databases"
cmd1="sqlite3 database.db"
cmd2="'select * from databases'"
echo $($cmd1 \'$cmd\')
echo $($cmd1 '$cmd')
echo $($cmd1 $cmd)
echo $($cmd1 $cmd2)
echo `$cmd1 $cmd2`
None of above works. I would like to have a function which would execute just query like "select * from databases".
Your trouble is word splitting. When you run this:
sqlite3 database.db $sql
The shell splits $sql to words. But sqlite3 expects the SQL command as a single parameter. To prevent word splitting, you must enclose $sql in double-quotes:
sqlite3 database.db "$sql"
If you want to store the sqlite3 database.db in a variable called cmd,
you can, and write:
$cmd "$sql"
The shell will split $cmd to words, and in this case you want that.
You want the shell to see the sqlite3 command, and another word database.db as the first argument. That's way we don't enclose $cmd in double-quotes.
Let's try this approach:
cmd1="sqlite3 database.db"
cmd2="select * from databases"
$cmd1 "$cmd2"
I've just tried - it works for me:

Concatenating asterisks in bash [duplicate]

This question already has answers here:
Printing asterisk ("*") in bash shell
(2 answers)
Closed 7 years ago.
I am trying to concatenate asterisks (*) to a string variable. However, I keep getting the files in the current directory instead. I have tried
row+='*'
row+=$row #where row is *
row='*'"$row"
row="\*$row"
row="${row}*"
etc.
row='*'."$row" #produces *.*.*.
I thought \ would escape the *, but it didn't work.
Try using quotes around the variable before you "print".
For example:
cronSen="*/$a * * * * bash /etc/init.d/ckDskCheck.sh"
echo "$cronSen"
This could work. I got the idea from here: Printing asterisk (*) in bash shell

passing argument from shell script to hive script

I've a concern which can be categorized in 2 ways:
My requirement is of passing argument from shell script to hive script.
OR
within one shell script I should include variable's value in hive statement.
I'll explain with an example for both:
1) Passing argument from shell script to hiveQL->
My test Hive QL:
select count(*) from demodb.demo_table limit ${hiveconf:num}
My test shell script:
cnt=1
sh -c 'hive -hiveconf num=$cnt -f countTable.hql'
So basically I want to include the value of 'cnt' in the HQL, which is not happening in this case. I get the error as:
FAILED: ParseException line 2:0 mismatched input '<EOF>' expecting Number near 'limit' in limit clause
I'm sure the error means that the variable's value isn't getting passed on.
2) Passing argument directly within the shell script->
cnt=1
hive -e 'select count(*) from demodb.demo_table limit $cnt'
In both the above cases, I couldn't pass the argument value. Any ideas??
PS: I know the query seems absurd of including the 'limit' in count but I have rephrased the problem I actually have. The requirement remains intact of passing the argument.
Any ideas, anyone?
Thanks in advance.
Set the variable this way:
#!/bin/bash
cnt=3
echo "Executing the hive query - starts"
hive -hiveconf num=$cnt -e ' set num; select * from demodb.demo_table limit ${hiveconf:num}'
echo "Executing the hive query - ends"
This works, if put in a file named hivetest.sh, then invoked with sh hivetest.sh:
cnt=2
hive -e "select * from demodb.demo_table limit $cnt"
You are using single quotes instead of double.
Using double quotes for OPTION #1 also works fine.
hadoop#osboxes:~$ export val=2;
hadoop#osboxes:~$ hive -e "select * from bms.bms1 where max_seq=$val";
or
vi test.sh
#########
export val=2
hive -e "select * from bms.bms1 where max_seq=$val";
#####################
Try this
cnt=1
hive -hiveconf number=$cnt select * from demodb.demo_table limit ${hiveconf:number}

How to create a "one-liner" for oracle that includes "set" commands as well as sql statements

I want to execute a dynamic sql containing some set commands. Is it possible to do so without embedding newlines?
set heading off ; set lines 1000 ; select * from my_table;
Note the above does not work due to the semicolons between the set commands:
SP2-0158: unknown SET option ";"'
Update The whole point of this question is to do it on one line.
The best I have found for my own purposes is to put my standard SET commands in a file called sql_settings.txt in a directory with an environment variable holding its path and another variable for the connect string:
sqlsets=/directory/where/sql_settings/stored/sql_settings.txt
db_conn=<ConnectStr>
& then execute a one-liner as such with a shell here-string:
sqlplus -s $db_conn #$sqlsets <<< "select * from my_table;" | less
(The "less" pipe will prevent from cluttering your shell session)
You could also get fancy and create a shell function to minimize typing to the SQL query:
function mydb { sqlplus -s $db_conn #$sqlsets <<< "$#;" ; }
Then call as such:
mydb 'select * from my_table;'
set command is a directive for sqlplus and is not related to sql and you can do it this way
set heading off lines 1000
select * from my_table;
After extensive research, I have concluded this is not possible to perform with oracle.

Resources