将输出分配给Bash中的变量 [英] Assign output to variable in Bash

查看:64
本文介绍了将输出分配给Bash中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将cURL的输出分配给这样的变量:

I'm trying to assign the output of cURL into a variable like so:

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

但是,当我运行脚本时,会发生以下情况:

However, when I run the script the following happens:

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

如何将输出正确地输入到$IP?

How can I get the output into $IP correctly?

推荐答案

在shell中,您不必在要分配的变量前放置$.仅在引用变量时使用$ IP.

In shell, you don't put a $ in front of a variable you're assigning. You only use $IP when you're referring to the variable.

#!/bin/bash

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

echo "$IP"

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

这篇关于将输出分配给Bash中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆