#!/usr/bin/env python

import subprocess

ncl_script = """
print("started running ncl script:")

begin
  do it = 0, 20, 1
    print("Iterator value: " + it)
    system("sleep 1")
  end do
end
"""

bash_script = """
echo "Started running ncl script:"

counter=0
while [[ counter -lt 20 ]]; do
    echo "Iterator Value: $counter"
    sleep 1
    let counter=counter+1
done
"""

def main():
    open("test.sh","wb").write(bash_script)
    open("test.ncl","wb").write(ncl_script)

    with open("log.out","wb") as out:
        with open("log.err", "wb") as err:
            bp=subprocess.Popen("bash test.sh",shell=True,
                                universal_newlines=True,
                                stdout=out,stderr=err)
            bp.wait()
            np = subprocess.Popen("ncl test.ncl",shell=True,
                                  universal_newlines=True,
                                  stdout=out,stderr=err)
            np.wait()

if __name__ == '__main__':
    main()

