-2

I need to find the encrypted(.enc) files from a folder and decrypt them. I will find the .enc files if [[ -n "$(ls -A /sodaman/tempPrabhu/temp/*.enc 2>/dev/null)" ]]; then And I used enc_files=($( ls *.enc )) but it takes all the files as one and fails. It considers all the files for the output as one line. So I replaced it with mapfile to decrypt the files one by one but it throws an error test.sh: line 31: syntax error near unexpected token `<' test.sh: line 31: ` mapfile -t enc_files < <(ls *.enc)'

Below is the script:

if [[ -n "$(ls -A /sodaman/tempPrabhu/temp/*.enc 2>/dev/null)" ]]; then
  #create array of encrypted files
  mapfile -t enc_files < <(ls *.enc)
  enc_files=($( ls *.enc ))
  #echo Creating array $enc_files
  
  #decrypt all encrypted files.
  echo Creating loop for encryped files
  for m in "${enc_files[@]}"
  do
  d=$(echo "$m" | cut -f 1 -d '.')
  echo $d
  d=$d.dat
  echo $d
  /empty/extproc/hfencrypt_plus $m $d Decrypt /empty/extproc/hfsymmetrickey.dat log.log infa91punv
  echo /empty/extproc/hfencrypt_plus $m $d Decrypt /empty/extproc/hfsymmetrickey.dat log.log infa91punv
  if [[ -f "$d" ]]; then
      mv $m /empty/sodaman/tempPrabhu/blr_temp
      #echo Moving file to encrypted archive : mv "$m" /empty/sodaman/enc_archive
      echo removing log file : rm log.log
      rm log.log
  else
      echo File was not decrypted successfully
  fi
  done
fi
Aserre
  • 4,916
  • 5
  • 33
  • 56
Prabhu U
  • 81
  • 1
  • 1
  • 4
  • 4
    For starters, [you should not parse ls](https://mywiki.wooledge.org/ParsingLs) in a script – Aserre Jun 22 '22 at 08:45
  • 2
    Please add a suitable shebang (`#!/bin/bash`) and then paste your script at http://www.shellcheck.net/ and try to implement the recommendations made there. – Cyrus Jun 22 '22 at 08:48
  • 1
    `files=(*.enc)` – dan Jun 22 '22 at 08:49
  • Also : you define `enc_files` twice – Aserre Jun 22 '22 at 08:51
  • If you are interested in the file names only, why are you invoking `ls`? What is wrong when simply globbing for the files? – user1934428 Jun 22 '22 at 09:07
  • 1
    @Cyrus : I'm pretty sure that we see only a small part of the script. After all, the syntax error was reported for line 31, which is the 3rd line from top in the part of the script being posted here. IMO, the main problem with this question is the lack of a small, reproducible example. – user1934428 Jun 22 '22 at 09:10

2 Answers2

0

Don't parse ls results, but use this:

find /sodaman/tempPrabhu/temp/ -maxdepth 1 -name "*.enc"
Dominique
  • 16,450
  • 15
  • 56
  • 112
  • You rarely need the big hammer of `find` for this; often it's just `printf '%s\n' *.enc` or `for file in *.enc; do`... – tripleee Jun 22 '22 at 10:50
  • 2
    ...and if one _is_ going to parse `find`, better to use `-print0` to make it 100% unambiguous; otherwise filenames containing literal newlines can evade your logic. – Charles Duffy Jun 22 '22 at 11:07
0

Here's a refactoring which avoids several of the http://shellcheck.net/ violations in your attempt.

for file in /sodaman/tempPrabhu/temp/*.enc; do
    # avoid nullglob
    test -e "$file" || continue

    # prefer parameter expansion
    d=${file%%.*}.dat

    if /empty/extproc/hfencrypt_plus "$file" "$d" Decrypt /empty/extproc/hfsymmetrickey.dat log.log infa91punv
    then
        # assume hfencrypt sets exit code
        mv "$file" /empty/sodaman/tempPrabhu/blr_temp
    else
        # print diagnostics to stderr
        # mention which file failed
        # mention which script emitted the warning
        echo "$0: $file was not decrypted successfully" >&2
        sed "s%^%log.log: $file: %" log.log >&2
    fi
    rm -f log.log
done

This assumes that you wanted to loop over the files in the directory you examine at the beginning of your script, not in the current directory (perhaps see also What exactly is current working directory?) and that the encryption utility sets its exit code to nonzero if encryption failed (perhaps see also Why is testing “$?” to see if a command succeeded or not, an anti-pattern? which discusses idioms around conditions involving errors). I added a sed command to include the output from log.log in the diagnostics after a failure, though perhaps you would like a different error-handling strategy (exit immediately and let the user troubleshoot? Or rename the log file to a unique name and keep it around for later?)

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thank you all for the help... And another doubt, is there any alternative for `if [[ -n "$(ls -A /sodaman/tempPrabhu/temp/*.enc 2>/dev/null)" ]]; then` ... As this is excluding the late file. Say I have a.enc, b.enc and c.enc... It is only taking a.enc and b.enc... – Prabhu U Jun 22 '22 at 11:49
  • I don't understand the question. Your command checks if the output from `ls -A` is non-empty, which it will be if the wildcard matches at all. The condition with the `nullglob` comment does the same thing more parsimoniously and idiomatically in this refactoring. Your code was highly redundant, and basically examined the same thing three times (though twice in the current directory, which would only do the same thing if you `cd`:ed into the target directory before running the script; it's still unclear to me whether this was a bug, or something you really wanted to work this way). – tripleee Jun 22 '22 at 13:20
  • Tangentially, ["doubt" does not mean "question" in standard English.](https://english.stackexchange.com/questions/2429/can-doubt-sometimes-mean-question) – tripleee Jun 22 '22 at 13:23