I'm pretty new to shell scripts and am only doing them because its about time I learnt and I need to for work.
I have been looking around and have tried multiple methods to get this working but can't seem to figure it out.
I have a script in which I want to access an SQLite database and store the result of a select statement in a variable.
What I've Tried So Far
This one just echoes whats inside the apostrophe. If I remove the dollar sign before the apostrophe I get the same outcome.
track_name=$'sqlite3 "$database_name" << EOF
select name from track where id = "$required_track";
exit;
EOF'
Here I get a syntax error near "track_name"
sqlite3 "$database_name" << EOF
track_name='select name from track where id = "$required_track";'
exit;
EOF
I have successfully executed the select statement without trying to store it in a variable but its not much use to me without being able to store it...
Any help would be much appreciated
To store the output of a command into a BASH variable you should use:
VAR_NAME=$(command);
For example, if you want to store your system current time into a variable or
the results of a list directory command ejecution:
DATE_EXAMPLE_VAR=$(date); #Stores 'date' command output into DATE_EXAMPLE_VAR
echo $DATE_EXAMPLE_VAR; #Shows DATE_EXAMPLE_VAR contents
DIRCONTENTS=$(ls); #Stores a list of your current directory contents.
Similarly, this should work for sqlite3:
track_name=$(sqlite3 "$database_name" "select name from track where id = $required_track")
Related
Part of script in the code, below, is work partial to me, in another words, executed samba-tool command and bring me result on the screen (python3 script.py). BUT, the value of process (variable) is all none.
I tried/read documentations, but without success to store and fix it.
from subprocess import Popen,PIPE, STDOUT
def run_command():
process = Popen(['samba-tool', 'dns','query','chaps.xpto.local','xpto.local','#','ALL','-U','auditor'], stdin=PIPE).communicate(input=b'#!3202#otpX')
print(process)
run_command()
~
I tried:
outs, errs = Popen(['****')
print (outs)
or
print (process.stdout)
I tried to use StringIO to capturing, but sysout of command Popen doesn't record tool
Thank you Ryanwebjackson.
After days suffer with it, I notice that problem is only for syntax samba-tools to query dns information, because the arguments need to inform login/pwd to query and if try capture
output doesn´t works, the utility not understands a valid credetials/bad password.
If i try the syntax to use with other utility or shell commands work well. I decided change the approach and replace :
process = Popen(['samba-tool', 'dns','query','chaps.xpto.local','xpto.local','#','ALL','-U','auditor'], stdin=PIPE).communicate(input=b'#!3202#otpX')
For :
process = subprocess.getoutput("(($(dig +noall +answer "+host+"."+domain+" |wc -c)>0)) && echo exist")
I had the list of host to test if there's records on DNS and zone and voila! fixed.
I want to store the value of the sqlite statement in a variable
backup=$(sqlite3 "/home/miguel/Desktop/SO/ProjetoFinal/Backup_Principal.db" "SELECT periocidade_backup FROM STORAGE WHERE path'$path';")
But when i echo $backup it returns the following:
sqlite3 "/home/miguel/Desktop/SO/ProjetoFinal/Backup_Principal.db" "SELECT periocidade_backup FROM STORAGE WHERE path='$path';"
What am I doing wrong?
the part of your code '$path' is using a single quote which is literal and show exactly as what is in the quotes, which would not use the variable's value. using speech marks like the following should work, "'$path'"
I have file1.sh file and which internally needs to execute one sql query against two Oracle DBs at a same time and needs to export date to csv fiiles, below is the sample shellscript which executes the query against two dbs.
....
#!bin/bash
set -X
sqlplus -S ${user1}#${DBCONNECTIONNAME_1}/${Pwd} Datesquery.sql & >> ${Targetdirectory}/csvfile1.csv
sqlplus -S ${user1}#${DBCONNECTIONNAME_2}/${Pwd} Datesquery.sql & >> ${Targetdirectory}/csvfile2.csv
sed 1d csvfile2.csv > file2noheader.csv
cat csvfile1.csv file2noheader.csv > ${Targetdirectory}/Expod.csv
....
But it does not connect to DB and does not execute any query and simply displays sqlplus manual as how to use the connection string, please let me know how to call one query against two dbs and execute them in parrallay and binds output to separate csv files.
A given sqlplus session can only connect to one db at a time, so your requirement 'at the same time' is essentially a non-starter. If 'at the same time' really means 'sequentially, in the same script, then you are back to fixing your connect string. And at that you 'have more errors than an early Mets game' (with apologies to any NY Mets fans).
First, your script indicates that your sqlplus command is the very first actual command following specification of your shell processor and 'set -x'. Yet you make heavy use of environment variables as substitutions for username, password, and connection name - without ever setting those variables.
Second, your use of an '&' in the command line is totally confusing to both me and the parser.
Third, you need to preceed your reference to the sql script with '#'.
Fourth, your order of elements in the command line is all wrong.
Try this
#!/bin/bash
orauser1=<supply user name here>
orapw2=<supply password here>
oradb_1=<supply connection name of first database>
#
orauser1=<supply user name here>
orapw2=<supply password here>
oradb_1=<supply connection name of first database>
#
Targetdirectory=<supply value here>
#
sqlplus -S ${orauser1}/${orapw1}#${oradb_1} #Datesquery.sql >> ${Targetdirectory}/csvfile1.csv
sqlplus -S ${orauser2}/${orapw2}#${oradb_1} #Datesquery.sql >> ${Targetdirectory}/csvfile2.csv
Or create a database link form one DB to other and then run both sqls in one db, one over DB link.
select * from tab1
union
select * from tab1#db_link
declare -a result=`$ORACLE_HOME/bin/sqlplus -silent $DBUSER/$DBPASSWORD#$DB << EOF $SQLPLUSOPTIONS $roam_query exit; EOF`
I am trying to pull data from an oracle database and populate a bash variable. The select query works however it returns multiple rows and those rows are returned as a long continuous string. I want to capture each row from the database in an array index for example:
index[0] = row 1 information
index[1] = row 2 information
Please help. All suggestions are appreciated. I checked all documentation without no luck. Thank you. I am using solaris unix
If you have bash version 4, you can use the readarray -t command to do this. Any vaguely recent linux should have bash v4, but I don't know about Solaris.
BTW, I'd also recommend putting double-quotes around variable references (e.g. "$DBUSER/$DBPASSWORD#$DB" instead of just $DBUSER/$DBPASSWORD#$DB) (except in here-documents), using $( ) instead of backticks, and using lower- or mixed-case variable names (there are a bunch of all-caps names with special meanings, and if you use one of those by accident, weird things can happen).
I'm not sure I have the here-document (the SQL commands) right, but here's roughly how I'd do it:
readarray -t result < <("$oracle_home/bin/sqlplus" -silent "$dbuser/$dbpassword#$db" << EOF
$sqlplusoptions $roam_query
exit;
EOF
)
I have the following scenario which is working fine on command line
value=`cat /root/abc`
echo $value
which is printing the content of the file abc
now same I want to perform using Expect
I did as follow
send "value=`cat /root/abc`\r"
now I want to capture the value in some variable as follow
set A_Value = $value
but getting error can't read "value": no such variable
I am very new to expect and trying it first time, So could anyone please let me know how to achieve the same.
Thanks
Keep in mind that expect is just a robot typing the keys for you. Expect does not have any special insight into the inner workings of the process it is automating.
You are stuck doing something like this: capture the output excluding the command you sent and the prompt:
send "echo \"\$value\"\r"
expect -re "echo \[^\r]+\r\n(.*)\r\n$prmpt $"
set file_contents $expect_out(1,string)