Linux 指定命令行前后添加echo打印内容

目录

  • [一. 前提条件](#一. 前提条件)
  • [二. 通过sh脚本进行批量修改](#二. 通过sh脚本进行批量修改)
  • [三. 通过Excel和文本编辑器进行批量转换](#三. 通过Excel和文本编辑器进行批量转换)
  • [四. 实际执行效果](#四. 实际执行效果)

一. 前提条件

⏹项目中有批量检索文件的需求,如下所示需要同时执行500多个find命令

bash 复制代码
find ./work -type f -name *.java
find ./work -type f -name *.html
find ./work -type f -name *.css
find ./work -type f -name *.js
... 省略若干 ...

⏹手动执行每条命令,既容易出错,也很耗时间,因此需要做一个sh脚本文件批量执行

但是通过脚本文件执行的话,执行结果会紧密靠在一起,不好区分,因此最好在每个命令执行结束后,添加一个空行之类的,便于区分。

例如将命令批量处理为下面这样,然后创建一个sh脚本文件,通过bash命令执行

bash 复制代码
echo "「find ./work -type f -name *.java」コマンドの実行開始..."
find ./work -type f -name *.java
echo -e "「find ./work -type f -name *.java」コマンドの実行終了...\n"

echo "「find ./work -type f -name *.html」コマンドの実行開始..."
find ./work -type f -name *.html
echo -e "「find ./work -type f -name *.html」コマンドの実行終了...\n"

echo "「find ./work -type f -name *.css」コマンドの実行開始..."
find ./work -type f -name *.css
echo -e "「find ./work -type f -name *.css」コマンドの実行終了...\n"

echo "「find ./work -type f -name *.js」コマンドの実行開始..."
find ./work -type f -name *.js
echo -e "「find ./work -type f -name *.js」コマンドの実行終了...\n"

⏹500多条命令,显然不能一条条的手动修改,因此需要有批量进行修改的方法

下文进行介绍


二. 通过sh脚本进行批量修改

  • 将需要进行批量转换的命令放到file_search.txt文件中

  • 由于是按行读取文件,因此最后一行命令需要有换行行为,否则最后一行会丢失

    bash 复制代码
    find ./work -type f -name *.java
    find ./work -type f -name *.html
    find ./work -type f -name *.css
    find ./work -type f -name *.js
    # 需要有该空行保证最后一行换行了
  • 部分windows上的文本编辑器默认的换行符是\r\n,而Linux默认的换行符是\n,为避免换行符不统一造成的问题

    使用tr -d '\r'命令,去掉每行中的\r,将换行符转换为Linux的换行符\n

⏹命令如下

bash 复制代码
while IFS= read -r line; do

	# 去掉\r 
	line=$(echo "$line" | tr -d '\r')
	
	# 跳过空行
	if [[ -z "$line" ]]; then
		continue
	fi
	
	echo "echo \"[$line]コマンドの実行開始...\""
	echo "$line"
	echo "echo -e \"[$line]コマンドの実行終了...\n\""
	echo
	
done < file_search.txt > output.sh

⏹效果如下所示

bash 复制代码
Admin@FengYeHong-HP MINGW64 ~/Desktop/tst
$ cat file_search.txt
find ./work -type f -name *.java
find ./work -type f -name *.html
find ./work -type f -name *.css
find ./work -type f -name *.js

Admin@FengYeHong-HP MINGW64 ~/Desktop/tst
$ sh cmd_convert.sh

Admin@FengYeHong-HP MINGW64 ~/Desktop/tst
$ cat output.sh
echo "[find ./work -type f -name *.java]コマンドの実行開始..."
find ./work -type f -name *.java
echo -e "[find ./work -type f -name *.java]コマンドの実行終了...\n"

echo "[find ./work -type f -name *.html]コマンドの実行開始..."
find ./work -type f -name *.html
echo -e "[find ./work -type f -name *.html]コマンドの実行終了...\n"

echo "[find ./work -type f -name *.css]コマンドの実行開始..."
find ./work -type f -name *.css
echo -e "[find ./work -type f -name *.css]コマンドの実行終了...\n"

echo "[find ./work -type f -name *.js]コマンドの実行開始..."
find ./work -type f -name *.js
echo -e "[find ./work -type f -name *.js]コマンドの実行終了...\n"


Admin@FengYeHong-HP MINGW64 ~/Desktop/tst

三. 通过Excel和文本编辑器进行批量转换

⏹在Excel的单元格中预埋以下公式,批量生成命令

vba 复制代码
="echo ""「" & A1 & "」コマンドの実行開始..."""
=A1
="echo -e ""「" & A1 & "」コマンドの実行終了...\n"""

⏹复制B,C,D列内容,粘贴为纯文本后,在D列的文本的最后添加一个特殊符号#

  • 特殊符号不一定非得是#,此处只是为了举例
  • 添加特殊符号是为了之后替换文本的时候用
vba 复制代码
echo -e "「find ./work -type f -name *.java」コマンドの実行終了...\n"#

⏹Excel的内容复制到文本编辑器中(此处用NotePad++举例),准备替换

  • Excel的内容复制到文本编辑器之后,列与列之间默认以Tab进行分隔
  • NotePad++新建的文件默认以\r\n作为换行符,我们将Tab空格替换为\r\n从而实现了行转列

⏹替换之后的效果如下

⏹可以看到,命令与命令之间很紧凑,不便于肉眼查看

这个时候,可以将之前特意添加的#给替换为\r\n,用来给每一组命令添加分隔行


四. 实际执行效果

💥注意事项

  • 脚本需要通过bash命令来执行,如果通过sh命令来执行的话,echo -e无法被识别
  • 因为 sh 可能是 较早版本的 Shell(如 /bin/sh 可能是 dash),而 dash 的 echo 不支持 -e 选项。
bash 复制代码
apluser@ubuntu24-01:~$ cat search_file.sh
echo "「find ./work -type f -name *.java」コマンドの実行開始..."
find ./work -type f -name *.java
echo -e "「find ./work -type f -name *.java」コマンドの実行終了...\n"

echo "「find ./work -type f -name *.html」コマンドの実行開始..."
find ./work -type f -name *.html
echo -e "「find ./work -type f -name *.html」コマンドの実行終了...\n"

echo "「find ./work -type f -name *.css」コマンドの実行開始..."
find ./work -type f -name *.css
echo -e "「find ./work -type f -name *.css」コマンドの実行終了...\n"

echo "「find ./work -type f -name *.js」コマンドの実行開始..."
find ./work -type f -name *.js
echo -e "「find ./work -type f -name *.js」コマンドの実行終了...\n"

apluser@ubuntu24-01:~$
apluser@ubuntu24-01:~$ bash search_file.sh
「find ./work -type f -name *.java」コマンドの実行開始...
./work/cbc/src/test/java/com/example/jmw/JmwApplicationTests.java
./work/cbc/src/main/java/com/example/jmw/service/MailSendFactory.java
...省略...
./work/cbc/src/main/java/com/example/jmw/entity/Product.java
./work/cbc/src/main/java/com/example/jmw/entity/Department.java
./work/cbc/src/main/java/com/example/jmw/entity/OrderInfoEntity.java
./work/cbc/src/main/java/com/example/jmw/entity/Menu.java
「find ./work -type f -name *.java」コマンドの実行終了...

「find ./work -type f -name *.html」コマンドの実行開始...
./work/cbc/src/main/resources/templates/test5.html
./work/cbc/src/main/resources/templates/test31.html
...省略...
./work/cbc/src/main/resources/templates/test33.html
./work/cbc/src/main/resources/static/error/404.html
./work/cbc/src/main/resources/static/error/500.html
「find ./work -type f -name *.html」コマンドの実行終了...

「find ./work -type f -name *.css」コマンドの実行開始...
./work/cbc/src/main/resources/static/css/public/jquery-ui.min.css
./work/cbc/src/main/resources/static/css/common/common.css
./work/cbc/src/main/resources/static/css/business/test2.css
./work/cbc/src/main/resources/static/css/business/test1.css
「find ./work -type f -name *.css」コマンドの実行終了...

「find ./work -type f -name *.js」コマンドの実行開始...
./work/cbc/src/main/resources/static/js/public/jquery-3.6.0.min.js
./work/cbc/src/main/resources/static/js/common/commonModule.js
./work/cbc/src/main/resources/static/js/common/common.js
./work/cbc/src/main/resources/static/js/business/test2.js
./work/cbc/src/main/resources/static/js/business/test1.js
「find ./work -type f -name *.js」コマンドの実行終了...

apluser@ubuntu24-01:~$
相关推荐
cui_win7 分钟前
【磁盘】每天掌握一个Linux命令 - iostat
linux·运维·服务器·iostat·磁盘io
急速前行Klein33 分钟前
Ubuntu中安装git
linux·git·ubuntu
拂去尘世尘1 小时前
一文掌握软件分支管理
linux
W说编程1 小时前
Linux与量子计算:面向未来的架构演进
linux·服务器·性能优化·架构·系统架构·量子计算
czhc11400756632 小时前
LINUX 69 FTP 客服管理系统 man 5 /etc/vsftpd/vsftpd.conf
linux·运维·chrome
好想打kuo碎2 小时前
轻量安全的密码管理工具Vaultwarden
linux·安全·ubuntu
Mayer_WOT2 小时前
Jetson Orin AGX Getting Start with Pytorch
linux
什么半岛铁盒3 小时前
Linux进程异常退出排查指南
linux·运维·服务器
Mylvzi3 小时前
Linux 性能利器:详解 `top` 命令的使用与输出信息解析
linux·服务器·网络
斗转星移33 小时前
解决ubuntu20.04无法唤醒的问题的一种方法
linux·ubuntu·电脑