How to add an extra argument conditionally in bash script?

后端 未结 2 460
一整个雨季
一整个雨季 2021-01-03 12:04

There\'s a script that calls the other program. The following is program.sh. It might look non-sense but I\'m omitting lots of details and… Let\'s say I wanna s

相关标签:
2条回答
  • 2021-01-03 12:42

    Use an array.

    cmd=(/usr/bin/foo -A -B -C)
    if somecond; then
      cmd+=(-X)
    fi
    "${cmd[@]}"
    
    0 讨论(0)
  • 2021-01-03 12:58

    You could add a conditional. I'm sure there are less repetitive ways to do it but this should work:

    #!/bin/bash
    
    function run_this {
        if [[ "$1" = "--quiet" ]]; then
           /usr/bin/foo -A -B -C -X
        else 
           /usr/bin/foo -A -B -C
        fi
    }
    run_this "$1"
    
    0 讨论(0)
提交回复
热议问题