LFTP and SFTP not working using crontab - linux

I am using below script :
script_home=/home/insetl/ppp_scripts/prod/VSAVS
date_var=`date +'%Y%m%d' -d "0 days ago"`
date_var1=`date +'%Y%m%d' -d "1 days ago"`
lftp<<END_SCRIPT
open sftp://213.239.205.74
user sftpvuclip LW?T8e62
set xfer:clobber on
get Vuclip_C2B_TRX-${date_var}0030.csv
bye
END_SCRIPT
sudo grep -v '^ACC' $script_home/Vuclip_C2B_TRX-${date_var}0030.csv
/home/insetl/ppp_scripts/prod/VSAVS/Vuclip_C2B_TRX-${date_var}.csv
scp $script_home/Vuclip_C2B_TRX-${date_var}.csv $TARGET_HOST:/tmp/
psql -h $TARGET_HOST -d $TARGET_DB -p 5432 << ENDSQL
delete from vsavs_offline_revenue_dump where update_date like '${date_var2}%';
copy vsavs_offline_revenue_dump from '/tmp/Vuclip_C2B_TRX-${date_var}.csv' with delimiter as ',';
update fact_channel_daily_aggr a
set revenue_from_file = (select sum(substr(amount,3)::float) from vsavs_offline_revenue_dump where to_char(to_timestamp(update_date, 'dd-mm-yyyy' ),'YYYYMMDD') = ${date_var1})
where product_sk = 110030 and
a.date_id=${date_var1};
ENDSQL
The script runs completely fine when run manually. However the LFTP/SFTP part of the script doesnt run when ran through crontab. Please advise the solution for the same.
Output :
/home/insetl/ppp_scripts/prod/VSAVS/sftp_pull_in.sh: line 9: 08/02/2017: No such file or directory
20170209
/home/insetl/ppp_scripts/prod/VSAVS
grep: /home/insetl/ppp_scripts/prod/VSAVS/Vuclip_C2B_TRX-201702090030.csv: No such file or directory
DELETE 0
COPY 0
UPDATE 20

Related

No operator matches the given name and argument types. You might need to add explicit

I am trying to get values from postgresql DB using the bash command line : There is an issue trying to get run a select statement from the table.
For example if I execute this select statement, it return successful and gives the value
psql -U postgres -d postgres -p 5432 -t -c "select count(*) from sampledata.sif_work where servicerequesttype='CreatepostgresCase'"
However when I tried to add more where statement either hardcoded or variables to the WHERE statement, I got this error :
ERROR: operator does not exist: character varying <> integer
LINE 1: ...questtype='CreatepostgresCase' and applicationerrorcode!=25 and a...
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
The script :
#!/bin/bash
errorCodeSuccess=0
errorCodeFailure=30
sampleDbUser=postgres
sampleDBPort=5432
appErrorCodeFailure=25
#hardcoded
psql -U postgres -d postgres -p 5432 -t -c "select count(*) from sampledata.sif_work where servicerequesttype='CreatepostgresCase' and applicationerrorcode=25 and pxcreatedatetime>current_date"
#variables used
psql -U "${sampleDbUser}" -d postgres -p "${sampleDBPort}" -t -c "select count(*) from sampledata.sif_work where servicerequesttype='CreatepostgresCase' and applicationerrorcode!="${appErrorCodeFailure}" and applicationerrorcode!="${errorCodeSuccess}" and pxcreatedatetime>current_date"
Any reason why even though I hardcoded the value, it is still throwing error. Any reason ?
PostgreSQL understands 25 as an integer literal but '25' will be interpreted as a text literal/string constant, which would work with your character varying type column.
You could add the single quote ' before you close and after you open the double quotes ", but you also don't need to close that double-quoted string at all - bash evaluates $ expressions in double quotes:
errorCodeSuccess=0
errorCodeFailure=30
sampleDbUser=postgres
sampleDBPort=5432
appErrorCodeFailure=25
#hardcoded
psql -U postgres -d postgres -p 5432 -t \
-c "select count(*)
from sampledata.sif_work
where servicerequesttype='CreatepostgresCase'
and applicationerrorcode='25'--single quotes indicate a text literal
and pxcreatedatetime>current_date"
#variables used
psql -U "${sampleDbUser}" -d postgres -p "${sampleDBPort}" -t \
-c "select count(*)
from sampledata.sif_work
where servicerequesttype='CreatepostgresCase'
and applicationerrorcode!='${appErrorCodeFailure}'
and applicationerrorcode!='${errorCodeSuccess}'
and pxcreatedatetime>current_date; "
You already knew you can safely use single quotes within a double-quoted string, looking at servicerequesttype='CreatepostgresCase'.
You can also make the single quotes a part of the value:
#already doesn't work:
errorCodeSuccess=0
#same effect:
errorCodeSuccess='0'
#this will be interpreted as a column named "0":
errorCodeSuccess='"0"'
#"0" would be a valid name, but I doubt you have one or want one
#this will work:
errorCodeSuccess="'0'"
errorCodeFailure="'30'"
sampleDbUser=postgres
sampleDBPort=5432
psql -U "${sampleDbUser}" -d postgres -p "${sampleDBPort}" -t \
-c "select count(*)
from sampledata.sif_work
where servicerequesttype='CreatepostgresCase'
and applicationerrorcode != ${appErrorCodeFailure}
and applicationerrorcode != ${errorCodeSuccess}
and pxcreatedatetime>current_date; "
Keep in mind that it's always unsafe to construct queries this way - both in terms of security and convenience. You could start improving this with psql -v.

Bash script with multiline heredoc doesn't output anything

I'm writing a script to send SQL output in mail, but it is not executing successfully and is not generating the output I want.
The query generates two columns with multiple rows. How can I generate the output in table format as below?
Below is my code:
#!/bin/bash
ORACLE_HOME= **PATH
export ORACLE_HOME
PATH=$PATH:$ORACLE_HOME/bin
export PATH
TNS_ADMIN= ** PATH
export TNS_ADMIN
today=$(date +%d-%m-%Y)
output=$(sqlplus -S user/pass#service <<EOF
set heading off;
SELECT distinct list_name ,max(captured_dttm) as Last_received FROM db.table1
group by list_name having max(captured_dttm) <= trunc(sysdate - interval '2' hour);
EOF)
if [ -z "$output" ];
then
echo"its fine"
exit
else
echo "
Dear All,
Kindly check we've not received the list for last 2 hour : $output
Regards,
Team" | mailx -S smtp=XX.XX.X.XX:XX -s "URGENT! Please check list FOR $today" user#abc.com
fi
When using a here document, the closing string can't be followed by anything but a newline. Move the closing parenthesis to the next line:
output=$(sqlplus -S user/pass#service <<EOF
...
EOF
)

how do I get bash globbing to work in script

I am trying to convert some of my ksh93 scripts to bash in cygwin. I have found 2 things right now that give me trouble. The first is a function I put in .bashrc was not recognized in the script. I put the function in the script to get around that. The second is that it won't glob like it does in ksh93. Setting extglob didn't seem to help. Here is what I have done:
#! /bin/bash
rep() {
perl -E 'say "'"$1"'" x '$2
# seq -s"$1" $2|tr -d '[:digit:]'
}
# added these 2 lines for testing
shopt -s extglob
shopt extglob
ziptext="Monthly files for $(date --date="$(date +%Y-%m-15) -1 month" +'%B %Y')"
equals4=$(rep = $((${#ziptext} + 6)))
equals="$(rep = ${#ziptext})"
spaces="$(rep ' ' ${#ziptext})"
# added these 2 lines for testing
ls -l 20[0-9][0-9]' Monthly Data - review.xlsx'
pwd
echo "
$equals4
= $equals =
= $spaces =
= $ziptext =
= $spaces =
= $equals =
$equals4\n\n\n" | zip -9 -u -z \
20[0-9][0-9]' Monthly Data - review.xlsx' \
20[0-9][0-9]' Monthly Tables - review.xlsx'
The result is:
extglob on
ls: cannot access '20[0-9][0-9] Monthly Data - review.xlsx': No such file or directory
/cygdrive/c/reports
zip warning: 20[0-9][0-9] Monthly Data - review.xlsx not found or empty
zip warning: name not matched: 20[0-9][0-9] Monthly Tables - review.xlsx
From the shell, doing
ls 20[0-9][0-9]' Monthly Data - review.xlsx' \
20[0-9][0-9]' Monthly Tables - review.xlsx'
Results in
'2019 Monthly Data - Review.xlsx'* '2019 Monthly Tables - Review.xlsx'*
What setting am I missing out on to get this to work like it did in ksh93?
Your filename has capital R Review while your glob uses lowercase review.
Your local shell most likely has nocaseglob enabled to do case insensitive globbing. If this used to work on ksh93, it probably had a similar option enabled by default as well.
In bash you have to enable it explicitly in a script with shopt -s nocaseglob

Twice Bash command substitution

I have directories a1..a5, b1..b5 and c1..c5. Inside each directory I have two files a1, b1 and c1.
do mkdir /tmp/{a,b}$d; touch /tmp/{a,b,c}$d/{a,b,c}1; done;
I want to get all the files starting with 'a' or 'b' inside the directories starting with an 'a'. I can do it with:
DIRS=`ls -1 -d /tmp/{a,b}*/a*`
echo ${DIRS}
and obtain:
/tmp/a1/a1 /tmp/a2/a1 /tmp/a3/a1 /tmp/a4/a1 /tmp/a5/a1
/tmp/b1/a1 /tmp/b2/a1 /tmp/b3/a1 /tmp/b4/a1 /tmp/b5/a1
Now, I will use a variable called DATA to store the directories and later get the files:
DATA="/tmp/{a,b}*"
echo ${DATA}
DIRS=`ls -1 -d ${DATA}/a*`
echo ${DIRS}
In the output, the DATA contents is OK (/tmp/{a,b}*), but I receive the following error:
ls: cannot access /tmp/{a,b}*/a*: No such file or directory
Any idea why this happens?
I solved the problem, but I can't find any reference about why my previous attempts failed.
DATA="/tmp/{a,b}*"
echo ${DATA}
DIRS=`eval "ls -1 -d ${DATA}/a*"`
echo ${DIRS}
Output:
/tmp/a1/a1 /tmp/a2/a1 /tmp/a3/a1 /tmp/a4/a1 /tmp/a5/a1 /tmp/b1/a1
/tmp/b2/a1 /tmp/b3/a1 /tmp/b4/a1 /tmp/b5/a1

freeTDS bash: Executing sql queries in Microsoft SQL server

I am able to connect to a Microsoft SQL Server 2008 instance via a Mint Linux VM using freeTSD and command line to execute sql statements on it. Now I want automate this in a bash script. I am able to successfully login in my bash script:
TDSVER=8.0 tsql -H servername -p 1433 -D dbadmin -U domain\\Administrator -P password
I then have my SQL query:
USE dbname GO delete from schema.tableA where ID > 5 GO delete from schema.tableB where ID > 5 GO delete from schema.tableC where ID > 5 GO exit
This works when doing manually via freeTSD command line, but not when I put in bash file. I followed this post: freeTSD & bash.
Here is my bash script sample:
echo "USE dbname GO delete from schema.tableA where userid > 5 go delete from schema.tableB where userid > 5 go delete from schema.tableC where ID > 5 GO exit" > tempfile | TDSVER=8.0 tsql -H servername -p 1433 -D dbname -U domain\\Administrator -P password < tempfile
the output of the bash script is:
locale is "en_US.UTF-8"
locale charset is "UTF-8"
Default database being set to sbdb
1> 2> 3> 4> 5> 6> 7> 8>
and then the rest of my script is executed.
Can someone give me a step by step answer to my problem ?
I'm not sure how your sample can work at all.
Here is my bash script sample:
echo "USE dbname .... exit" > tempfile | TDSVER=8.0 tsql -H servername -p 1433 -D dbname -U domain\\Administrator -P password < tempfile
# ------------------------------------^^^^ ---- pipe char?
Try using a ';' char.
echo "USE dbname .... exit" > tempfile ; TDSVER=8.0 tsql -H servername -p 1433 -D dbname -U domain\\Administrator -P password < tempfile
# ------------------------------------^^^^ ---- semi-colon
Better yet, use shell's "here documents".
TDSVER=8.0 tsql -H servername -p 1433 -D dbname -U domain\\Administrator -P password <<EOS
USE dbname
GO
delete from schema.tableA where userid > 5
go
delete from schema.tableB where userid > 5
go
delete from schema.tableC where ID > 5
GO
exit
EOS
IHTH.
Current command line input:
echo "delete from table where userid > 5
go
delete from table where userid > 5
go
delete from table where ID > 5
GO
exit" < /tmp/tempfile; TDSDUMP=/tmp/freetds.log TDSVER=8.0 tsql -H servername -p 1433 -D dbname -U Administrator -P password <<EOS
Old thread but this seemed to work..
printf "use mydbname\ngo\nselect * from mytable\ngo\nexit\n"|tsql -I freetds.conf -S profileName -U user -P 'password'
1> 2> 1> 2> ID stringtest integertest
1 test 50
2 teststring2 60
3 test3 70
(3 rows affected)
try
echo "USE dbname\n GO\n delete from schema.tableA where ID > 5\n GO\n delete from schema.tableB userid > 5\n go\n delete from schema.tableC where ID > 5\n GO\n exit\n"
the rest of this string is stuff that maybe works
and try
echo "USE dbname;\n delete from schema.tableA where ID > 5;\n delete from schema.tableB userid > 5;\n delete from schema.tableC where ID > 5;\n exit\n"
and try
echo "USE dbname; delete from schema.tableA where ID > 5; delete from schema.tableB userid > 5; delete from schema.tableC where ID > 5; exit"
if you are using odbc, i recommend the second trial.
if you are sending commands to sql with a "go" word as sql sentences separator, maybe the first one is better.
maybe the third one... who knows... only trial and error can tell...

Resources