0

I have a while loop, where A=1~3

mysql -e "select A from xxx;" while read A; 

do 

whatever

done

The mySQL command will return only numbers, each number in each line. So the while loop here will have A=1, A=2, A=3

I would like to append the integer number in the loop (here is A=1~3) into a command line to run outside the while loop. Any bash way to do this?

parallel --joblog test.log --jobs 2 -k sh ::: 1.sh 2.sh 3.sh

2
  • Can you clarify the output from the mysql command (is it always just numbers? Is each on a separate line, or separated by spaces and/or commas, or something else?). Also, it looks like you want ".sh" appended to each one; is that correct? Commented Sep 22, 2017 at 3:57
  • Yes, each number on a separate line from the MySQL command. Also, yes on the .sh appended to each one. I also edit the question! Thanks! @Gordon Davisson Commented Sep 22, 2017 at 7:11

2 Answers 2

2

You probably want something like this:

mysql -e "select A from xxx;" | while read A; do 
    whatever > standard_out 2>standard_error
    echo "$A.sh"
done | xargs parallel --joblog test.log --jobs 2 -k sh ::: 
Sign up to request clarification or add additional context in comments.

5 Comments

You can simplify this a bit by replacing the while loop with sed 's/$/.sh/'.
I simply stated an echo instead of the "whatever", assuming that some other commands might be executed as well. If that is not the case, the sed is indeed the way to go. (added whatever to the loop)
Could you enlighten more details on your solution, @GordonDavisson? Thanks.
@Chubaka If the only thing the loop is doing is appending ".sh" to each line, the sed command I gave will do the same thing. If it's doing more than that (e.g. writing to a file), then it won't work as a replacement for the loop.
@GordonDavisson ah i see. Yes, the while loop does more than appending .sh. This is a simplified case.
0

Thanks for enlightening me. xargs works perfectly here:

Assuming we have A.csv (mimic the mysql command)

1
2
3
4

We can simply do:

cat A.csv | while read A; do

echo "echo $A" > $A.sh

echo "$A.sh"

done | xargs -I {} parallel --joblog test.log --jobs 2 -k sh ::: {}

The above will print the following output as expected

1
2
3
4

Here -I {} & {} are the argument list markers:

https://www.cyberciti.biz/faq/linux-unix-bsd-xargs-construct-argument-lists-utility/

Comments

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.