I'm pretty new at this, so excuse me if this is too easy. The thing is, I have a bunch of instructions stored in a text file named: 'FTIG_dldStruct.txt' and I want to write them all into an executable named: 'STP'. I wrote this script:

set +H

./STP # This is the executable

for i in {1..586} 
        sed -n -e "$i"p FTIG_dldStruct.txt

My program runs the executable, and it asks me for input like this:

STP> # I want the instructions here, but my for loop is not working until I exit the executable.

  • 2
    Welcome Herber - scripting is totally on topic here as on several other sites. No need to go elsewhere unless you don’t get any good answers in 2 weeks time or so. That being said - no worries you are new. Can you edit and clarify - executable normally means a compiled program and scripts / interpreted programs are bash scripts. Are you saying you need to run a program and then process a file when the program ends? – bmike Jan 25 at 4:15
  • @MatthewBarclay Scripting questions (both for AppleScript and for shell scripts) are perfectly on-topic on AD. – nohillside Jan 25 at 10:58

It appears you want to send some number (586?) lines from FTIG_dldStruct.txt to the ./STP command? If so, assuming ./STP reads from standard input as is typical, that may look like:

head -n 586 FTIG_dldStruct.txt | ./STP

As pointed out in the comments to pass the entire contents of the file that would be:

./STP < FTIG_dldStruct.txt

Various pipelines are also possible, perhaps to filter out comments (here arbitrarily defined as lines beginning with #) with grep:

grep -v '^#' FTIG_dldStruct.txt | ./STP

If you need ./STP to stick around after those commands are entered so the program can be interacted with, that's a more complicated issue and typically requires an expect script that in turn wraps around ./STP.

#!/usr/bin/env expect -f
set filehandle [open "FTIG_dldStruct.txt"]
set maxlines 586
spawn -noecho ./STP
while {[gets $filehandle line] >= 0 && $maxlines > 0} {
    # wait for prompt from program
    expect -ex "STP>"
    send "$line\r"
    incr maxlines -1
# turn interaction with the program over to the user

This code would need to be saved to a file and made executable like any other script on unix. expect has also been made available as a library in various other programming languages.

  • 1
    If you want to send the entire contents of FTIG_dldStruct.txt, it's even easier: ./STP <FTIG_dldStruct.txt. – Gordon Davisson Jan 25 at 5:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .