0

I made a list of directory names in a file HS_client_list. I need to file the with _zipped_m_ and decrypt and unzip them.

This the script I have written. It works well till decrypt and after that it is not reading the name from HS_client_list. So, the first external argument is the list of directory names, and the second one will read the list of files within the directory.

What I am attempting is, first cd to the directory from HS_client_list file and find the .enc in the directory and pass it to gap.log file. Then, I need to decrypt and unzip the files one by one.

The issue: Until the decrypt loop it works fine.

#!/bin/bash

while IFS= read -r line; do
    echo Executing cd /moveit/$line
    cd /moveit/$line

    #check if encrypted files exist
    if [[ -n "$(ls -A /moveit/$line/*.enc 2>/dev/null)" ]]; then
        #find gaps files
        find $PWD -type f -iname "*_zipped_m_*.enc" -mmin +5 -execdir basename '{}' ';' >/home/infa91punv/gaps.log
 
        #Creating array
        while IFS= read -r line; do
            echo Creating loop for encryped files
            for m in "${line[@]}"
            do
                d=$(echo "$m" | cut -f 1 -d '.')
                echo $d
                d=$d.dat
                echo $d
                /cerner/extproc/hfencrypt_plus $m $d Decrypt /cerner/extproc/hfsymmetrickey.dat log.log infa91punv
                echo /cerner/extproc/hfencrypt_plus $m $d Decrypt /cerner/extproc/hfsymmetrickey.dat log.log infa91punv

                if [[ -f "$d" ]]; then
                    mv $m /cerner/sodaman/enc_archive
                    echo Moving file to encrypted archive
                    echo removing log file : rm log.log
                    rm log.log
                else
                    echo File was not decrypted successfully
                fi
            done
        done < /home/infa91punv/gaps.log
    else
        echo No encrypted files present
    fi

It works fine till this. The below command should put the argument from the HS_client_list file, but it doesn’t. It is blank.

For instance, the output is (ttest.sh,43+ ls -A '/moveit//zipped_m.dat') it is blank after /moveit//... Where it has to be /moveit/foldername/ from the HS_client_list file.

After using the IFS= read the second time, the next command is not reading the first IFS read, it seems.

    #check if zipped files exist
    if [[ -n "$(ls -A /moveit/$line/*_zipped_m_*.dat 2>/dev/null)" ]]; 
    then
        #create array of all zipped files
        dat_zipped_files=($( ls *_zipped_m_*.dat ))
        echo Creating array for gaps zipped files : $dat_zipped_files

        #unzip all zipped files.
        for m in "${dat_zipped_files[@]}"
        do
            echo Unzipping files in array : tar -zxvf $m
            tar -zxvf $m
            echo Moving file to archive : mv $m /cerner/sodaman/enc_archive
            mv $m /cerner/sodaman/enc_archive
            echo Increment move indicator by 1
            move=$((move+1))
            echo Move count is $move
        done
    else
        echo No zipped files present
    fi    
done < HS_client_list
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Prabhu U
  • 81
  • 1
  • 1
  • 4
  • Could be [this](https://stackoverflow.com/questions/16854280/a-variable-modified-inside-a-while-loop-is-not-remembered) – LMC May 16 '22 at 14:11
  • You should start by running your script through https://shellcheck.net – Fravadona May 16 '22 at 15:09
  • [Shellcheck](https://www.shellcheck.net/) identifies a bunch of issues with the code. If it was my code I'd fix them. However, I think the fundamental issue is not something that [Shellcheck](https://www.shellcheck.net/) can find. The code uses the same variable, `line`, for reading from both the `HS_client_list` and `/home/infa91punv/gaps.log` files. The `line` value read from `HS_client_list` is overwritten by the time it is used when checking if zipped files need to be processed. Use a variable with a different name when reading from one of the files. – pjh May 16 '22 at 15:12
  • I have edited your question and indented your code; however, you should really take a look at the code and throw away everything that isn't directly related to your problem. Reduce the code to the bare minimum that still has the problem you encounter ([mcve]). – Benjamin W. May 16 '22 at 15:52

0 Answers0