Home > database >  Parallelism - Shell scripting
Parallelism - Shell scripting

Time:04-20

I would like to run two commands in my script. The fist runs a test and the second queries this test to obtain a test id.

Now, the problem is that the second command works ONLY WHILE the first command is running (and of course, after it started). So i can't run it sequentially. As far as i can understand, i need some sort of parallelism, which should allow the 2 processes to run in parallel. This would allow the second command to obtain the ID from the first, as it is still running.

Point is: i don't have and i can't install Parallel on this machine. I saw also xargs, but doesn't seem to suite for this task. Any ideas?

The command looks something like this:

run test | list test running

Thanks

Edit This is how the report looks like. It has many different parameters but the first one is the one i need.

ID             | 94503
Name           | Test 
...

To grab the ID i use:

sed -n '1p' | sed 's/^.\{,17\}//' > test_id.dat

Which give me back the ID number.

Piping won't work. The point is that after run test, the problem in fact is that when I start the test, the "shell remains busy" with the test execution. The second command, on the other hand, can retrieve this ID only while the test is running. Example: if I start the test from two separate sessions (one local, one ssh) I can get the expected result.

Edit 2 Some additional details. In order to obtain the "test id", i've to run a commnad which lists all current running tests (list test running). I follow the advices and wrote something like this:

#!/bin/bash
list test running > test_running.dat | {
    run test
    cat test_running.dat | sed -n '1p' | sed 's/^.\{,17\}//' > test_id.dat
    cat test_id.dat
}

The problem here (no matter which command i put first) is that i get as result in the file "No test running". Meaning that the list test running gets executed to early or to late, but in any case not WHILE run test is running.

CodePudding user response:

update

From what I understand the output of run test is irrelevant because you'll get the ID with the list test running command.

I would say that you can simply wait that list test running gives you the ID. Here I use a bash regex based on "how the report looks like" so you might have to adjust it.

#!/bin/bash

run test &

# bash regex for capturing the test ID from the output of 'list test running'
# REMARK: it is based on "how the report looks like" in your question
regex='^ID *\| *([0-9] )'

while ! [[ $(list test running) =~ $regex ]] && jobs -rp | awk 'END{exit(NR==0)}'
do
    sleep 1
done

test_id=${BASH_REMATCH[1]}

[[ $test_id ]] && echo "I got the test-ID: $test_id"

wait

old answer

So run test starts by outputting a line with the ID and then continue computing and outputting whatever it might be?
It's not clear how you'll use that ID but the following code should show you a way to get what you want and run a command with it while run test continue executing:

#!/bin/bash

run test | {
    IFS= read -r line          # catch the first line (containing the ID)

    obtain test "${line:17}" & # launch the other command as a sub-process

    printf '%s\n' "$line"      # print the first line that was read
    cat                        # continue reading & printing the output
}

remark: You also need to disable any output buffering that run test might have.

  • Related