awk & basename puzzler - advise sought

Hi

I have been able generate a file ($ELOG) that can have multiple lines within it. The first column represents the full path source file and the other is the full path target ... the file names are the same but the target directory paths are slightly different.

<source_dir1>/file1 <target_dir1>/file1
<source_dir1>/file2 <target_dir1>/file2
<source_dir2>/file3 <target_dir2>/file3

I need to run a 3rd party ENCRYPT (executable file) against <source_dir1>/file1 and then move it to <target_dir1/file1...<target_dir1>/file1 is the encrypted version of the human-readable <source_dir1>/file1

The code I have thusfar is

cat $ELOG | while read line;
       do
         echo $line | awk '{print "ENCRYPT " (basename $1)"  "<encrypt password>}' | sh
         sleep 3
         echo $line | awk '{print "mv "$1" "$2}' | sh

By using basename, I am hoping to seperate the filename so the line should read

ENCRYPT file1 <password> ....pause 3 and then move the file to the correct <target_dir> located in column 2 of the file

Unfortunately the baseline part is not working - what am I not doing correctly ?

Please advise how to resolve

Thanks in advance

David

First of all, you don't need to cat into a while, you can redirect the file into the while:

while read stuff
do
   echo $stuff
done <$ELOG

Further, the use of awk isn't needed either. Something like this should work, provided you actually can supply your encryption password as you indicated on the command line:

while read source dest
do
    ENCRYPT ${source##*/} "password"
    sleep 3
    mv $source $dest
done <$ELOG
1 Like

If your "ENCRYPT" command encrypts the file named by its 1st argument and stores the results in a file in the current directory with the same name, this should do what you want:

#!/bin/ksh
while read src dest
do      ENCRYPT "$src" "<encrypt password>"
        mv "$(basename "$src")" "$dest"
done < "$ELOG"

This is written using the Korn shell, but will work with any shell that accepts basic Bourne shell syntax (i.e., not csh or similar shells).

If you don't happen to be in the directory where the source file is located, passing the basename of the source file to ENCRYPT won't allow it to find your source file.

1 Like

Agma / Don

Many thanks for responding

I've tried both and I think the one that I more readily understand and has worked all OK is your contribution Don

Agma - apologies but didn't understand <source##/>....the ## bit but I did appreciate you taking the time to explain that I didn't need cat / while

Hi davidra,
I'm glad to hear that my contribution worked for you.

This is what ${source##*/} does: it looks at the shell variable named source and if its value matches the pattern */ (which matches any string that ends with a / character), it removes the longest match for that pattern from the start (left) of the string. If $source does not contain any / characters, it is expanded unchanged. Similarly, ${var#pattern} removes the shortest match for pattern from the start of the expansion of $var, ${var%pattern} removes the shortest match for pattern from the end of the expansion of $var, and ${var%%pattern} removes the longest match for pattern from the end of the expansion of $var. For example:

src="/usr/src/cmd.c"
echo X${src##*/}X X${src#*/}X X${src%/*}X X${src%%/*}X X${src%XYZ}X

produces the output:

Xcmd.cX Xusr/src/cmd.cX X/usr/srcX XX X/usr/src/cmd.cX
1 Like