Is it possible to store mongo output into a variable in shell script, below is the example query which prints decimal date
ex: 1489442900000
maxdate =$( echo mongo getmaxdate.js --quiet)
Any help is highly appreciated.
Thanks,
Ashwin.
There should be no space between the variable name and the command during the assignment.
variable_name=$(command1 |command2 .....)
In your case:
maxdate=$( mongo getmaxdate.js --quiet)
Make sure to preserve the formatting of the output. if echo'ed Without double quote the whole content will show up in one line.
echo "$maxdate"
Related
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'"
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 a filename that is formated as electric_inventory_WE_20170730_195758.dat. There is a similar file created each day. The time created may vary. I need to capture the filename with today's date, not worry about the time and end with a .dat extension.
I have created a variable for today's date. I need to use this variable inside a string inside back ticks. Something like this:
my $today = `date "+%Y%m%d"`;
my $filename = `ls electric_inventory_WE_$today*.dat`;
I cannot find a way to get this variable to work inside a string inside the back ticks. Any help would be greatly appreciated.
That is exactly how you use a variable in backticks.
I'm not sure what the actual problem is ("I cannot find a way to get this to work" is not a helpful problem description), but here's my guess:
`date ...` returns a string with a trailing newline, "\n", so the command you're actually running ends up being:
ls electric_inventory_WE_20170330
*.dat
A possible solution is to avoid using the shell (and external commands such as date and ls) at all:
use POSIX qw(strftime);
my $today = strftime "%Y%m%d", localtime;
my #candidates = glob "electric_inventory_WE_$today*.dat";
if (#candidates != 1) {
... # handle error: either 0 or more than 1 files were found
}
my $filename = shift #candidates;
Thanks to all that provided information on this and I do apologize for the cross posts. Thanks to your posts, I was able to use the following to get this to work:
my $filename="electric_inventory_WE_" . $today . "*.dat";
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")
I have a bash script I am having some issues with concatenating 2 variables to call a 3rd.
Here is a simplification of the script, but the syntax is eluding me after reading the docs.
server_list_all="server1 server2 server3";
var1 = "server";
var2 = "all";
echo $(($var1_list_$var2));
This is about as close as I get to the right answer, it acknowledges the string and tosses an error on tokenization.
syntax error in expression (error token is "server1 server2 server3....
Not really seeing anything in the docs for this, but it should be doable.
EDIT: Cleaned up a bit
The Bash Reference Manual explains how you can use a neat feature of parameter expansion to do some indirection. In your case, you're interested in finding the contents of a variable whose name is defined by two other variables:
server_list_all="server1 server2 server3"
var1=server
var2=all
combined=${var1}_list_${var2}
echo ${!combined}
The exclamation mark when referring to combined means "use the variable whose name is defined by the contents of combined"
The Advanced Bash Scripting Guide has the answer for you (http://tldp.org/LDP/abs/html/ivr.html). You have two options, the first is classic shell:
#!/bin/bash
server_list_all="server1 server2 server3";
var1="server";
var2="all";
server_var="${var1}_list_${var2}"
eval servers=\$$server_var;
echo $servers
Alternatively you can use the bash shortcut ${!var}
#!/bin/bash
server_list_all="server1 server2 server3";
var1="server";
var2="all";
server_var="${var1}_list_${var2}"
echo ${!server_var}
Either approach works.