Iva*_*kov 16 linux bash shell command-line bash-scripting
我正在尝试制作一个模块化脚本。我有几个从单个脚本调用的脚本/命令。
我想为每个单独命令的输出添加前缀。
例子:
我的文件都是commands.sh/command1.sh/command2.sh
command1.sh输出
file exists
file moved
command2.sh输出
file copied
file emptied
allcommands.sh运行脚本command1.sh和command2.sh
我想为这两个脚本的每个输出添加前缀,如下所示:
[command1] file exists
[command1] file moved
[command2] file copied
[command2] file emptied
小智 23
我假设您在 allcommands.sh 中所做的是:
command1.sh
command2.sh
Run Code Online (Sandbox Code Playgroud)
只需将它与
command1.sh | sed "s/^/[command1] /"
command2.sh | sed "s/^/[command2] /"
Run Code Online (Sandbox Code Playgroud)
Dan*_*son 10
一个最小的例子allcommands.sh:
#!/bin/bash
for i in command{1,2}.sh; do
./"$i" | sed 's/^/['"${i%.sh}"'] /'
done
Run Code Online (Sandbox Code Playgroud)
使用command1.sh和command2.sh可执行文件并且在同一个目录中只echo输入所需的字符串,这给出了 shell 输出:
$ ./command1.sh
file exists
file moved
$ ./command2.sh
file copied
file emptied
$ ./allcommands.sh
[command1] file exists
[command1] file moved
[command2] file copied
[command2] file emptied
Run Code Online (Sandbox Code Playgroud)
sed分解sed 's/^/['"${i%.sh}"'] /'
Run Code Online (Sandbox Code Playgroud)
s/ 进入“正则表达式模式匹配和替换”模式^/ 意思是“匹配每一行的开头”${i%.sh}发生在 shell 上下文中,意思是“ $i,但去掉后缀.sh”['"${i%.sh}"'] /首先打印 a [,然后退出引用的上下文以$i从 shell获取变量,然后重新输入以完成]和 空格。| 归档时间: |
|
| 查看次数: |
10569 次 |
| 最近记录: |