分配输出到变量中击

问题描述 投票:134回答:2

我试图卷曲的输出分配到像这样的变量:

#!/bin/sh
$IP=`curl automation.whatismyip.com/n09230945.asp`
echo $IP
sed s/IP/$IP/ nsupdate.txt | nsupdate

然而,当我运行该脚本会发生以下情况:

./update.sh: 3: =[my ip address]: not found

我怎样才能得到输出到$IP正确?

bash curl
2个回答
255
投票

在外壳,你不要把$你要指定一个变量的前面。您只使用$ IP当你指的是可变的。

#!/bin/bash

IP=$(curl automation.whatismyip.com/n09230945.asp)

echo "$IP"

sed "s/IP/$IP/" nsupdate.txt | nsupdate

22
投票

同样的,更复杂的东西......从实例中得到EC2实例区域。

INSTANCE_REGION=$(curl -s 'http://169.254.169.254/latest/dynamic/instance-identity/document' | python -c "import sys, json; print json.load(sys.stdin)['region']")

echo $INSTANCE_REGION
© www.soinside.com 2019 - 2024. All rights reserved.