LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need help adding alert on the attached script - Solaris 10 (https://www.linuxquestions.org/questions/programming-9/need-help-adding-alert-on-the-attached-script-solaris-10-a-4175440052/)

eagletoo 12-04-2012 07:29 PM

Need help adding alert on the attached script - Solaris 10
 
Hi I have this script that does the FTP to another server and wanted to know where I can add an alert that sends email to me if it failed and or successful.

bash-3.00# more 1order
#!/bin/sh
pid=`ps -ef | grep "/bin/sh $0" | grep -v grep | grep -v $$`
if [ -n "$pid" ]; then
exit 1;
fi
cd /extra/home/remisol/order1
ls -1 * >/extra/home/remisol/listord.txt
echo "user sunftp sunftp" >/extra/home/remisol/ftpord.cmd
echo "cd ORDER1" >> /extra/home/remisol/ftpord.cmd
echo "bin" >> /extra/home/remisol/ftpord.cmd
while read myline
do
echo $myline
echo "put "$myline >> /extra/home/remisol/ftpord.cmd
echo "cd ../ORDER3" >> /extra/home/remisol/ftpord.cmd
echo "put "$myline >> /extra/home/remisol/ftpord.cmd
echo "cd ../ORDER2" >> /extra/home/remisol/ftpord.cmd
echo "put "$myline >> /extra/home/remisol/ftpord.cmd
echo "cd ../BKP_ORDERS" >> /extra/home/remisol/ftpord.cmd
echo "put "$myline >> /extra/home/remisol/ftpord.cmd
echo "cd ../ORDER1" >> /extra/home/remisol/ftpord.cmd
done < /extra/home/remisol/listord.txt
echo "quit" >> /extra/home/remisol/ftpord.cmd

ftp -n 192.1.2.185 < /extra/home/remisol/ftpord.cmd
sleep 10

while read myline
do
# cp /extra/home/remisol/order1/$myline /extra/home/remisol/logs/order1
mv /extra/home/remisol/order1/$myline /extra/home/remisol/backup/order1
done < /extra/home/remisol/listord.txt


Thanks in advance

chrism01 12-06-2012 01:51 AM

Theoretically after every line ;)
But seriously this is the key cmd
Code:

ftp -n 192.1.2.185 < /extra/home/remisol/ftpord.cmd
so immediately after that I'd say.
Don't understand why the 'sleep 10' is there???

David the H. 12-06-2012 01:22 PM

Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

I'm not going to immediately address the OP, but I just can't stand to see poorly-written scripts, so I was compelled to rewrite it.

See the comments I added for descriptions of the changes and other recommendations.

Code:

#!/bin/bash
# If you have bash and want to use it,
# then specify it explicitly in the shebang.
# Using "/bin/sh" forces the interpreter into posix mode.

# Don't use hard-coded filenames in the code itself.
# Define them in variables at the top of the script.
topdir="/extra/home/remisol"
listfile="$topdir/listord.txt"
outfile="$topdir/ftpord.cmd"
startdir="$topdir/order1"
logdir="$topdir/logs/order1"
backupdir="$topdir/backup/order1"
ftptarget=192.1.2.185

# I'm not entirely sure what this is supposed to do, is this supposed to be
# the PID of the script?  The first grep grabs it, but the second removes it?
# In any case, there are probably better ways to get the PID than parsing ps.
# Is this step even necessary?
pid=$( ps -ef | grep "/bin/bash $0" | grep -v -e grep -e $$ )

# In bash, the double-bracket test is recommended for file and string tests.
if [[ -n "$pid" ]]; then
        exit 1
fi

# cd into the starting directory.
cd "$startdir"

# Don't use ls to generate lists of files.  You can just printf the globbing pattern.
printf '%s\n' * > "$listfile"

# Actually, I really recommend replacing the list file with an array instead.
# listarray=( * )

# You can redirect a whole block of commands at once.
# I used a command grouping, but a here document would also work.
{
        echo "user sunftp sunftp"
        echo "cd ORDER1"
        echo "bin"
} > "$outfile"

# You can also redirect the entire output of a loop at once.
# Always make sure your variables are quoted too!
while read -r myline; do

        echo "$myline"
        echo "put $myline"
        echo "cd ../ORDER3"
        echo "put $myline"
        echo "cd ../ORDER2"
        echo "put $myline"
        echo "cd ../BKP_ORDERS"
        echo "put $myline"
        echo "cd ../ORDER1"

done < "$listfile" >> "$outfile"

# Or if using an array
# for myline in "${listarray[@]}"; do
#        ....
# done >> "$outfile"

echo "quit" >> "$outfile"

# Run the generated ftp script.
ftp -n "$ftptarget" < "$outfile"
sleep 10

# Move the files to the backup directory.
while read myline ; do

        #  cp "$myline" "$logdir"
        mv "$myline" "$backupdir"

done < "$listfile"

# If using an array just operate on it directly.
# mv "${listarray[@]}" "$backupdir"

# You should always supply an explicit exit code for the script.
exit 0



All times are GMT -5. The time now is 04:19 PM.