分配输出变量猛砸
问题描述:
我试图卷曲的输出分配到一个变量,像这样:分配输出变量猛砸
#!/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
?
答
在shell中,您不会在要分配的变量前放置$。当你引用变量时,你只使用$ IP。
#!/bin/bash
IP=$(curl automation.whatismyip.com/n09230945.asp)
echo "$IP"
sed "s/IP/$IP/" nsupdate.txt | nsupdate
答
与更复杂的东西一样...从实例中获取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
+0
这正是我在我的bash脚本中所做的,并且使我无需apt-get install jq! – Nungster 2017-10-31 18:52:59
有没有一种方法可以抑制'curl'的输出和进度条?添加'-silent'会让'$ IP'空...... – Dror 2014-07-14 08:30:40
@Dror,'curl'将其嘈杂的输出发送到stderr,所以在这样的脚本的情况下应该忽略进度条。尽管如此,'--silent'或'-s'工作得很好。如果你有麻烦,请[提问](http://*.com/questions/ask)。 – ghoti 2014-07-14 15:46:17
curl -sS在较新版本的curl中工作 – 2016-08-03 10:04:25