我只想以命令行(新行上的每个文件)的相同方式打印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]" endWhich for me produces the output:
count=3 2 = b 1 : a 2 : b 3 : cNote 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]" endThe 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.
更多推荐
发布评论