BASH:从字符串地带换行符(读线) [英] BASH: Strip new-line character from string (read line)

查看:156
本文介绍了BASH:从字符串地带换行符(读线)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到以下问题:我在写一个Linux的bash脚本,执行以下操作:

I bumped into the following problem: I'm writing a Linux bash script which does the following:


  • 从文件中读取行

  • 剥去从行的末尾 \\ n 字符只是读

  • 执行这就是在那里指挥

例如:
commands.txt中

Example: commands.txt

ls
ls -l
ls -ltra
ps as

在bash文件的执行应该得到的第一行,并执行它,但同时 \\ n present,外壳只是输出命令未找到:LS
剧本的那部分看起来像这样

The execution of the bash file should get the first line, and execute it, but while the \n present, the shell just outputs "command not found: ls" That part of the script looks like this

 read line

        if [ -n "$line" ]; then #if not empty line

                #myline=`echo -n $line | tr -d '\n'`
                #myline=`echo -e $line | sed ':start /^.*$/N;s/\n//g; t start'`

                myline=`echo -n $line | tr -d "\n"`
                $myline  #execute it

                cat $fname | tail -n+2 > $fname.txt
                mv $fname.txt $fname
        fi

评论你有我问SO尝试过的事情。任何解决方案?我砸我的脑子超过这个最后的几个小时...

Commented you have the things I tried before asking SO. Any solutions? I'm smashing my brains for the last couple of hours over this...

推荐答案

我总是喜欢的perl -ne'格格和打印,修整换行。尼斯和容易记住。

I always like perl -ne 'chomp and print' , for trimming newlines. Nice and easy to remember.

例如。 的ls -l | perl的-ne格格和打印

然而

我不认为这是在这里你的问题虽然。虽然我不知道我知道你是如何传递的命令文件中,通过在你的shell脚本的读

I don't think that is your problem here though. Although I'm not sure I understand how you're passing the commands in the file through to the 'read' in your shell script.

使用我自己的像这样(test.sh)测试脚本

With a test script of my own like this (test.sh)

read line
if [ -n "$line" ]; then
  $line
fi

和示例输入文件像这样(test.cmds)

and a sample input file like this (test.cmds)

ls 
ls -l
ls -ltra

如果我运行它像这样 ./ test.sh< test.cmds ,我看到了预期结果,这是在当前目录运行的第一个命令ls'的。

If I run it like this ./test.sh < test.cmds, I see the expected result, which is to run the first command 'ls' on the current working directory.

也许你的输入文件中有额外的非打印字符?

Perhaps your input file has additional non-printable characters in it ?

矿山看起来像这样

od -c test.cmds 
0000000    l   s      \n   l   s       -   l  \n   l   s       -   l   t
0000020    r   a  \n                                                    
0000023

从下面您的意见,我怀疑你可能在你的输入文件,这是不一样的东西作为一个换行符回车(\\ r)。是输入文件最初在DOS格式?如果是这样,那么你就需要2字节的DOS行结束转换为\\ r \\ n来单字节UNIX之一,\\ n来达到预期的效果。

From your comments below, I suspect you may have carriage returns ( "\r" ) in your input file, which is not the same thing as a newline. Is the input file originally in DOS format ? If so, then you need to convert the 2 byte DOS line ending "\r\n" to the single byte UNIX one, "\n" to achieve the expected results.

您应该能够在你的任何注释行交换的\\ n表示为\\ r来做到这一点。

You should be able to do this by swapping the "\n" for "\r" in any of your commented out lines.

这篇关于BASH:从字符串地带换行符(读线)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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