簡體   English   中英

bash 無法將變量導出到腳本

[英]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'
  • "@'; filename=$(echo $userlistlocation | sed -E 's/./ (. )$/\\1/') ; echo $filename ; echo filename='@"
  • '$文件名'
  • "'; chmod +x uuidwithduplicate.sh; ./uuidwithduplicate.sh"

注意:在第三個標記上,在雙引號和單引號之間添加了“@”以使其更具可讀性。 不是命令的一部分。

表面上有幾個問題:

  • '$thread_size'應該是"$thread_size"以啟用擴展
  • 'echo $filename' 是雙引號,導致在本地主機上擴展,其中設置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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM