变量用“echo"覆盖文本问题在 Bash 中 [英] Variables overwriting text problem with "echo" in Bash

查看:58
本文介绍了变量用“echo"覆盖文本问题在 Bash 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 OS X 10.6.5,Bash.

I use OS X 10.6.5, Bash.

当我运行这个时:

echo $IP; echo of; echo $IPLINES

我得到这个输出:

219.80.4.150:3128
of
1108

当我运行这个时:

echo $IP of $IPLINES

我得到这个输出:

 of 1108.150:3128

我希望得到:

219.80.4.150:3128 of 1108

什么会导致我得到的输出失真?

What would cause the distorted output I am getting?

实际脚本是这样的:

#!/bin/bash

IPLINES=`cat a.txt | wc -l | awk '{print $1}'`

if [ $IPLINES > 1 ]; then
    LINE=`expr $RANDOM % $IPLINES + 1`
    IP=`head -$LINE a.txt | tail -1`
    sed -e "${LINE}d" -i .b a.txt

    echo $IP of $IPLINES
fi

推荐答案

这里有一个疯狂的猜测:您正在从 .txt 文件中提取 IP 变量——如果它是 Windows 文件,或者是 Windows 风格的编码,则行结束使用 .您去掉了换行符,但是如果其中有一个 使您回到行首怎么办?

A wild guess here: you are extracting the IP variable from a .txt file -- if that's a Windows file, or is encoded Windows-style, lines end with . You take the newline away, but what if there's a in it that's making you go back to the beginning of the line?

不问任何问题的快速修复:使用 echo -n,它会抑制回显文本末尾的换行符.

Quick dirty fix with no questions asked: use echo -n, it supresses the newline at the end of the echoed text.

echo -n $IP; echo -n of; echo -n $IPLINES

如果问题仍然存在,可能是我上面说的.尝试右修剪 $IP.

If the problem persists, it's probably what I said above. Try right-trimming $IP.

没有看到 OSX 部分,抱歉.在 OSX 中,行以 结尾——这一定是问题所在.

didn't see the OSX part, sorry. In OSX, lines end with -- that must be the issue.

这篇关于变量用“echo"覆盖文本问题在 Bash 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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