LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   For loop question (https://www.linuxquestions.org/questions/linux-newbie-8/for-loop-question-4175508863/)

jonnybinthemix 06-23-2014 04:15 AM

For loop question
 
Good Morning Guys,

I have a quick question regarding a for loop I'm constructing.

I need to get an output filename from an existing file by removing the last part of the file extension and then use that as an output filename in a decryption command.

I've got the following:
Code:

for i in $FILES;
        do basename $i | cut -d'.' --complement -f3
        gpg --batch --passphrase-file /root/.gpgpass --output
done

In the variable $FILES I have a list of files with the extension .csv.pgp

The first command works and returns the same filename with an extension of .csv - however I can't seem to store that filename in a variable... I need to put something at the end of the GPG command --output to give the decrypted file the name .csv

For example, $FILES may contain the following files:

230614093023.csv.pgp
230614101435.csv.pgp

My loop needs to decrypt the above files, and name the output the same as above but without the PGP. I tried decrypting without an output filename thinking it may do it that way as standard, but it seems to require an output filename.

I'd be as always really grateful for any advice :)

Thanks
Jon

chakka.lokesh 06-23-2014 05:34 AM

I not sure if it is homework. but following may work...

Quote:

for i in `ls`
do
value=`basename $i | cut -d'.' --complement -f3 | cut -d '.' --complement -f2`
echo $value
done
check if this serves your need.

pan64 06-23-2014 05:51 AM

instead of
for i in `ls`
use
for i in *
but they are not really safe. Is that what you really want?
output filename can be generated as ${i%.pgp}
basename can be calculated by ${i##*/}

jonnybinthemix 06-23-2014 05:54 AM

Hi,

Thanks for your response.

Not sure what you mean by homework? It's not my homework if that's what you mean! lol - I'm just learning BASH and doing a little project to help me learn.

So I took your idea and adapted it slightly but it still does not do work in the way I hoped, however I think we're closer:

Code:

for i in $FILES; do
        output=`basename $i | cut -d'.' --complement -f3`
        echo $output
done

The above does display the correct filename and lists them in the variable output. But if I put the $output at the end of the GPG command, it fails.

jonnybinthemix 06-23-2014 06:57 AM

Ahh that's done it.

The following appears to work fine:

Code:

for i in $FILES; do
        gpg --batch --passphrase-file /root/.gpgpass --output ${i%.pgp} --decrypt $i
done

I guess i%.pgp tells it to remove the pgp extension? Does this process have a name that I can read about?

pan64 06-23-2014 07:02 AM

see man bash, parameter expansion or here: http://wiki.bash-hackers.org/syntax/pe, substring removal
(and don't forget to press YES if you really want to say thanks)


All times are GMT -5. The time now is 09:12 AM.