Register a domain and help support LQ
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-04-2012, 08:29 PM   #1
LQ Newbie
Registered: Dec 2012
Posts: 1

Rep: Reputation: Disabled
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
pid=`ps -ef | grep "/bin/sh $0" | grep -v grep | grep -v $$`
if [ -n "$pid" ]; then
exit 1;
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
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 < /extra/home/remisol/ftpord.cmd
sleep 10

while read myline
# 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
Old 12-06-2012, 02:51 AM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.7, Centos 5.10
Posts: 16,917

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
Theoretically after every line
But seriously this is the key cmd
ftp -n < /extra/home/remisol/ftpord.cmd
so immediately after that I'd say.
Don't understand why the 'sleep 10' is there???
Old 12-06-2012, 02:22 PM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954
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.

# 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.

# 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

# 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

Last edited by David the H.; 12-06-2012 at 02:32 PM. Reason: minor code corrections


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
DB Mail Alert Script vignesh4sh Linux - Server 3 11-28-2012 03:37 AM
Adding Network attached storage as a download path samsom Linux - Networking 3 03-02-2010 01:06 AM
Can IBM 3490F Tape Driver attached to Solaris 8 ?? budiss Linux - Hardware 1 01-27-2008 03:37 AM
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 05:19 AM
Adding new Disk attached from SAN with out reboot.? dcj1mmy Linux - Enterprise 4 10-28-2005 05:42 PM

All times are GMT -5. The time now is 12:46 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration