How to run multiple .py in ksh?

Hi programmers, say I have 4 files : file1.py,file2.py,file3.py,file4.py

How do I, on a korn shell, create one file, run_all, that is one file that sequentially calls file1-file4, but only so if they complete w/o errors?

Something like:

#!/usr/bin/ksh
file1.py
/*......????*/

Anyone know how to call a bunch a files in one file, kind of like one .sas file with'a bunch of %INCLUDE statements to call other .sas programs (for any sas programmers out there). :slight_smile:

see samples below ... both forms mean the same thing ... modify as you see fit ...

#! /bin/ksh
PATH=/dir

file1.py && \
file2.py && \
file3.py && \
file4.py

exit 0

or ...

#! /bin/ksh
PATH=/dir

file1.py
if [ $? -eq 0 ]
then
    file2.py
    if [ $? -eq 0 ]
    then
        file3.py
        if [ $? -eq 0 ]
        then
            file4.py
        fi
    fi
fi

exit 0

the first form could also be done on the command line ...

 file1.py && file2.py && file3.py && file4.py

If you've got a stack of them, you can always do this in a loop too of course:

# runs anything that starts with "file" and ends with .py in alpha order
for script in ./file*.py   
do
  if script
  then
    echo "$script OK"
  else
    echo "$script failed"
    exit 1
  fi
done
exit 0

I don't know what my problem is, but I tried all three above and no dice?

New to shells, but I basically have 4 files (1.py, 2.py, 3.py,
4.py). I created a new file foo in vi, typed all three types of code above and the go to the unix prompt and type foo [enter] no dice. Pyton foo, still no dice???

A normal .py has the following and works fine???

#!/usr/bin/python
...

Am I executing this wrong?

what was the command you use to execute the script?

also, what are the permissions of the script? post output of ls -l foo

Doh! I bet yer right^ I prob forgot to set the permission to execute *blushing*

I just logged out of the ssh and rebooted the PC, will check in the am but bet yer prob right!

Love this place! You all are real patient and nice. Sorry for the basic questions! I've a python book on order from the library so, I should be less confused soon....(well maybe not :slight_smile: )

The command was >foo and >ksh foo

#!/usr/bin/ksh

set -e

file1.py
file2.py
file3.py
file4.py

Regards,
Alister

1 Like

Worked like a charm Alister, that is some seriously easy to understand code!

It was nice to make file2 have a bad ref in the infile and then see what happened. It stopped the process dead in the tracks and the KSH reported the error with file 2 (i.e. "....no such file or dir..." I fixed the ref and all's square again. Great code, easy to understand. Me and my team are new to unix. Gotta get my python book.

thanks a bunch and have a great weekend.