Pan*_*das 6 command-line bash scripts printing
我在文件伞形 31_*.xvg 上运行“ls -lX”。我运行一个命令来搜索 ls 命令输出的第五列上的数字,比 20000 大。它看起来像这样:
ls -lX umbrella31_*log | awk '{if($5 >=20000) {print}}' | wc -l
Run Code Online (Sandbox Code Playgroud)
并输出一个数字(第 5 列中的数字 > 20000 的行数)。
当我在脚本中包含上述命令时:
#!/bin/bash -x
ls -lX umbrella31_*log | awk '{if($5 >=20000) {print}}' | wc -l
Run Code Online (Sandbox Code Playgroud)
并运行它,我在屏幕上也看到了“ls”的打印结果(我不想要)。我怎样才能让我的脚本像我的屏幕命令一样运行,并且只打印所需的行数?
bac*_*c0n 17
您的脚本会将管道中的每个命令打印到终端,因为您正在使用-x
标志运行它。来自man bash
:
-x Print commands and their arguments as they are executed.
Run Code Online (Sandbox Code Playgroud)
但是,您使用ls
和 的wc
方法并不是计算文件的最佳方法。
要查找 >= 20000 的文件,您可以使用find:
find -type f -maxdepth 1 -name 'umbrella31_*log' -size +19999c -ls
Run Code Online (Sandbox Code Playgroud)
(因为 find interpreters + sign (大于四舍五入)你得到 n+1,因此是奇数
-size n
)
count 输出:(
当我们计算文件时,我们只打印一个换行符,因为我们真的不需要输出)
wc -l < <(find -maxdepth 1 -type f -name 'umbrella31_*log' -size +19999c -printf \\n)
Run Code Online (Sandbox Code Playgroud)
-maxdepth n
在起始点以下的目录的大多数级别(非负整数)级别下降。
-size n
文件使用 n 个空间单位,四舍五入。
-ls
在标准输出中以 ls -dils 格式列出当前文件。