I want to assign the output of a mongo command (i.e database names) to a bash array variable but I am not sure how to go about it.
I am getting an error :
dump.sh
Starting-BACKUP
dump.sh
./dump.sh: line 16: declare: `–a': not a valid identifier
./dump.sh: line 24: mongo: command not found
Database backup was successful
when I attempt using dump.sh below:
#!/bin/bash
declare –a databases=()
databases=$(mongo --quiet --uri="mongodb://root:mypassword#mongodb-prom/admin" --authenticationDatabase admin --eval="show dbs;" | cut -d " " --field 1)
echo $databases
Ordinarily I am able to get a listing of the databases when I kubectl into the pod with following steps :
$ kubectl exec -it mongodb-prom-xxxxxxxxx-dddx4 -- sh
$ mongo
> use admin
> db.auth('root','mypassword')
> show dbs
admin 0.000GB
platforms 0.000GB
users 0.000GB
I am not sure why the mongo command is not being recognized because the same script is able to execute the mongodump command below :
mongodump --uri="<uri_here>" --authenticationDatabase admin --gzip --archive=/tmp/"<variable_here>".gz
UPDATE : This is the associated Dockerfile. My expectation is that both mongo and mongodump should be working by default in a mongo container but it seems only mongodump is working for now
FROM mongo
WORKDIR /opt/backup/
WORKDIR /usr/src/configs
COPY dump.sh .
RUN chmod +x dump.sh
My two issues :
Is my syntax correct for the variable assignment (I suspect its not correct) ?
How should I properly declare the array variable to avoid the warnings ?
NB : Mongo tooling is already installed on the container and is actually working for mongodump
You don't need declare -a. Simply putting the value inside () in the assignment will make it an array.
To get all the elements of an array, you have to use ${variable[#]}. $variable is equivalent to ${variable[0]} and just retrieves the first element.
databases=($(mongo --quiet --uri="mongodb://root:mypassword#mongodb-prom/admin" --authenticationDatabase admin --eval="show dbs;" | cut -d " " --field 1))
echo "${databases[#]}"
With bashv4+, mapfile is available, with Process Substitution.
mapfile -t databases < <(mongo --quiet --uri="mongodb://root:mypassword#mongodb-prom/admin" --authenticationDatabase admin --eval="show dbs;" | cut -d " " --field 1)
Now check the value of the databases array
declare -p databases
Your code has
declare -a databases=()
but
databases=$(...)
is not an array, the $(...) construct is Command Substitution.
See also Bash Arrays
sh : The term 'sh' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a
path was included, verify that the path is correct and try again.
At line:1 char:61
+ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
+ ~~
+ CategoryInfo : ObjectNotFound: (sh:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The command which on line 4 on the script below seems to have an issue, intellij says
which is non-standard. Use builtin 'command -v' instead
Since which psql seems like it is not working it automatically affects line 12 and 13.
While investigating i removed line 4 then the script executed line 6 to 10 which succefully created a docker file(pg-docker) however i also need the schema.sql (line 12) and data.sql (13) to be executed. Is there an alternative command for which command(line 4)
Below is my bash Script:
#!/usr/bin/env bash
set -euo pipefail
which psql > /dev/null || (echo "Please ensure that postgres client is in your PATH" && exit 1)
mkdir -p $HOME/docker/volumes/postgres
rm -rf $HOME/docker/volumes/postgres/data
docker run --rm --name pg-docker -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=dev -d -p 432:5432 -v $HOME/docker/volumes/postgres:/var/lib/postgresql postgres
sleep 3
export PGPASSWORD=postgres
psql -U postgres -d dev -h localhost -f schema.sql
psql -U postgres -d dev -h localhost -f data.sql
I get the below on the problems on Intellij
line 4 complains about which command
line 6,7 and 9 complains about $HOME
line 11 complains about PGPASSWORD
which is used to find and show the full path of a command (in your script it is only used to make sure the command psql is there).
IntelliJ or probably better the defined linter for (bash) scripts suggest not to rely on an separate whichcommand but just use the builtin bash-function command -v so the line 4 would read
command -v psql > /dev/null || (echo "Please ensure that postgres client is in your PATH" && exit 1)
That said - it's most likely not your real problem. You need the PostgreSQL Client psql installed and in your PATH variable to run the commands in line 12 and 13. Exactly that's what you're checking in line 4 - regardless of using which or command -v.
How to install the psql command depends on your OS.
I am trying to convert vmdk formate image to hda.qcow2 formate through this command:
qemu-img convert -f vmdk -O qcow2 server\server2016.vmdk hda.qcow2
but it show the following error
qemu-img : The term 'qemu-img' is not recognized as the name of a cmdlet,
function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path
is correct and try again.
At line:1 char:1
+ qemu-img convert -f vmdk -O qcow2 server2016.vmdk server2016.qcow2
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (qemu-img:String) [],
CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I do this two-stage conversion (vmdk->raw->qcow2) in bash using qemu-img QEMU utility (in Debian qemu-utils package)
echo "Converting multiple VMDK files in $PWD into QCOW2..."
rm -f tmpImage.raw
for i in *.vmdk; do
echo "Converting $i ..."
qemu-img convert -f vmdk "$i" -O raw "$i.raw"
cat "$i".raw >> tmpImage.raw
done
echo "Creating finalImage.qcow2..."
qemu-img convert tmpImage.raw finalImage.qcow2
rm tmpImage.raw
The only real question is whether we want to append the unsequenced "$i.vmdk" file to the end of its final raw image before converting, but it seems to work best this way.
I am getting started with Apache isis (I have Windows 10) and following their tutorial (https://isis.apache.org/guides/ugfun/ugfun.html#_ugfun_getting-started_helloworld-archetype)
I installed Java and Maven, added them to the path and then I created a folder inside which when I run command mvn -v I see folling output:
E:\Apache isis\test_project>mvn -v
Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-24T20:49:05+01:00)
Maven home: D:\Development softwares\apache-maven-3.5.3-bin\apache-maven-3.5.3\bin\..
Java version: 9.0.4, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jre-9.0.4
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
When I run the command specified in their docs to generate app:
mvn archetype:generate \
-D archetypeGroupId=org.apache.isis.archetype \
-D archetypeArtifactId=helloworld-archetype \
-D archetypeVersion=1.16.2 \
-D groupId=com.mycompany \
-D artifactId=myapp \
-D version=1.0-SNAPSHOT \
-B
I get following error:
PS C:\Users\Nitish> cd .\Desktop\
PS C:\Users\Nitish\Desktop> mvn archetype:generate \
>> -D archetypeGroupId=org.apache.isis.archetype \
>> -D archetypeArtifactId=simpleapp-archetype \
>> -D archetypeVersion=1.16.2 \
>> -D groupId=com.mycompany \
>> -D artifactId=myapp \
>> -D version=1.0-SNAPSHOT \
>> -B
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.114 s
[INFO] Finished at: 2018-03-20T15:42:22+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (C:\Users\Nitish\Desktop). Please verify you invoked Maven from the correct directory. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException
-D : The term '-D' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:2 char:5
+ -D archetypeGroupId=org.apache.isis.archetype \
+ ~~
+ CategoryInfo : ObjectNotFound: (-D:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
-D : The term '-D' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:3 char:5
+ -D archetypeArtifactId=simpleapp-archetype \
+ ~~
+ CategoryInfo : ObjectNotFound: (-D:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
-D : The term '-D' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:4 char:5
+ -D archetypeVersion=1.16.2 \
+ ~~
+ CategoryInfo : ObjectNotFound: (-D:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
-D : The term '-D' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:5 char:5
+ -D groupId=com.mycompany \
+ ~~
+ CategoryInfo : ObjectNotFound: (-D:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
-D : The term '-D' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:6 char:5
+ -D artifactId=myapp \
+ ~~
+ CategoryInfo : ObjectNotFound: (-D:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
-D : The term '-D' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:7 char:5
+ -D version=1.0-SNAPSHOT \
+ ~~
+ CategoryInfo : ObjectNotFound: (-D:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
-B : The term '-B' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:8 char:5
+ -B
+ ~~
+ CategoryInfo : ObjectNotFound: (-B:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Am I missing out something?
As I was using Powershell I had to use the command :
mvn archetype:generate "-DarchetypeGroupId=org.apache.isis.archetype" "-DarchetypeArtif
actId=helloworld-archetype" "-DarchetypeVersion=1.16.2" "-DgroupId=com.mycompany" "-DartifactId=myapp" "-Dversion=1.0-SN
APSHOT" "-B"
For Unix, it should as below.
mvn archetype:generate \
-DarchetypeGroupId=org.apache.beam \
-DarchetypeArtifactId=beam-sdks-java-maven-archetypes-examples \
-DarchetypeVersion=2.14.0 \
-DgroupId=org.example \
-DartifactId=word-count-beam \
-Dversion="0.1" \
-Dpackage=org.apache.beam.examples \
-DinteractiveMode=false
If you want to use it in powershell replace backslash \ with backtick `