我应该只用这种结构回显文件或目录的名称:
ls -Al | while read string
do
...
done
Run Code Online (Sandbox Code Playgroud)
ls -Al
输出 :
drwxr-xr-x 12 s162103 studs 12 march 28 12:49 personal domain
drwxr-xr-x 2 s162103 studs 3 march 28 22:32 public_html
drwxr-xr-x 7 s162103 studs 8 march 28 13:59 WebApplication1
Run Code Online (Sandbox Code Playgroud)
例如,如果我尝试:
ls -Al | while read string
do
echo "$string" | awk '{print $9}
done
Run Code Online (Sandbox Code Playgroud)
然后只输出没有空格的文件和目录。如果文件或目录有“个人域”之类的空格,则只有“个人”一词。
我需要非常简单的解决方案。也许有比 awk 更好的解决方案。
我正在用 ksh 编写脚本。我需要找到所有拥有超过 N 个进程的用户并在 shell 中回显它们。N 从 ksh 中读取。
我知道我应该使用ps -elf
,但是如何解析它,找到具有 >N 个进程的用户,并用它们创建一个数组?我在 ksh 中遇到了数组问题。也许一个简单的解决方案可以帮助我,而不必创建一个数组。一个人推荐我使用
ps -elf | awk '{a[$3]++;}END{for(i in a)if (a[i]>N)print i, a[i];}' N=3
Run Code Online (Sandbox Code Playgroud)
但它不能正常工作。
为什么read
比 慢getc
?
例如,这个:
for (;;) {
chr++;
amr=read(file1, &wc1, 1);
amr2=read(file2, &wc2, 1);
if (wc1 == wc2) {
if (wc1 == '\n')
line++;
if (amr == 0) {
if (eflg)
return (1);
return (0);
}
continue;
}
Run Code Online (Sandbox Code Playgroud)
比这慢:
for (;;) {
chr++;
c1 = getc(file1);
c2 = getc(file2);
if (c1 == c2) {
if (c1 == '\n')
line++;
if (c1 == EOF) {
if (eflg)
return (1);
return (0);
}
continue;
}
Run Code Online (Sandbox Code Playgroud)
getc
使用read
系统调用,为什么它更慢?
我必须解析ls -Al
输出并获取文件或目录名称
ls -Al
输出 :
drwxr-xr-x 12 s162103 studs 12 march 28 2012 personal domain
drwxr-xr-x 2 s162103 studs 3 march 28 22:32 public_html
drwxr-xr-x 7 s162103 studs 8 march 28 13:59 WebApplication1
Run Code Online (Sandbox Code Playgroud)
我应该只使用 ls -Al | <something>
例如:
ls -Al | awk '{print $8}'
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为$8
如果目录名称中有空格,则不是名称,它是名称的一部分。也许有一些实用程序可以删除姓氏或删除以前的任何内容?