2

Basic idea here is to call 1 file from the asd directory and 1 line from the output.txt file in each curl call:

#!/bin/bash
python MJD.py -> output.txt
FILES=/home/user/asd/*
for f in $FILES
do
filename="$output.txt"
while read -r line
do
curl -F ID='zero' -F dir="@$f" -F TIME="$line" -F outputFormat=json "http://blabla"
done
done

This code actually calls the python script and saves the output inside the output.txt file. The output file is just 1 number on each line for several lines. Now what I want to do is to get, starting from the first line, for -F TIME=" one value inside the text file".

I don't know what part of my code is causing the problem. When I call this script the part calling the files from dir works but for every time the TIME=0 appears on screen, looks like nothing is read from the output.txt file.. What am I missing here?

2 Answers 2

2

This would be a good place for a process substitution and exec:

#!/bin/bash

exec 4< <( python MJD.py - )
# now, the output from the python script can be read from channel 4

for file in /home/user/asd/*; do
    IFS= read -u4 -r time              # get the next line from MJD.py
    curl -F ID='zero' -F dir="@$file" -F TIME="$time" -F outputFormat=json "http://blabla"
done

That exec "trick" is specified in the manual:

exec [-cl] [-a name] [command [arguments]]

[...] If no command is specified, redirections may be used to affect the current shell environment.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Now everything works like a charm. I understood the idea but I should look into that manual and learn more about this. Again, thanks a lot!
0

You're not feeding your inner loop the output file, try:

#!/bin/bash
python MJD.py > output.txt
FILES=/home/user/asd/*
for f in $FILES
do
filename="output.txt"
while read -r line
do
curl -F ID='zero' -F dir="@$f" -F TIME="$line" -F outputFormat=json "http://blabla"
done < "$filename"
done

Although, since you're already using Python, why not just write all the logic in Python and deal with everything at one place?

1 Comment

I have understood the situation here and corrected it now it works. However, it goes for all values to all values. There are 2 files in the dir path and there are 2 values in the txt file. When this code works it applies everyone 1 by 1 creates 4 results.. What I need is only 2 results 1 entry from dir and 1 line from the txt starting from first and goes until they finish for the first time so only 2 results and once it done the loop to stop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.