[英]BASH: How to add text in the same line after command
I have to print number of folders in my directory, so i use ls -l $1| grep "^d" | wc -l
我必须打印目录中的文件夹数,所以我使用
ls -l $1| grep "^d" | wc -l
ls -l $1| grep "^d" | wc -l
ls -l $1| grep "^d" | wc -l
after that, I would liked to add a text in the same line. ls -l $1| grep "^d" | wc -l
之后,我想在同一行中添加文本。 any ideas? 有任何想法吗?
Assign the result to a variable, then print the variable on the same line as the directory name. 将结果分配给变量,然后在与目录名称相同的行上打印该变量。
folders=$(ls -l "$1" | grep "^d" | wc -l)
printf "%s %d\n" "$1" "$folders"
Also, remember to quote your variables, otherwise your script won't work when filenames contain whitespace. 另外,请记住引用变量,否则当文件名包含空格时,脚本将无法工作。
如果您不想使用变量来保存输出,则可以使用echo并将命令放在该echo行的$()中。
echo $(ls -l $1| grep "^d" | wc -l ) more text to follow here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.