Jump to content

SOLVED: Problem executing compiled AU3 scripts from Cygwin Bash script


tremolux66
 Share

Recommended Posts

Before I dive into the specifics of my problem, are there known, fatal issues when running a compiled AutoIt script (.exe file) from a Cygwin Bash script? Am I wasting my time trying to get it to work?

If not, then here's my problem: I have 2 compiled AU3 scripts that are normally executed in the background by a Windows service. The Start script is executed by the service with a directory path as a command-line argument, does some processing, and creates a database record. After an external (human) activity finishes, the Stop script is executed, does some more processing, and updates the database record for the directory. The Bash script emulates the service for purposes of re-processing directories that had errors during the "live" pass: it loops through a text file containing a list of failed directories and executes the Start and Stop scripts.

If the AU3 script executions are commented-out (e.g., to test other logic in the loop), the Bash script reads through the entire directory-list file; the Start and Stop scripts are treated as if they had run successfully. But if the AU3 scripts are actually executed, the loop exits after the first full iteration (i.e., without skips caused by error-detection).

Subsequent runs of the Bash script process one directory each and exit (the list file shrinks with each pass). It seems like something about the execution of the AU3 scripts is changing the Bash loop execution (e.g., causing it to see end-of-file for the directory list file). A puzzling aspect is that an earlier version of the Bash script executed successfully (all directories processed), but in that case the directory list was taken from a Bash array variable rather than a file. Since there could be hundreds (or more) directories requiring re-processing, I modified the Bash script to read from a file, with the intention of making it more robust.

Any suggestions for troubleshooting this will be welcome.

Outline of the script; the actual script is much longer:

#!/bin/bash
# Initialize variables...
export START_EXEPATH="C:/mydir/StartScript.exe"
export STOP_EXEPATH="C:/mydir/StopScript.exe"

# Generate (unix) text file containing (unix) directory paths, one per line
find C:/somedir -type d [some other criteria...] -print >/tmp/dir_list.out

# Core code:
let errcnt=0
while IFS='' read -r dpath
do
    # Validate the last component of dpath...
    dpath_base="$(basename "$dpath")"
    regexp="some-extended-regexp"
    if [[ "$dpath_base" =~ $regexp ]]; then dpath_bad=0; else dpath_bad=1; fi
    if [ $dpath_bad -eq 1 ]; then let errcnt+=1; echo "error message"; continue; fi
    
    win_dpath="$(cygpath -a -w "$dpath")"
    
    # Run 1st AutoIt script
    "$START_EXEPATH" "$win_dpath" "another-argument"
    if [ $? -ne 0 ]; then let errcnt+=1; echo "error message"; continue; fi
    
    # Run 2nd AutoIt script
    "$STOP_EXEPATH" "$win_dpath"
    if [ $? -ne 0 ]; then let errcnt+=1; echo "error message"; fi

done </tmp/dir_list.out

echo "$errcnt errors"
if [ $errcnt -ne 0 ]; then exit 2; fi
exit 0

 

Edited by tremolux66
Mark problem as solved

When the going gets tough, the tough start coding.

Link to comment
Share on other sites

  • 1 month later...

SOLUTION:

When I posed this question, I was unaware of a couple of aspects of Bash:

  1. Arrays can grow to be as large as needed; it's not necessary to worry about the array size
  2. Bash doesn't work as desired with this construct:
# Original Bash construct
while read dir
do
	# Process dir
    autoitscript.exe "$dir"   
done <listfile

(The above method was ingrained from the days of Bourne shell programming. )

I eventually found the "mapfile" command and used it to populate an array and walk through it; the revised code is structured like this:

# Improved Bash code
mapfile -t dirlist <listfile

for dir in "${dirlist[@]}"
do
    # Process dir
    autoitscript.exe "$dir"
done

When the going gets tough, the tough start coding.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...