[英]Find all files by name recursively, decrypt and rename them using shell script
I'm trying to write a script to find all of the files with .production
in names, decrypt those files and save copies of them without .production
.我正在尝试编写一个脚本来查找名称中带有
.production
所有文件,解密这些文件并保存它们的副本,而无需.production
。
Example files:示例文件:
./functions/key.production.json
./src/config.production.js
Here is my code:这是我的代码:
decrypt() {
echo $1
for file in $(find . -name "*.$1.*")
do
echo "some $file"
openssl enc -aes-128-cbc -a -d -salt -pass pass:asdffdsa -in $file -out $(sed -e "p;s/.$1//")
done
}
$(sed -e "p;s/.$1//")
is the part that hangs. $(sed -e "p;s/.$1//")
是挂起的部分。 You can check that out by adding set -x
and executing your script.您可以通过添加
set -x
并执行脚本来检查它。 This is because sed
expectes an input file/stream, and there is none given to it.这是因为
sed
需要输入文件/流,而没有给它。
You could rather use bash substring replacement "${file//.$1}"
您宁愿使用 bash 子字符串替换
"${file//.$1}"
${string//$substring_to_remove/}
${string//$substring_to_remove/}
All occurrences of the content after //
is replaced in the main string, with the content after the last /
//
之后所有出现的内容在主字符串中被替换,最后一个/
之后的内容
So, the working function would be所以,工作函数将是
decrypt() {
echo $1
for file in $(find . -name "*.$1.*")
do
echo "some $file"
openssl enc -aes-128-cbc -a -d -salt -pass pass:asdffdsa -in $file -out "${file//.$1}"
done
}
You can avoid the subshell $(find . -name " .$1. ") by using a while loop.您可以通过使用 while 循环来避免子 shell $(find . -name " .$1. ") 。
decrypt() {
echo "$1"
local file
while read -r file; do
echo "some $file"
PROCESS-YOUR-FILE-AND-DO-YOUR-STUFF_HERE
done < <(find . -name "*.$1.*")
}
see看
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.