LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-23-2014, 04:15 AM   #1
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

Rep: Reputation: Disabled
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
 
Old 06-23-2014, 05:34 AM   #2
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
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.
 
Old 06-23-2014, 05:51 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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##*/}
 
1 members found this post helpful.
Old 06-23-2014, 05:54 AM   #4
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-23-2014, 06:57 AM   #5
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

Original Poster
Rep: Reputation: Disabled
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?
 
Old 06-23-2014, 07:02 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
for loop question unclesamcrazy Linux - Newbie 4 05-07-2014 01:21 AM
question in a for loop john83reuben Programming 7 10-08-2013 10:59 PM
for loop question s_linux Programming 4 09-07-2011 06:26 PM
while loop question IceOner Programming 6 10-26-2007 08:23 AM
vbs loop question jonlake Programming 3 10-05-2006 05:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration