Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

logically invoking blocks of code 1

Status
Not open for further replies.

jawon

Programmer
Feb 6, 2003
31
US
I have 3 "blocks" of code and I want to run them sequentially only after the previous successfully completes. How do I do this when it involves a block of code (eg, "for" statement) as opposed to just a simple command? Sample code is below.

# BLOCK 1: "sas" invokes a program
sas ftp.sas

# BLOCK 2
for date in $(< list)
do
ftp << EndFTP
# more code
EndFTP

# BLOCK 3
sas actv.sas
done
 
Shell programming is linear. Just place one block after the other and include a test to see if they each completed successfully:

# Block 1
# If failed, bail

# Block 2
# If failed, bail

# Block 3

Whether you're using a huge loop or a single command is irrelevant.
 
take a look at &&

ls && pwd && ps

The next command will only run if the previous was sucessful

try

sdjskds && ls && ps

the first is rubish so the rest dont run

you can also use || to run a command if first fails

sdsdsd || ls

ls will only run if sdsdsd fails

Hope this helps

--
| Mike Nixon
| Unix Admin
|
----------------------------
 
Sorry I've re-read your question

do

#!/bin/ksh

function command1
{
ls
}

function command2
{
pwd
}

function command3
{
ps
}

command1 && command2 && command3 || echo &quot;All Failed&quot;

;-)

--
| Mike Nixon
| Unix Admin
|
----------------------------
 
Try....

# BLOCK 1
sas ftp.sas || exit 1

# BLOCK 2
for date in $(< list)
do
ftp << EndFTP |tee /tmp/file$$
# more code
EndFTP
grep -q 226 /tmp/file$$ || exit 2

# BLOCK 3
sas actv.sas || exit 3
done
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top