目录
- 一. 前提条件
- 二. 通过sh脚本进行批量修改
- 三. 通过Excel和文本编辑器进行批量转换
- 四. 实际执行效果
一. 前提条件
⏹项目中有批量检索文件的需求,如下所示需要同时执行500多个find命令
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
命令执行
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
文件中 -
由于是按行读取文件,因此最后一行命令需要有换行行为,否则最后一行会丢失
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
。
⏹命令如下
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
⏹效果如下所示
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的单元格中预埋以下公式,批量生成命令
="echo ""「" & A1 & "」コマンドの実行開始..."""
=A1
="echo -e ""「" & A1 & "」コマンドの実行終了...\n"""
⏹复制B,C,D列内容,粘贴为纯文本后,在D列的文本的最后添加一个特殊符号#
- 特殊符号不一定非得是
#
,此处只是为了举例 - 添加特殊符号是为了之后替换文本的时候用
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
选项。
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:~$