简体   繁体   English

BASH:如何在命令后的同一行中添加文本

[英]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.

相关问题 如何将文本添加到同一行? - How can I add text to the same line? 如何在bash中的同一行添加来自不同来源的内容 - How to add content from different sources on the same line in bash 如何使用 bash 在文件中间添加一行文本? - How do I add a line of text to the middle of a file using bash? bash - 如何在 xml 文件的第二行之后的 append 文本? - bash - how to append text after the 2nd line of a xml file? 在 Bash 中,如何在文件的每一行之后添加一个字符串? - In Bash, how do I add a string after each line in a file? 在bash中的行号上插入文本后面的文本 - insert a text after text on line number in bash 在 bash 文件中的 # /bin/bash 行之后插入字符串/源命令 - Insert string/source command after #!/bin/bash line in a bash file Bash脚本-如何在文本文件中匹配模式后在第二行插入变量值 - Bash script - how to insert value of variable as new line, 2 line after matching a pattern in text file 围绕nc命令编写的bash脚本。 如何在每行之前添加文字? - A bash script written around the nc command. How do I prepend text before each line? 如何通过ftp连接获取比Linux命令行(bash)中指定的文本字母数字大的文件列表? - How to get list of files alphanumerically greater than text specified in Linux command line (bash) via ftp connection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM