【问题标题】:Issue reading parameterized argument in Jenkins在 Jenkins 中读取参数化参数的问题
【发布时间】:2019-04-05 17:39:40
【问题描述】:

我有一个带有以下参数变量的 Jenkins 工作。

source_host(字符串参数)dest_user(选择参数)备份(复选框参数)

我将 Jenkins 参数传递给 Jenkins Execute Shell,如下所示:

$ANSIBLE_SCRIPT_HOME/transfer.sh -a $source_host -b $dest_user -c $backup

下面是 transfer.sh 脚本。

while getopts ":a:b:c:" opt; do

  case $opt in

    a) source_host="$OPTARG"

    ;;

    b) dest_user="$OPTARG"

    ;;

    c) isbkup="$OPTARG"

    ;;

    \?) echo "Invalid option -$OPTARG" >&2

    ;;

  esac

done

printf "Argument source_host is %s\n" "$source_host"

printf "Argument dest_user is %s\n" "$dest_user"

printf "Argument backup is %s\n" "$backup"

Jenkins 版本:Jenkins 2.138.1 核心和库

Jenkins 2.145 及更早版本中存在多个安全漏洞,以及 LTS 2.138.1 及更早版本

如果我不选择 dest_user(选择参数),即空白并调用执行 shell,则 transfer.sh 的输出显示 $dest_user 的输入不正确

参数 source_host 是 user1

参数 dest_user 是 -c

参数备份是否

我希望 dest_user 在输出中不打印任何内容,而不是 -c。

【问题讨论】:

    标签: shell unix jenkins arguments parameter-passing


    【解决方案1】:

    如果“dest_user”为空甚至空白,则 $dest_user '消失'并且 -c 成为 -b 参数的值。我想你可能想试试

    $ANSIBLE_SCRIPT_HOME/transfer.sh -a "$source_host" -b "$dest_user" -c "$backup"
    

    然后,如果 $dest_user 为空,您将传递一个空字符串作为参数。区别在于:

    /path/to/ansible/transfer.sh -a source_host_value -b -c backup_value
    

    对比

    /path/to/ansible/transfer.sh -a "source_host_value" -b "" -c "backup_value"
    

    将你的脚本放入 x.sh,我得到了:

    prompt> $ ./x.sh -a a -b b -c c
    Argument source_host is a
    Argument dest_user is b
    Argument backup is c
    
    prompt> $ ./x.sh -a a -b -c c
    Argument source_host is a
    Argument dest_user is -c
    Argument backup is
    
    prompt> $ ./x.sh -a "a" -b "" -c "c"
    Argument source_host is a
    Argument dest_user is
    Argument backup is c
    

    【讨论】:

    • 谢谢它的帮助!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 2022-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多