Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Bash命令行中创建if语句(不是脚本)_Bash_Jenkins - Fatal编程技术网

在Bash命令行中创建if语句(不是脚本)

在Bash命令行中创建if语句(不是脚本),bash,jenkins,Bash,Jenkins,我试图在bash命令行中创建一个if语句,不是一个脚本,而是一个可以在命令行中键入而不返回的语句,因为我必须通过groovy命令行调用来运行这个脚本 if [cat $(find ./ -name userId.txt) == "517980"]; then cat $(find ./ -name userId.txt); fi Jenkins groovy脚本如下所示 node("puppet-$ENVIRONMENT") { sh "/opt/puppet/bin/puppet mod

我试图在bash命令行中创建一个if语句,不是一个脚本,而是一个可以在命令行中键入而不返回的语句,因为我必须通过groovy命令行调用来运行这个脚本

if [cat $(find ./ -name userId.txt) == "517980"]; then cat $(find ./ -name userId.txt); fi
Jenkins groovy脚本如下所示

node("puppet-$ENVIRONMENT") {
  sh "/opt/puppet/bin/puppet module uninstall ${module} || echo 'NOT INSTALLED!'"
  sh "pwd"
  sh "rm -rf *"
  //unarchive the tar in the remote file system and install it
  unarchive mapping: ['*.*': './']
  sh 'if [cat $(find ./ -name userId.txt) == "517980"]; then echo "it works"; fi'
  sh "ls -alrt"
  sh '/opt/puppet/bin/puppet module install --force $(find ./ -name *.tar.gz)'
}
  • 无需多次运行
    find
    cat
  • [
    实际上是一个命令,而不仅仅是语法:它需要一个空格将其与参数分隔开
  • 您的
    cat
    呼叫周围缺少
    $()

错误?异常?似乎是什么问题?和
sh
类似
['sh','-c',cmd]。execute()
,对吗?问题出在bash而不是groovy,我需要一个单行命令来比较bash中的两个字符串,
。[cat…
(在
周围的空格中)[
壳牌公司不应该已经抱怨了吗?
sh“rm-rf*”
--yikes!最好绝对确保您当前的目录是您认为的目录!格伦·杰克曼:我感谢您的关注,这是一个概念证明。一旦我能让它工作起来,我会妥善清理它。感谢您的回答,我想我更了解如何在bashI中构建单行脚本。我不确定OP是什么试图实现,但它不是(几乎)等同于此吗?
find./-name userId.txt-type f-exec sh-c'read-r line<“$1”[“$line”=517980]&echo“$line”{}\-退出
file=$(find ./ -name userId.txt) && [ -n "$file" ] && { contents=$(cat "$file"); [ "$contents" == "517980" ] && echo "$contents"; }