您如何在C-shell echo命令中包含换行符? [英] How would you include newline characters in a C-shell echo command?

查看:445
本文介绍了您如何在C-shell echo命令中包含换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来非常简单,而且与其他外壳程序一样。但我似乎无法弄清楚如何获取回显以显示换行符。例如-

This sounds ridiculously easy, and it is with other shells. But I can't seem to figure out how to get echo to display newlines. For example -

cat myFile

显示文件实际存在的位置,这就是我想要的-

shows the file as it actually exists, which is what I want -

this
is
my
file

而我的脚本包含以下内容-

whereas my script, which contains the following -

#!/bin/csh
set var = `cat myFile`
echo "$var"

删除所有换行符,这不是我想要的-

removes all the newlines, which is not what I want -

this is my file

感谢

推荐答案

问题不在于 echo 命令,它与csh的反引号处理有关。当您执行

The problem isn't with the echo command, it's with csh's handling of backticks. When you execute

set var = `cat myFile`

myfile 中的换行符永远不会存储在 $ var 中;它们将转换为空格。我想不出任何强制csh变量包含从文件读取的换行符的方法,尽管可能有一种方法。

the newlines from myfile are never stored in $var; they're converted to spaces. I can't think of any way to force a csh variable to include newlines read from a file, though there might be a way to do it.

sh及其派生类表现出您想要的方式。例如:

sh and its derivatives do behave the way you want. For example:

$ x="`printf 'foo\nbar'`"
$ echo $x
foo bar
$ echo "$x"
foo
bar
$ 

赋值中的双引号引起换行符(最后一行除外)被保留。 echo $ x 用空格替换换行符,但是 echo $ x 保留空格。

The double quotes on the assignment cause the newlines (except for the last one) to be preserved. echo $x replaces the newlines with spaces, but echo "$x" preserves them.

您最好的选择是做其他事情,而不是尝试将文件内容存储在变量中。您在一条评论中说,您正在尝试发送包含日志文件内容的电子邮件。因此,将文件内容直接馈送到您使用的任何邮件命令。我没有所有的细节,但是可能看起来像这样:

Your best bet is to do something other than trying to store the contents of a file in a variable. You said in a comment that you're trying to send an e-mail with the contents of a log file. So feed the contents of the file directly to whatever mail command you're using. I don't have all the details, but it might look something like this:

( echo this ; echo that ; echo the-other ; cat myFile ) | some-mail-command

强制性引用: http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

这篇关于您如何在C-shell echo命令中包含换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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