简体   繁体   中英

Script to execute commands in sequence php

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

eg 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM