LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 12-04-2012, 07:29 PM   #1
eagletoo
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
#!/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
 
Old 12-06-2012, 01:51 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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???
 
Old 12-06-2012, 01:22 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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

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


Reply



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 02:37 AM
Adding Network attached storage as a download path samsom Linux - Networking 3 03-02-2010 12:06 AM
Can IBM 3490F Tape Driver attached to Solaris 8 ?? budiss Linux - Hardware 1 01-27-2008 02:37 AM
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM
Adding new Disk attached from SAN with out reboot.? dcj1mmy Linux - Enterprise 4 10-28-2005 04:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:56 AM.

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