0

I'm trying to figure out how to write a script that will essentially execute one after the other (provided the previous command has finished).

php bin/console SOME:CUSTOM:COMMAND <PART_1_ARGUMENT> <PART_2_ARGUMENT> --env=xxx
  • The following is fixed and I need to pass in say a list of variables in-between
    • php bin/console COMMAND --env=xxx

e.g. lets say I have the following as my arguments

  • Apple pie
  • Apple crumble
  • Pear apple

then my command would work in order executing one after the other like

php bin/console COMMAND Apple pie --env=xxx 

and then right after

php bin/console COMMAND Apple crumble --env=xxx 

and so forth ?

Any info is helpful I've googled for hours .. (newbie)

3
  • 1
    Just put the two commands right after each other in a shell script. Commented Aug 10, 2018 at 17:07
  • Can't you just pass them as a list of arguments, and in your PHP script, loop through the inputs and call your logic/function for each input. Commented Aug 10, 2018 at 17:13
  • Not really sure if it's exactly a "duplicate", but see this question which a quick search turned up. Commented Aug 10, 2018 at 17:15

1 Answer 1

1

You can use a loop:

for args in "Apple pie" "Apple crumble" "Pear apple"
do
    php bin/console COMMAND $args --env=xxx
done

One caveat: this won't work if any of the arguments can contain whitespace or wildcard characters. You can't quote $args in the command line, because you need Apple and pie to be separate arguments. It wouldn't work to put quotes in the strings, because quotes aren't parsed when expanding variables. You could do it with two arrays:

arg1=("Apple" "Apple" "Pear")
arg2=("pie" "crumble" "apple")

for (( i=0; i < ${#arg1[@]}; i++ ))
do
    php bin/console COMMAND "${arg1[$i]}" "${arg2[$i]}" --env=xxx
done
Sign up to request clarification or add additional context in comments.

3 Comments

very useful! I compared what I had, I had a bunch on extra brackets etc. But knowing that about the "whitespace" I didn't know. It's 90% there I will work on trying to get it done because the loop doesn't fully cycle. It stops after the first one :)
I just removed the let i=i+1 And the rest fell into place .. Thanks for your support @Barmar
that was left over from an earlier while loop

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.