I am running some commands to mongodb in a bash script but need to insert a string into a heredoc text. I am unable to get the value inserted correctly. How would this be done?
today=`date -d "00:00:00" +%s`
todaytime=$(($today*1000))
mongo <<EOF > test
use log
db.translogs.remove("{Dt: {$lt: new Date($todaytime)}}")
exit
EOF
I don't know mongodb, but it looks like $lt is part of a query and not a shell variable. But in your code, the shell is trying to expand it before passing the here-doc to mongo. So I think all you need to do is escape the $ in $lt:
today=`date -d "00:00:00" +%s`
todaytime=$(($today*1000))
mongo test
use log
db.translogs.remove("{Dt: {\$lt: new Date($todaytime)}}")
exit
EOF
If I replace mongo with cat in the above code, we see the commands-to-be-sent to mongo in the test file as:
use log
db.translogs.remove("{Dt: {$lt: new Date(1393315200000)}}")
exit
Related
I have the following code -
ssh host sh -s << EOF
cd /backup/
ls
psql -U user -d db << SQL
$(sed 's/${previous_quarter}/${current_quarter}/' table_quarters.sql);
$(sed 's/${previous_quarter}/${current_quarter}/' plans.sql);
SQL
EOF
This is the order of execution it is following when I execute it -
table_quarters script
plans script
ls command.
Why is it not following this order of execution -
ls command
table_quarters script
plans script
You are sending a string to the standard input of ssh. This string can only be constructed if the stuff in $(...) is run first, because these parts will be replaced by the output of the enclosed commands.
Once the string is expanded, it's send by ssh to the remote machine which runs the resulting commands in order.
If you want to run the expansions on the remote end, you need to properly escape the dollar signs.
I have a text file of roughly 900 cURLs to run. They are pretty hairy, with tons of quotes, apostrophes and other special characters.
To run them I have been trying to create a bash script to loop through the list:
#!/bin/sh
OLDIFS=$IFS
IFS="&&&"
echo "getting started"
cat staging_curl_script|while read line
do
$line
done
echo "done"
Unfortunately I have had an unusual issue. commands that run fine in the command prompt are returning the "file name too long" error. I echoed out these commands from the script and compared them to the manually run command, and they are identical.
Any idea why I am seeing different results?
silly mistake here, needed bash -c "$line"
I am trying to use the result printed from a parameterized MongoDB script file in a bash script.
The call looks like this:
mongo --quiet server/db --eval "a='b'" mongoscript.js
Inside mongoscript.js there is a print statement that prints the value 'foo' I want to use in my shell script. The problem is that when I execute above statement I get:
b
foo
instead of just 'foo'
Thus, if I do
res=`mongo --quiet server/db --eval "a='b'" mongoscript.js`
res contains both lines.
I can of course solve this with
res=`mongo ... |tail -n 1`
but I am hoping there is a more general way to avoid this superfluous output.
Thanks!
The superfluous output is the result of your assignment of a='b', which displays the result of the assignment in this context.
If you add the var keyword for variable assignment, you shouldn't have any extra output (and can still use the variable a in your script):
$ mongo --quiet --eval "var a='b'" mongoscript.js
foo
You can see the same behaviour in the mongo shell:
> a='b'
b
> var a='b'
>
I am querying a database from a bash script using following query:-
Output = echo "$QUERY_STR" | mysql -h $DB_HOST -u $DB_USER -p$DB_PASS $DB_NAME
it gives me the required output which I save in a Variable
However when I echo $output I do not get proper formatted output like in command line of mysql query.
Read one of the post to use -t in the query however for large data set it does not give proper output.
To work around it, I am saving the output in a .csv file.
To maintain all the whitespace that is indeed kept in the variable's value, it is crucial to double-quote the variable:
echo "$output"
Also, you cannot have whitespace around the equal sign in a variable assignment:
output=$(mysql ... <<< "$QUERY_STR")
As part of a system build script I have a script that creates various files and configurations.
However one part of the build script creates a new script that contains variables that I don't want resolved when the build script runs. Code snippet example
cat - > /etc/profile.d/mymotd.sh <<EOF
hostname=`uname -n`
echo -e "Hostname is $hostname"
EOF
I have tried all sorts of combinations of ' and " and ( and [ but I cannot get the script to send the content without substituting the values and placing the substitutes in the new script rather than the original text.
Ideas?
The easiest method, assuming you don't want anything to be substituted in the here doc, is to put the EOF marker in quotes, like this:
cat - > /etc/profile.d/mymotd.sh <<'EOF'
hostname=`uname -n`
echo -e "Hostname is $hostname"
EOF
Easiest is to escape the $
echo -e "Hostname is \$hostname"