AWK与LS和可变的bash [英] awk in bash with ls and variable
问题描述
我想打印一个特定的目录中的文件只有名称:
这样一来它的工作原理:
LS -g --sort =大小-R /箱| AWK'{打印$ 8 $ 9 $ 10,$ 11 $ 12 $ 13}'
但如果我读了路径变量它不工作:
读取路径
LS -g --sort =大小-r $(PATH)| AWK'{打印$ 8 $ 9 $ 10,$ 11 $ 12 $ 13}'
命令'的awk'是提供'在/ usr /斌/的awk'
它应该是:
LS -g --sort =大小-r $ {PATH} | AWK'{打印$ 8 $ 9 $ 10,$ 11 $ 12 $ 13}'
注意花括号。
使用 $(..)
,它会执行指定的命令/功能 PATH
并替换结果,这是不是你想要的。
注意 PATH
是一个变量名可怜的选择,因为它会覆盖具有相同名称的系统变量,使系统命令不可用(因为原来的 PATH
现在已经消失)。我建议你使用其他名称,如 my_path的
:
阅读my_path的
LS -g --sort =大小-r $ {my_path的} | AWK'{打印$ 8 $ 9 $ 10,$ 11 $ 12 $ 13}'
I wanted to print only the name of files of a specific directory: In this way it works:
ls -g --sort=size -r /bin | awk '{print $8,$9,$10,$11,$12,$13}'
but if I read the path variable it doesn't work:
read PATH
ls -g --sort=size -r $(PATH) | awk '{print $8,$9,$10,$11,$12,$13}'
Command 'awk' is available in '/usr/bin/awk'
It should be:
ls -g --sort=size -r ${PATH} | awk '{print $8,$9,$10,$11,$12,$13}'
Notice the curly braces.
With $(..)
, it'll execute the command/function named PATH
and substitute the result, which is not what you want.
Note that PATH
is the poor choice for a variable name as it will overwrite the system variable with the same name and make the system commands unavailable (since the original PATH
has gone now). I suggest you use some other name such as my_path
:
read my_path
ls -g --sort=size -r ${my_path} | awk '{print $8,$9,$10,$11,$12,$13}'
这篇关于AWK与LS和可变的bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!