UNIX:如何使用格式打印变量的内容。(UNIX: How to print contents of variable with formatting.)

编程入门 行业动态 更新时间:2024-10-23 04:54:49
UNIX:如何使用格式打印变量的内容。(UNIX: How to print contents of variable with formatting.)

我只想以命令行(新行上的每个文件)的相同方式打印ls -l。 我已经找到了解决方案的每一个方面,并且知道我的解决方案应该有效,但由于某些原因它没有。 我努力了:

#!/bin/csh set list = `ls -l` echo "$list"

和:

#!/bin/csh set list = "`ls -l`" echo "$list"

没有运气。 我真正想要做的是在ls -l之后使用grep(所以maby我会发生这种错误),但我不能,因为它将列表打印为一条长行。 (是的,我必须使用csh)

I just want to print ls -l the same way it looks from the command line (each file on a new line). I have looked every where for a solution and know my solution should work but for some reason it doesn't. I have tried:

#!/bin/csh set list = `ls -l` echo "$list"

and:

#!/bin/csh set list = "`ls -l`" echo "$list"

with no luck. What I really want to do is use grep on ls -l later (so maby I'm going about this wrong), but I can't because it prints list as one long line. (and yes, I have to use csh)

最满意答案

我不知道如何解决csh在echo时将单词连接在一起的行为,但是你可以使用类似数组的功能和循环。 例如:

#!/bin/csh set list=( "`printf 'a\nb\nc\n'`" ) echo "count=$#list" echo "2 = $list[2]" echo set n=0 while ( $n < $#list ) @ n += 1 echo "$n : $list[$n]" end

这对我来说产生了输出:

count=3 2 = b 1 : a 2 : b 3 : c

请注意,我在FreeBSD上使用tcsh。 您的csh可能不同,您没有提到您的平台。

要将其恢复到文件列表问题,您可以使用类似的循环复制您正在寻找的输出:

#!/bin/csh set list=( "`ls -l`" ) set n=0 while ( $n < $#list ) @ n += 1 echo "$list[$n]" end

这里重要的考虑因素是在(命令替换)反引号( `...` )中,输出由空格分隔,而在双引号( "..." )内,输出由换行按字分隔。

那说......

我真正想要做的是在ls -l之后使用grep(所以maby我会发生这种错误),但我不能,因为它将列表打印为一条长行。

完全可能! :-)但是如果没有完全理解你想要解决的潜在问题,帮助你实现解决方案是我们能做的最好的事情。 当心可怕的XY问题 。

I don't know how to get around csh's behaviour of joining words together when you echo, but you may be able to use array-like functionality and a loop. For example:

#!/bin/csh set list=( "`printf 'a\nb\nc\n'`" ) echo "count=$#list" echo "2 = $list[2]" echo set n=0 while ( $n < $#list ) @ n += 1 echo "$n : $list[$n]" end

Which for me produces the output:

count=3 2 = b 1 : a 2 : b 3 : c

Note that I'm using tcsh on FreeBSD. Your csh may be different, you haven't mentioned your platform.

To bring this back to your list of files question, you can replicate the output you're looking for with a similar loop:

#!/bin/csh set list=( "`ls -l`" ) set n=0 while ( $n < $#list ) @ n += 1 echo "$list[$n]" end

The important consideration here is that within (command substitution) backquotes (`...`), output is word-separated by whitespace, whereas inside double quotes ("..."), output is word-separated by newlines.

That said...

What I really want to do is use grep on ls -l later (so maby I'm going about this wrong), but I can't because it prints list as one long line.

Entirely possible! :-) But without a full understanding of the underlying problem you're trying to solve, helping you achieve your solution is the best we can do. Beware the dreaded XY Problem.

更多推荐

本文发布于:2023-08-06 08:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446677.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   变量   格式   内容   UNIX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!