[SOLVED] I am Attempting to use the output of ls command as an array. And I want it to be line by line


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[@]}"
  d=$(echo "$m" | cut -f 1 -d '.')
  echo $d
  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
      echo File was not decrypted successfully


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

    if /empty/extproc/hfencrypt_plus "$file" "$d" Decrypt /empty/extproc/hfsymmetrickey.dat log.log infa91punv
        # assume hfencrypt sets exit code
        mv "$file" /empty/sodaman/tempPrabhu/blr_temp
        # 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
    rm -f log.log

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?)

Answered By – tripleee

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.