Check if a file exist in FTP with an automated bash script - bash

I want to automate a batch job that does the following:
check if my file.txt exists in a FTP server, I rename it to
file.trt
check if my file.txt and file.trt exist, if so I send an email
I run another script
at the end I delete file.trt
Here's what I have done:
#!/bin/bash
host='ftp.xxxx.com'
USER='xxxx'
PASSWD='xxxx'
ftp -n -v $host << EOF
ascii
user $USER $PASSWD
prompt
mls /TEST/file.txt test.txt
quit
EOF
if [[ $? -eq 0 ]]
then
echo "The File file.txt Exists";
else
echo "The File file.txt dons not Exist";
fi
Am confused and don't know how to do it, can someone please help me?

I did this and it works fine:
#!/bin/bash
host='ftp.xxxx.com'
USER='xxxx'
PASSWD='xxxx'
FTPFile1="/TEST/file.txt"
FTPFile2="/TEST/file.trt"
#search file.txt et file.trt on the ftp server
ftp -n -v $host << EOT
ascii
user $USER $PASSWD
prompt
mls $FTPFile1 $FTPFile2 test.txt
quit
EOT
# # ----------------------------------------------------------------------------------------------------------------------#
# # test if file.txt et file.trt are present => send mail
# # ----------------------------------------------------------------------------------------------------------------------#
if grep -inr '/TEST/file.txt' test.txt && grep -inr '/TEST/file.trt' test.txt
then
echo "" | mail -s "aaaaa] Error, aaaaa." mail#mail.fr -c mail#mail.fr
elif grep -inr '/TEST/file.txt' test.txt
then
echo "The File file.trt does not Exist (no loading at the moment), rename file.txt to file.trt and start loading";
# ----------------------------------------------------------------------------------------------------------------------#
# rename file.txt
# ----------------------------------------------------------------------------------------------------------------------#
ftp -n -v $host<< EOT
user $USER $PASSWD
get $FTPFile1 file.txt
rename $FTPFile1 $FTPFile2
quit
EOT
else
echo " file.txt (file not yet ready). No action to do."
fi
# ------------------------------------#
ftp -n -v $host << EOT
user $USER $PASSWD
get $FTPFile1 file.txt
quit
EOT
TRT_DATE=`cat file.txt | grep -Po "[0-9]{8}"`
# run my_script.sh
./my_script.sh -d $TRT_DATE
#delete file.txt et test.txt
rm -f file.txt test.txt
# delete file.trt
ftp -n -v $host << EOT
user $USER $PASSWD
delete $FTPFile2
quit
EOT
exit 0

Related

How to check if files exists in FTP? [duplicate]

I want to automate a batch job that does the following:
check if my file.txt exists in a FTP server, I rename it to
file.trt
check if my file.txt and file.trt exist, if so I send an email
I run another script
at the end I delete file.trt
Here's what I have done:
#!/bin/bash
host='ftp.xxxx.com'
USER='xxxx'
PASSWD='xxxx'
ftp -n -v $host << EOF
ascii
user $USER $PASSWD
prompt
mls /TEST/file.txt test.txt
quit
EOF
if [[ $? -eq 0 ]]
then
echo "The File file.txt Exists";
else
echo "The File file.txt dons not Exist";
fi
Am confused and don't know how to do it, can someone please help me?
I did this and it works fine:
#!/bin/bash
host='ftp.xxxx.com'
USER='xxxx'
PASSWD='xxxx'
FTPFile1="/TEST/file.txt"
FTPFile2="/TEST/file.trt"
#search file.txt et file.trt on the ftp server
ftp -n -v $host << EOT
ascii
user $USER $PASSWD
prompt
mls $FTPFile1 $FTPFile2 test.txt
quit
EOT
# # ----------------------------------------------------------------------------------------------------------------------#
# # test if file.txt et file.trt are present => send mail
# # ----------------------------------------------------------------------------------------------------------------------#
if grep -inr '/TEST/file.txt' test.txt && grep -inr '/TEST/file.trt' test.txt
then
echo "" | mail -s "aaaaa] Error, aaaaa." mail#mail.fr -c mail#mail.fr
elif grep -inr '/TEST/file.txt' test.txt
then
echo "The File file.trt does not Exist (no loading at the moment), rename file.txt to file.trt and start loading";
# ----------------------------------------------------------------------------------------------------------------------#
# rename file.txt
# ----------------------------------------------------------------------------------------------------------------------#
ftp -n -v $host<< EOT
user $USER $PASSWD
get $FTPFile1 file.txt
rename $FTPFile1 $FTPFile2
quit
EOT
else
echo " file.txt (file not yet ready). No action to do."
fi
# ------------------------------------#
ftp -n -v $host << EOT
user $USER $PASSWD
get $FTPFile1 file.txt
quit
EOT
TRT_DATE=`cat file.txt | grep -Po "[0-9]{8}"`
# run my_script.sh
./my_script.sh -d $TRT_DATE
#delete file.txt et test.txt
rm -f file.txt test.txt
# delete file.trt
ftp -n -v $host << EOT
user $USER $PASSWD
delete $FTPFile2
quit
EOT
exit 0

How can I tail a log file and add command: word on every line before output in a different file?

trying to tail and grep certain logs with $installingFil and adding Command: word before output in $DNLOGfile, but file is empty /private/tmp/DownLog.log
What will be the correct approach to echo "Command:" before every line.
I am not able to add/echo "Command:" word before every line which is output in /private/tmp/DownLog.log
like:-
Command: bla bla
Command: downloding finish
Scrip -
#!/bin/bash
downFil=$(tail -f /var/log/system.log | grep 'Downloading files of')
dLog="/private/tmp/DownLog.log"
if [ -f /var/log/system.log ]; then
echo "Command:" "$downFil" >> "$dLog" # dLog is blank
else
echo "Command: Please wait.." >> $dLog # this works fine
fi
If I understand you correctly, you should try:
tail -f /var/log/system.log | grep 'Downloading files of' | sed 's/^/Command: /' >> /private/tmp/DownLog.log

Getting an error EOF : Command no found when using ssh

I am trying to rename the filenames in remote server like filename-dirname.suffix
and copy the files to my server .
I had written code like ....
#!/usr/bin/bash
TRANSFERSERVERXMLS="/emp/transfer/XMLS"
REMOTESERVERXMLS="remoteemp/empdir/XMLS"
# renaming the filenames in remote server like filename-dirname.suffix
ssh abc#xyz REMOTESERVERXMLS=$REMOTESERVERXMLS 'bash -s'<< 'EOF'
for i in $REMOTESERVERXMLS/* ; do
if [[ -d $i ]]; then
dirname=$(basename $i)
for j in $REMOTESERVERXMLS/$dirname/* ; do
fname="$(basename "$j")"
prefix=$(echo $fname | awk -F "." 'NF{NF-=1};1')
suffix=$(echo $fname | awk -F "." '{print $NF}')
target=$prefix-$dirname.$suffix
mv $REMOTESERVERXMLS/$dirname/"$fname" $REMOTESERVERXMLS/$dirname/"${target// /_}"
done
fi
done
EOF
scp abc#xyz:${REMOTESERVERXMLS}/*/* ${TRANSFERSERVERXMLS}
Getting an error : EOF:Command not found
and scp is not working ( not able to copy into calling server)
You have a space before the delimiter EOF. Do not indent EOF at the end of your "here document". The delimiter (EOF) must be the only thing on the line, with no leading or trailing whitespace.
Alternatively use <<- 'EOF' and indent with a tab.

ftp while do error

Anyone can help what will be the problem?
Host='192.153.222.1'
User='ftpuser'
passwd='apple'
logfile='a.log'
while :; do
ftp -n -p -v $HOST < example.script >> $logfile
grep -qF "Connected" $logfile &&
grep -qF "File successfully transferred" $logfile && break
done
quote USER $USER
quote PASS $PASSWD
example.script contains
put example.txt
The error is
./example.sh: line 20: syntax error: unexpected end of file
Some fixes:
You missed the closing quote in:
Host='192.153.222.1'
Use a single <, otherwise it's an "here document" in:
ftp -n -p -v "$HOST" < example.script >> "$logfile"
Why you use << in this line?
ftp -n -p -v $HOST << example.script >> $logfile
Change it to
ftp -n -p -v $HOST < example.script >> $logfile
It will work :-)

Deleting a PARTICULAR word from a file using shell script

Hi I am having a problem in deleting a particular set of words from a file using Shell script. Here goes my problem,
My file: group.dat
Sample lines
ADMIN
ADMINISTRATION
ADMINISTRATOR
My Script: groupdel.sh
#!/bin/sh
groupf="<pathtofile>/group.dat"
tmp="<pathtofile>/te"
delgrp()
{
echo "Enter the group to be deleted"
read gname
echo "-------------------"
for gn in `cat $groupf`
do
if [ "$gname" = "$gn" ]
then
sed -e "s/$gname//g" $groupf > $tmp&&mv $tmp $groupf
echo "deleted group"
cat $groupf
exit 1
fi
done
}
echo "Welcome to Group delete wizard"
delgrp
Output:
Enter the group to be deleted
ADMIN
deleted group
ISTRATION
ISTRATOR
Problem: My problem is I dont want the script to delete ADMINISTRATION or ADMINISTRATOR but to delete only ADMIN, any help how to achieve it.
Thanks in Advance
#!/bin/sh
groupf="<pathtofile>/group.dat"
tmp="<pathtofile>/te"
delgrp()
{
echo "Enter the group to be deleted"
read gname
echo "-------------------"
sed -e "/^$gname[[:blank:]]/d" "$groupf" > "$tmp" && mv "$tmp" "$groupf"
echo "deleted group $gname"
cat "$groupf"
return 0
}
echo "Welcome to Group delete wizard"
delgrp
Assuming that the group name is at the beginning of the line and there are other things on the line and you want to delete the whole line, use the regular expression and command as shown.
There's no need for a loop since sed will iterate over the lines of the file for free.
You should return from a function rather than exit from it. Zero means success. One indicates an error or failure.
Always quote variable names that contain filenames.
If the file is one group per line, and the group name is the only thing on the line, use anchors in your regular expression:
s/^$gname:.*//g
If you have Perl installed, you can probably simplify this a bit with something like this:
if grep -q "^${gname}:" $groupf ; then
perl -ni -e "print unless /^${gname}:/" $groupf
echo "Group deleted."
else
echo "No such group $gname."
fi
Or even
grep -v "^${gname}:" $groupf > $tmp && \
cp -f $tmp $groupf && rm -f $tmp
which will copy all lines except the matching one to the temporary file, and then copy the tempfile over the original file, replacing it.
Note that I suggest using a cp rather than a mv in order to retain the permissions of the original file; mv will result in the edited file having permissions set according to your umask with no concern for the original permissions.
So, for the complete script:
#!/bin/sh
groupf="<pathtofile>/group.dat"
tmp="<pathtofile>/te"
delgrp()
{
echo -n "Enter the group to be deleted: "
read gname
echo "-------------------"
if grep -q "^${gname}:" $groupf ; then
grep -v "^${gname}:" $groupf > $tmp
cp -f $tmp $groupf
rm -f $tmp
else
echo "No such group '$gname'"
fi
}
echo "Welcome to Group delete wizard"
delgrp
That should work reliably.
You can use \W to denote the start and end of a word, if they are separated properly:
sed -e "s/\(\W\)$gname\(\W\)/\1\2/g" $groupf > $tmp&&mv $tmp $groupf
Awk is a readable alternative to sed:
awk -v to_delete="$gname" -F: '$1 == to_delete {next} {print}'
Why you don't use sed ?
sed 's/^word$//g'
Also you can use regex to specify multiple words
sed 's/word1|word2//g'
I didn't try this, but this is what you need. Just take a look on Internet on the sed syntax.
Regards

Resources