[英]bash unable to export the variable to script
我被我的一段代码困住了任何帮助表示赞赏。 这是我从 jenkins 执行的一段代码。
#!/bin/bash
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value|[0],PrivateIpAddress]' --output text | column | grep devtools > devtools
ip=`awk '{print $2}' devtool`
echo $ip
ssh ubuntu@$ip -n "aws s3 cp s3://bucket/$userlistlocation . --region eu-central-1"
cd builds/${BUILD_NUMBER}/
scp * ubuntu@$ip:/home/ubuntu
if [ $port_type == "normal" ]; then
if [ $duplicate_value == "no" ]; then
if [ $userlist == "uuid" ]; then
ssh ubuntu@$ip -n "export thread_size='"'$thread_size'"'; filename=$(echo $userlistlocation | sed -E 's/.*\/(.*)$/\1/') ; echo $filename ; echo filename='"'$filename'"'; chmod +x uuidwithduplicate.sh; ./uuidwithduplicate.sh"
fi
fi
fi
fi
userlistlocation --> 是一个用户输入,它可以是任何格式/rahul/december/file.csv 或者它可以是file.csv。
通过 sed 命令,我能够获取输出并存储在“文件名”变量中。
但是当我尝试 echo $filename 它打印为 echo $filename 它应该打印为 file.csv
这个 file.csv 将是另一个要运行的脚本的源文件,即 uuidwithduplicate.sh
userlistlocation 和 thread_size 都是通过 Jenkins 作业参数指定的。
我在导出 thread_size 时没有遇到问题,唯一的问题是文件名。
它只是打印 echo $filename --> 它应该打印 file.csv
分解ssh
命令:
ssh ubuntu@$ip -n "export thread_size='"'$thread_size'"'; filename=$(echo $userlistlocation | sed -E 's/.*\/(.*)$/\1/') ; echo $filename ; echo filename='"'$filename'"'; chmod +x uuidwithduplicate.sh; ./uuidwithduplicate.sh"
分为单/双引号项
注意:在第三个标记上,在双引号和单引号之间添加了“@”以使其更具可读性。 不是命令的一部分。
表面上有几个问题:
'$thread_size'
应该是"$thread_size"
以启用扩展filename=$(echo ...)
在远程主机上执行。echo
,不知道为什么建议的解决方案是将filename的设置移动到本地主机(简化命令),并将thread_size移动到双引号中。 可以将完整的命令放入单个双引号项中:
filename=$(echo $userlistlocation | sed -E 's/.*\/(.*)$/\1/')
ssh localhost -n "export thread_size='$thread_size'; echo '$filename' ; echo filename='$filename'; chmod +x uuidwithduplicate.sh; ./uuidwithduplicate.sh"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.