run bash script from another script and redirect its output

8,885

This sounds suspiciously like a homework assignment*, but here we go.

  1. Why would you source the ELABORATO.SH script? If you do and it has an exit statement or errors out, your TEST.SH does the same. I don't think that's what you want. I would just run the script:

    bash ELABORATO.SH param1 param2
    
  2. Did you think about redirecting its input?

    bash ELABORATO.SH param1 param2 < INPUT_FILE
    
  3. To capture the output of ELABORATO.SH, well, doesn't redirection just seem to be made for that?

    bash ELABORATO.SH param1 param2 < INPUT_FILE > OUTPUT_FILE
    

Finding out which files ELABORATO.SH writes is non-trivial. (Sideline: if you don't know its output files, then how could they be relevant to computing the score of ELABORATO.SH? Why bother to even move them around?)

Anyway, here we go. The following pipeline lists the files created by ELABORATO.SH:

strace bash ELABORATO.SH params < INPUT_FILE 2>&1 > OUTPUT_FILE \
    | grep '^open(.*O_CREAT' | cut -d\" -f 2  

Then to move them elsewhere you could do:

for f in $(
    strace bash ELABORATO.SH params < INPUT_FILE 2>&1 > OUTPUT_FILE 
       | grep '^open(.*O_CREAT' | cut -d\" -f 2) 
do
    mv "$f" /path/to/elsewhere
done

*) Please recommend the friendly people at AskUbuntu to your supervisor.

Share:
8,885

Related videos on Youtube

Ciccio
Author by

Ciccio

Updated on September 18, 2022

Comments

  • Ciccio
    Ciccio over 1 year

    Hi everyone I have to do a program.. I'll be really concise to explain it... And hat I've already done and what i have to do...

    Simply, I have to do a "simulator" in Bash that run exercises of students and check if they are correct... for each exercise I have to calculate a score and say if student has passed the exam or not...

    I've done almost everything... I think I miss just some line of code to write.. but I think they are the most important too... :(

    My script is TEST.SH and i must run the exercise of the student.. the exercise is a script ELABORATO.SH... Now, I can have some scenarios:

    1. ELABORATO.SH await in input as parameter some values;
    2. ELABORATO.SH await in input from stdin, from keyboard;
    3. ELABORATO.SH write output in a file/fifo/etc.. ;

    1. First one point it's really simple to resolve... infact from my TEST.SH I call ELABORATO.SH in this way:

      . ELABORATO.SH param1 param2 param3;
      

      Second and third It's more complex to me..

    2. I have to read from a file a string (and it's ok) and pass this string to ELABORATO.SH as the string was from stdin.

    3. I have to create a file with the output of ELABORATO.SH... so for example, if ELABORATO.SH produce a file (I don’t know the name of the output file) I have to produce a file too, in another directory... How can I do?

  • Ciccio
    Ciccio over 9 years
    Hi... thank you for your answer.. Yes.. It's an homework.. but I really do not understand your note (*)...
  • Ciccio
    Ciccio over 9 years
    So.. 1. Thank you.. you are right.. so I was wrong how I was running the script... 2. I mean to take the input from a file instead of the stdin/keyboard, even if the script is conceived to wait for an input from user 3. I was thinking right that solution my only doubt was "what happen if ELABORATO.SH create a file? Can I redirect the content of that output file in another file?"
  • Ciccio
    Ciccio over 9 years
    I cannot know which file produce because the ELABORATO.SH is the exercise written from different students.. so one student could generate the output in a file called just "output", another student could write the result in a file called "out" and so on... I hope I have been clear... However, I can consider to find all file create by ELABORATO.SH and move in another location...
  • Ciccio
    Ciccio over 9 years
    No... something goes wrong... I'm try to run this command $(strace bash "$assessment_tests/elaborato.sh" $parameters < "$assessment_template_dir/stdin" > "$testdir/stdout" 2> "$testdir/stderr" 2>&1 > | grep '^open(.*O_CREAT' | cut -d\" -f 2).. I need to redirect stderr to my stderr file and stdout to my stdout file and in the end i need to take the file created by elaborato.sh and move it in another folder... But I think last step should be not necessary: Why the output file created by elaborato.sh is not created inside "$assessment_tests" (the folder where elaborato.sh is)?