简体   繁体   English

Bash脚本打印错误值

[英]Bash script is printing the wrong value

I've created a script that seperates the IP that it finds, based on NIC card name input. 我已经创建了一个脚本,用于根据NIC卡名称输入来分隔找到的IP。

#!/bin/bash
echo what is your NIC?
read NIC
IP=`ifconfig $NIC 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'`
NEWSTRING=${IP:0:6}
ALPHARETTA="12.101"
EUFAULA="12.102"

if [ "${NEWSTRING}" = "${ALPHARETTA}" ] ; then
  echo I'm in Alpharetta
else
  echo I'm in Eufaula
fi

If eth0 were to be 12.101.1.1 it would only take (12.101) 如果eth0为12.101.1.1,则只需(12.101)

I'm comparing 12.101 and 12.101 for my tests... and i'm getting this echo back.... 我正在比较12.101和12.101进行测试...并且正在得到回显...。

what is your NIC?
eth0
Im in Alpharetta
else
  echo Im in Eufaula

I'm obviously doing something silly, and not seeing it.. could somebody point me in the correct direction? 我显然在做一些愚蠢的事情,却没有看到..有人可以指出我正确的方向吗?

The bash parser is seeing the apostrophe characters you are trying to echo in the word "I'm" and it thinks you are trying to print one long string that spans from line 10 to line 12 of your script. bash解析器看到您想在单词“ I'm”中回显的撇号字符,并且认为您正在尝试打印一个跨越脚本第10行至第12行的长字符串。 You can even see how the syntax highlighting on this site is also indicating a problem. 您甚至可以看到此站点上突出显示的语法也表明存在问题。 You should wrap the message you are echoing in quotes. 您应该将要回显的消息用引号引起来。 For example: 例如:

echo "I'm in Alpharetta"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM