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

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

问题描述

我正在尝试将 cURL 的输出分配给一个变量,如下所示:

#!/bin/sh$IP=`curlautomation.whatismyip.com/n09230945.asp`回声 $IPsed s/IP/$IP/nsupdate.txt |更新

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

<块引用>

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

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

解决方案

在 shell 中,不要在要赋值的变量前放置 $.只有在引用变量时才使用 $IP.

#!/bin/bashIP=$(curl 自动化.whatismyip.com/n09230945.asp)回声$IP"sed "s/IP/$IP/" nsupdate.txt |更新

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

How can I get the output into $IP correctly?

解决方案

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天全站免登陆