How to execute mongo commands through shell scripts?

I want to execute mongo commands in shell script, e.g. in a script test.sh: #!/bin/sh mongo myDbName db.mycollection.findOne() show collections When I execute this script via ./test.sh, then the connection to MongoDB is established, but the following commands are not executed. How to execute other commands through shell script test.sh? 21 Answers 21 You can … Read more

How can I split a shell command over multiple lines when using an IF statement?

How can I split a command over multiple lines in the shell, when the command is part of an if statement? This works: if ! fab –fabfile=.deploy/fabfile.py –forward-agent –disable-known-hosts deploy:$target; then rc=1 fi This doesn’t work: # does not work: if ! fab –fabfile=.deploy/fabfile.py \ –forward-agent \ –disable-known-hosts deploy:$target; then rc=1 fi Instead of the … Read more

“echo -n” prints “-n”

I have a problem with echo in my script: echo -n “Some string…” prints -n Some string… and moves to the next line. In the console it’s working correcly without newline: Some string… 1Best Answer 11 There are multiple versions of the echo command, with different behaviors. Apparently the shell used for your script uses … Read more

Command not found error in Bash variable assignment

I have this script called test.sh: #!/bin/bash STR = “Hello World” echo $STR when I run sh test.sh I get this: test.sh: line 2: STR: command not found What am I doing wrong? I look at extremely basic/beginners bash scripting tutorials online and this is how they say to declare variables… So I’m not sure … Read more

What does set -e mean in a bash script?

I’m studying the content of this preinst file that the script executes before that package is unpacked from its Debian archive (.deb) file. The script has the following code: #!/bin/bash set -e # Automatically added by dh_installinit if [ “$1” = install ]; then if [ -d /usr/share/MyApplicationName ]; then echo “MyApplicationName is just installed” … Read more

How can I declare and use Boolean variables in a shell script?

I tried to declare a Boolean variable in a shell script using the following syntax: variable=$false variable=$true Is this correct? Also, if I wanted to update that variable would I use the same syntax? Finally, is the following syntax for using Boolean variables as expressions correct? if [ $variable ] if [ !$variable ] 2 … Read more