简体   繁体   中英

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 .

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. You can check that out by adding set -x and executing your script. This is because sed expectes an input file/stream, and there is none given to it.

You could rather use bash substring replacement "${file//.$1}"

${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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM