0

我试图执行以下脚本以从文件中获取当前路径字符串并将其传递给在下一个命令中查找和替换。直接在主机服务器中运行时它工作正常,但是当尝试从 jenkins 构建步骤执行时,我遇到了找不到文件的失败。

错误:sed:无法读取 test.txt:没有这样的文件或目录

预期的结果是在“currentPath”存在的地方用“newPath”更新“test.txt”文件

代码 :

user="testuser"
host="remotehost"
newPath="/testpath/"
filetoUpdate="./test.txt" # this is a file 
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ${user}@${host} "currentPath="$(sed -n '/^PATH='/p $filetoUpdate | cut -d'=' -f2)" ; echo  "currentPath was "$currentPath"" ; sed -n 's|$currentPath|$newPath|g' $filetoUpdate"
4

1 回答 1

0

问题之一是本地定义的变量 newPath 和 filetoUpdate 在远程主机上运行的脚本中不可用。同样在此脚本中,仅 currentPath= 将在远程主机上执行,其余命令在本地运行。

我建议将脚本保存在单独的文件中。测试.sh:

newPath="/testpath/"
filetoUpdate="./test.txt" 
currentPath=`sed -n '/^PATH=/p' $filetoUpdate | cut -d= -f2`  
echo  "currentPath was "$currentPath""  
sed -i .bak "s|$currentPath|$newPath|g" $filetoUpdate

(我添加了 -i 用于现场编辑)并使用命令运行它

ssh remotehost < t.sh
于 2019-07-18T20:35:26.797 回答