0

How could we execute a program with "dynamic arguments" (loop) ?

I would like to add an argument as per user interest/input. I can do it by several if conditions, But it is not practical. Arg1, Arg3, and Arg4 might be needed for a program (eg: docker run ) but Arg2 and Arg5 are not.

For example:

docker run -d --name $CONTAINER_NAME \
               --hostname $MY_PROFILE \  ## if $hostname_req=1
               --user $USER \  
               -v $MYAPP_DL:/home/$USER/Downloads \  ## if $dwnldfolder_req=1
               --net-alias=${MY_NEW_DNS_NAME} \   ## if $dns_req=1
               --label "appname=$MYAPP" \  ## if $label_req=1
               --label "profile=$DOMAIN_CODE" \
               --label "purpose=$PURPOSE" \
               --label "maintainer=$DEV_ID" \ 
               $DOCKER_IMAGE_TAG

We can use function and loop but how will substitute to Program ( docker run ) arguments?

    ## NOT ACTUAL CODE :: ILLUSTRATING PLAN1

    if [$label_req==1]
    argument_aray+=('--label "appname=$MYAPP"')
    if [$dns_req==1]
    argument_aray+=('-net-alias=${MY_NEW_DNS_NAME} ')
    ...
    ...
    if [$hostname_req==1]
    argument_aray+=('-hostname $MY_PROFILE')

    docker run <<argument_aray>>

I couldn't find any dynamic argument constructor for the shell. Please suggest if anyone has any idea how can achieve this

ilkkachu
  • 133,243
  • 15
  • 236
  • 397
JOEL
  • 1
  • 1
  • Are you perhaps looking for [How can I conditionally pass an argument from a POSIX shell script?](https://unix.stackexchange.com/q/562870/170373) or [How can we run a command stored in a variable?](https://unix.stackexchange.com/q/444946/170373)? Or [How can I expand a quoted variable to nothing if it's empty?](https://unix.stackexchange.com/q/415990/170373) – ilkkachu Jan 31 '23 at 18:48
  • 1
    This definitely can't be [tag:posix] since you're already using arrays. – muru Feb 01 '23 at 10:28
  • not [tag:linux]-specific either – ilkkachu Feb 01 '23 at 10:31
  • @JOEL, did you look at the linked posts? – ilkkachu Feb 01 '23 at 10:32
  • @ilkkachu Thanks for the link. ```argv+=( --enhancement "$2" ) && python evaluate.py "${argv[@]}" ```. Working fine for me – JOEL Feb 02 '23 at 07:55

0 Answers0