Embed a PS variable in a PS variable - powershell

This works for me.
$puda_TBList=(isql -S $DSQuery -U $User -P $PW -w 200 -D $DB -b -i $SQLDir\Get_TBList.sql)
But what I want to do is embed a variable in the initial variable name like this.
$($DB)_TBList=(isql -S $DSQuery -U $User -P $PW -w 200 -D $DB -b -i $SQLDir\Get_TBList.sql) where $DB is a variable within a FOR loop and I just can't make it work.
Is there an easy way to do this?
I tried all of these and got erros.
$($DB)_TBList=(isql -S $DSQuery -U $User -P $PW -w 200 -D $DB -b -i $SQLDir\Get_TBList.sql)
$($("$DB"_TBList)=(isql -S $DSQuery -U $User -P $PW -w 200 -D $DB -b -i $SQLDir\Get_TBList.sql)
$($DB)_TBList)=(isql -S $DSQuery -U $User -P $PW -w 200 -D $DB -b -i $SQLDir\Get_TBList.sql)
"$($DB)_TBList"=(isql -S $DSQuery -U $User -P $PW -w 200 -D $DB -b -i $SQLDir\Get_TBList.sql)

Related

How to check Postgres backup has been successful(Without manual efforts)?

We have 100+ databases contains daily backups
how to check failure backups in PostgreSQL backup schedules
pg_dump -h localhost -p 5432 -U postgres -d db1 -v -f "path/file.backup"
pg_dump -h localhost -p 5432 -U postgres -d db2 -v -f "path/file.backup"
pg_dump -h localhost -p 5432 -U postgres -d db3 -v -f "path/file.backup"
pg_dump -h localhost -p 5432 -U postgres -d db4 -v -f "path/file.backup"
pg_dump -h localhost -p 5432 -U postgres -d db5 -v -f "path/file.backup"
...
like this i have 100 backup schedules
Try to do it in for loop for example?
#!/bin/bash
# create an indexed array with all your databases listed
database_names=( "1" "2" "3" )
# Declare an associative array to store dbname and dump status codes
declare -A all_dbdump_states
for db in "${database_names[#]}"; do
echo "Executing $db dump.."
pg_dump -h localhost -p 5432 -U postgres -d $db -v -f "path/file.backup"
dump_rc=$? # Save exit code of pg_dump process into variable
# After each for loop iteration, append data into array
all_dbdump_states[$db]+=$dump_rc
done
echo -e "\nListing status codes of all dumps:"
for db in "${!all_dbdump_states[#]}"; do
echo "Database [$db] status: ${all_dbdump_states[$db]}"
sleep 1
done
Here I'm echoing these pg_dump lines for better tests and made an explicit mistake in echo command to put second command fail, with exit code 127:
#!/bin/bash
# create an indexed array with all your databases listed
database_names=( "1" "2" "3" )
# Declare an assotiative array to store dbname and dump status codes
declare -A all_dbdump_states
for db in "${database_names[#]}"; do
echo "Executing $db dump.."
if [[ $db -eq 2 ]]; then
ech "pg_dump -h localhost -p 5432 -U postgres -d 2 -v -f 'path/file.backup'" &>/dev/null
dump_rc=$? # Save exit code of pg_dump process into variable
all_dbdump_states[$db]+=$dump_rc
continue
fi
echo "pg_dump -h localhost -p 5432 -U postgres -d $db -v -f 'path/file.backup'" &>/dev/null
dump_rc=$? # Save exit code of pg_dump process into variable
# After each for loop iteration, append data into array
all_dbdump_states[$db]+=$dump_rc
done
echo -e "\nListing status codes of all dumps:"
for db in "${!all_dbdump_states[#]}"; do
echo "Database [$db] status: ${all_dbdump_states[$db]}"
sleep 1
done
Sample output:
$ ./test.sh
Executing 1 dump..
Executing 2 dump..
Executing 3 dump..
Listing status codes of all dumps:
Database [1] status: 0
Database [2] status: 127
Database [3] status: 0

Shell Script getting terminated in the middle when unable to make DB connection

I have a function block as below which is getting called from somewhere. The call is happening fine but when it's executing any of the below psql check commands eg:
local db_availability_check_88=`psql -h 10.95.187.88 -p 5444 -c "\pset tuples_only" -c "select pg_is_in_recovery;"|grep -v "Tuples"`
The script terminates immediately if the script is unable to communicate to the database via the above psql command. I want thee script to continue its execution. If the connectivity fails it should continue with the next statements.
check_db_status_function () {
echo "entered function block db_status"
local db_availability_check_67=`psql -h 10.95.167.87 -p 5444 -c "\pset tuples_only" -c "select pg_is_in_recovery;"|grep -v "Tuples"`
local db_availability_check_68=`psql -h 10.95.167.88 -p 5444 -c "\pset tuples_only" -c "select pg_is_in_recovery;"|grep -v "Tuples"`
local db_availability_check_69=`psql -h 10.95.167.89 -p 5444 -c "\pset tuples_only" -c "select pg_is_in_recovery;"|grep -v "Tuples"`
if [[ ( $a1 = "down" ) && ( $a3 != "primary" ) && ( $db_availability_check_67 = 't' ) ]];
then
echo "pgPool services are down on the node:$a2"
echo "Promoting..."
/u01/edb/pgpool3.6/bin/pcp_attach_node -w -U pcpuser -h localhost -p 9898 $a0
fi
The script o/p is as :
Nested block 1
check_db_stat
entered function block db_status
psql.bin: could not connect to server: Connection refused
Is the server running on host "10.95.167.88" and accepting
TCP/IP connections on port 5444?
Thanks,
Sandeep

Mongoexport shell script - Error in query

I wrote a shell script to create a data dump of records updated yesterday using mongoexport command.
yesterday=$(date -d 'yesterday 00:00:00' '+%s'000)
today=$(date -d 'today 00:00:00' '+%s'000)
query="'{\"updated_at\":{\$gte:new Date(${yesterday}),\$lte:new Date(${today})}}'"
echo ${query}
mongoexport -h $HOST -d $DOC -c $COL_NAME -u $USER -p $PWD -q ${query} -o $fileName
After adding query, when I run the shell script I get below error in console
'{"updated_at":{$gte:new Date(1484287200000),$lte:new Date(1484373600000)}}'
too many positional arguments: [Date(1484287200000),$lte:new Date(1484373600000)}}']
try 'mongoexport --help' for more information
When I run this query in command line it works properly. Can someone pls let me know why is this error when ran in shell script?
This works in command line.
$mongoexport -h <<HOST>> -d <<DOC>> -c <<COL> -u <<UN>> -p <<PWD>> -q '{"updated_at":{"$gte":new Date(1484287200000),"$lte":new Date(1484373600000)}}'
There is a rule of thumb in bash: when you use a variable, always surround it with double quotes. There are exceptions, but they are rare.
mongoexport -h "$HOST" -d "$DOC" -c "$COL_NAME" -u "$USER" -p "$PWD" -q "${query}" -o "$fileName"
This code worked for me
yesterday=$(date -d 'yesterday 00:00:00' '+%s'000)
today=$(date -d 'today 00:00:00' '+%s'000)
query1="{\"transactionDate\":{\$gte: new Date(${yesterday}),\$lte: new Date(${today})}}"
echo $yesterday
echo $today
mongoexport -d databasename-c collectionname --host yourip --port 27017 -p password -u username-q "${query1}" --type=csv --fields=transactionDate,amount > test5.csv

sqlcmd -v parameter ignored in PowerShell

I've read the documentation (sqlcmd Utility), but I can't get sqlcmd's -v parameter to work as expected.
Assuming this SQL script (echo.sql):
:setvar THE_PATH C:\Users\Craig\Desktop
PRINT 'THE_PATH: $(THE_PATH)'
:setvar THE_TOP 10
PRINT 'THE_TOP: $(THE_TOP)'
When run at the PowerShell prompt without the -v argument set:
PS> sqlcmd -E -S 'server' -d 'database' -i '.\echo.sql'
THE_PATH: C:\Users\Craig\Desktop
THE_TOP: 10
Setting the numeric variable (THE_TOP) are ignored:
PS> sqlcmd -E -S 'server' -d 'database' -i '.\echo.sql' -v THE_TOP=5
PS> sqlcmd -E -S 'server' -d 'database' -i '.\echo.sql'
THE_PATH: C:\Users\Craig\Desktop
THE_TOP: 10
If I eliminate the default value for THE_TOP in echo.sql, it reinforced the assertion that the parameter is being ignored:
:setvar THE_TOP
PRINT 'THE_TOP: $(THE_TOP)'
PS> sqlcmd -E -S 'server' -d 'database' -i '.\echo.sql' -v THE_TOP=5
THE_PATH: C:\Users\Craig\Desktop
THE_TOP: $(THE_TOP)
If I attempt to set the THE_PATH parameter, I get:
PS> sqlcmd -E -S 'server' -d 'database' -i '.\echo.sql' -v THE_PATH="C:\path"
Sqlcmd: ':\path': Invalid argument. Enter '-?' for help.
What is the correct -v syntax?
OK, this is retarded.
If :setvar is used in the script:
:setvar THE_TOP
PRINT 'THE_TOP: $(THE_TOP)'
then you attempt to set it at the PowerShell prompt, you'll get an error:
PS> sqlcmd -E -S server -d database -i .\echo.sql -v THE_TOP=5
'THE_TOP' scripting variable not defined.
However, if you DO NOT set it in the script:
-- variable disabled
-- :setvar THE_PATH
PRINT 'THE_TOP: $(THE_TOP)'
then you attempt to set it at the PowerShell prompt, then it will work as (not really) expected:
PS> sqlcmd -E -S server -d database -i .\echo.sql -v THE_TOP=5
THE_TOP: 5
** edit **
To supply a dynamically-generated path as a parameter to a script file (extract.sql):
:out $(THE_PATH)\extract.csv
SELECT *
FROM the_table
WHERE the_key = $(THE_ID)
...
Execute in PowerShell session:
PS> $cd = Get-Location
PS> sqlcmd -E -S 'server' -d 'database' -i '.\extract.sql' -v THE_ID=5 THE_PATH=`"$cd\build`"

command work in command prompt but not in batch file

I am trying to send out email attachment periodically from a batch script using this command line client
http://caspian.dotconf.net/menu/Software/SendEmail
the command for it is pretty straightforward
sendEmail -t abc#gmail.com -u hellp -f abd#nus.edu.sg -m world -s smtp.nus.edu.sg -xu nusstu\a123456 -xp helloworld! -a C:\Users\ll\Desktop\Quotes.docx
The strange thing is if I type that line in command prompt, it works perfectly fine. I can
receive the email with attachment.
However, when I put it into a batch script, like
:email
sendEmail -t abc#gmail.com -u hellp -f abd#nus.edu.sg -m world -s smtp.nus.edu.sg -xu nusstu\a123456 -xp helloworld! -a C:\Users\ll\Desktop\Quotes.docx
goto:eof
and I call this subroutine in my batch file, the output says
SMTP:AUTH authentication to smtp.nus.edu.sg:25 failed.
I checked. no typo, Could someone help me what is the difference? why the same command works in command line but cause trouble in a script file?
Thanks
I believe you're censoring your batch too much.
Try running the following:
#ECHO OFF
setlocal
echo sendEmail -t abc#gmail.com -u hellp -f abd#nus.edu.sg -m world -s smtp.nus.edu.sg -xu nusstu\a123456 -xp helloworld! -a C:\Users\ll\Desktop\Quotes.docx
endlocal
SETLOCAL enabledelayedexpansion
echo sendEmail -t abc#gmail.com -u hellp -f abd#nus.edu.sg -m world -s smtp.nus.edu.sg -xu nusstu\a123456 -xp helloworld! -a C:\Users\ll\Desktop\Quotes.docx
echo sendEmail -t abc#gmail.com -u hellp -f abd#nus.edu.sg -m world -s smtp.nus.edu.sg -xu nusstu\a123456 -xp helloworld^^! -a C:\Users\ll\Desktop\Quotes.docx
goto:eof
Because your password contains !, batch interprets that as a variable-name if DELAYEDEXPANSION is invoked. Since the variablename isn't set, it's replaced by [nothing] and sendtoemail believes that your password is helloworld\Users\ll\Desktop\Quotes.docx - hence the error message.