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
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.
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
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 :-)
I've created a script for account creation that reads from a csv file, i do not need the first line in the csv as it has the titles for the colums, im trying to delete the first line using sed 1d $file, but it doesnt seem to work.
#!/bin/bash
FILE="applicants.csv"
sed 1d $FILE |while IFS=: read USERNAME PASSWORD SCHOOL PROGRAM STATUS; do
#------------------------------------------
groupadd -f $SCHOOL
useradd $USERNAME -p $PASSWORD -g $SCHOOL
if [ $? == 0 ]; then
echo
echo "Success! $USERNAME created"
grep $USERNAME /etc/passwd
echo
#------------------------------------------
else
echo "Failed to create account for $USERNAME"
fi
done < $FILE
here is the csv file
Full Name:DOB:School:Program:Status
JoseDavid:22-08-86:ACE:Bsc Computing:Unfinished
YasinAhmed:22-07-85:ACE:Bsc Networking:Complete
MohammedAli:21-04-84:ACE:Bsc Forensics:Complete
UtahKing:22-09-84:ACE:BSC IT:Unfinished
UsmanNaeem:21-09-75:ACE:BSC Computing:Complete
Here is a screenshot of the output
http://i.stack.imgur.com/R5zPN.jpg
is there anyway to skip the first line?
Try using tail -n +2 $FILE instead of sed.
You're reading from the unedited file with the redirect at the end: done < $FILE. Try changing that line to just done.
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