Unix Shell编程:打印时添加一个空行 [英] Unix Shell Programming: Add a blank line when printing

查看:164
本文介绍了Unix Shell编程:打印时添加一个空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图列出目录中的所有文件,但是如何以空行分隔每个文件?基本上每个文件显示由一个空行分隔?我正在尝试使用for循环?我做了几个例子,但没有真正的工作间隔一个空行吗?

 为我在ls 

回声\ n&& ls -l
完成

为ls

echo\\\

ls
完成


解决方案



  find -printf'%p \\\
\\\
'



一个稍微差一点的(但更便携的):

  ls | sed's | $ | \\\
|'

更复杂的一个:

  ls |同时读f;做
echo$ f
echo
done

这是什么 你永远不应该做的:

  for`ls`中的f; do 
echo $ f
echo
done
code


编辑:

而且,正如

  for f in *;做
回声$ f
echo
完成

其中一个缺点是默认情况下,在许多shell * 中展开为自身,而不存在文件时为空字符串。


I am trying to list all the files in the directory but how would you separate each of the files by a blank line? basically each file displayed by separated by a blank line? I am trying to use a for loop? I did try few examples but none really work by spacing a blank lines in between?

for i in ls  
do
echo "\n" && ls -l
done

for i in ls  
do
echo "\n" 
ls 
done

解决方案

Here's one:

find -printf '%p\n\n'

A slightly worse (but more portable) one:

ls | sed 's|$|\n|'

A more convoluted one:

ls | while read f; do
    echo "$f"
    echo
done

And here is what you should not ever do:

for f in `ls`; do
    echo "$f"
    echo
done

EDIT:

And, as mentioned by Nija, the simple shell-only one:

for f in *; do
    echo "$f"
    echo
done

This one has the disadvantage that on many shells * by default expands to itself, rather than an empty string when no files exist.

这篇关于Unix Shell编程:打印时添加一个空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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