【问题标题】:Removing backslash in windows env variables in shell在 shell 中删除 windows env 变量中的反斜杠
【发布时间】:2020-12-24 02:15:43
【问题描述】:

我在将环境变量正确传递给 shell 脚本时遇到问题。我在 Windows 机器上运行管道:

pipeline{
  environment{
    PATH="C:/Git/bin" //point to sh.exe
  }
  stages{
    stage{
      steps{
        sh "run.py $WORKSPACE"
      }
    }
  }
}

$WORKSPACE 有一个值C:\Jenkins\workspace\example,但是当我将它传递给脚本时,反斜杠被删除了C:Jenkinsworkspaceexample。如何克服这个问题并在 shell 中使用 Windows 环境变量?

【问题讨论】:

  • 遇到同样的问题 - 仅限 Windows。运气好吗?

标签: shell groovy environment-variables jenkins-pipeline sh


【解决方案1】:

我在我的 Linux 机器上重新创建了相同的内容。 $WORKSPACE 是 jenkins 环境变量。

这是工作流水线脚本:

pipeline {
   agent any

   stages {
      
      stage('Git Pull')
      {
         steps {
             echo "checkout"
         } 
      }
      stage('Run the python script') {
          steps {
              echo "$WORKSPACE"
              sh 'chmod 777 test.py' //given the executable permission to test.py script
              sh "python test.py $WORKSPACE"
          }
      }
   }
}

Python 脚本:

#!/usr/bin/python

import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

输出:

【讨论】:

  • 它可以在 Linux 上运行,但我在 Windows 上遇到了问题
猜你喜欢
  • 2023-03-22
  • 2016-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-23
  • 1970-01-01
  • 1970-01-01
  • 2011-02-23
相关资源
最近更新 更多