我正在尝试执行以下脚本来从文件中获取currentpath字符串,并将其传递给下一个命令中的查找和替换。当直接在主机服务器中运行时,它工作得很好,但当尝试从jenkins构建步骤执行时,我得到了一个失败,即找不到文件。
错误: sed:无法读取test.txt:没有这样的文件或目录
预期的结果是将"test.txt“文件更新为"newPath”,只要存在"currentPath“
代码:
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"
发布于 2019-07-19 04:35:27
其中一个问题是本地定义的变量newPath和filetoUpdate在远程主机上运行的脚本中不可用。此外,在此脚本中,只有currentPath=将在远程主机上执行,其余命令在本地运行。
我建议将脚本保存在一个单独的文件中。test.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
https://stackoverflow.com/questions/57083515
复制相似问题