LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-18-2012, 04:30 PM   #1
drandre
LQ Newbie
 
Registered: Apr 2012
Distribution: OpenSUSE 12.1
Posts: 11

Rep: Reputation: 0
Grabbing the last field of a delimited entry without using "awk" or "sed"


Using a do-while loop, I am processing entries contained in a file, each on a separate line. Each entry is a path to a file. Each file will be encrypted using openssl, and the encrypted file will be saved in directory called "/home/backup". Since I am not replicating the path of each file in the "/home/backup" directory, I only need the last field which contains the file name.

I need a command, or series of commands, that can grab the last field of a delimited entry without using the "awk" or "sed" commands.

Below is the file "/root/CONFIGS" containing paths to files (the exception is the "/etc/xinetd.d directory"), as well as the code that I have so far. The openssl code won't work until I give it a valid file output (the last field).

Code:
/boot/grub/menu.lst
/etc/cups/printers.conf
/etc/fstab
/etc/group
/etc/hosts
/etc/inittab
/etc/issue
/etc/issue.net
/etc/modprobe.conf
/etc/named.conf
/etc/ntp.conf
/etc/passwd
/etc/postfix/main.cf
/etc/resolv.conf
/etc/samba/smb.conf
/etc/services
/etc/shells
/etc/vmware/vmnet8/dhcpd/dhcpd.conf
/etc/xinetd.conf
/etc/xinetd.d
/var/cron
Code:
function1()
{
while read configName
do 
	if [ -e "$configName" ]
	then
		openssl enc -aes-256-cbc -salt -in $configName -out /home/backup$configName.enc -pass pass:$secondKey #This does not work
	else
		echo -e "ERROR: File \"$configName\" does not exist."
	fi
done < "/root/CONFIGS"
}

echo -en "\nSpecify encryption key: "
read firstKey
echo -en "\nConfirm encryption key: "
read secondKey
if [ "$firstKey" = "$secondKey" ]
then
	function1
else
	echo -e "\nERROR: Encryption key does not match. \n"
	exit 2
fi
}

Last edited by drandre; 04-18-2012 at 04:31 PM. Reason: Typo error
 
Old 04-18-2012, 04:32 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Had a look at basename?
Or bash string substitution?
Code:
$ tmp=/boot/grub/menu.lst
$ echo ${tmp##*/}
menu.lst

Cheers,
Tink

Last edited by Tinkster; 04-18-2012 at 04:36 PM. Reason: added example
 
1 members found this post helpful.
Old 04-18-2012, 04:34 PM   #3
CTM
Member
 
Registered: Apr 2004
Distribution: Slackware
Posts: 308

Rep: Reputation: 287Reputation: 287Reputation: 287
Any particular reason you can't use sed or awk? Anyway, here's a creative use of cut and rev:

Code:
$ cat CONFIGS | rev | cut -d'/' -f1 | rev
menu.lst
printers.conf
fstab
group
hosts
inittab
issue
issue.net
modprobe.conf
named.conf
ntp.conf
passwd
main.cf
resolv.conf
smb.conf
services
shells
dhcpd.conf
xinetd.conf
xinetd.d
cron
 
1 members found this post helpful.
Old 04-18-2012, 04:37 PM   #4
drandre
LQ Newbie
 
Registered: Apr 2012
Distribution: OpenSUSE 12.1
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Tinkster View Post
Had a look at basename?
Or bash string substitution?
Looked at, yes.

I am working on this as part of a scripting project and I have been told that I "should use the cut command".

Quote:
Originally Posted by CTM View Post
Any particular reason you can't use sed or awk? Anyway, here's a creative use of cut and rev:

Code:
$ cat CONFIGS | rev | cut -d'/' -f1 | rev
menu.lst
printers.conf
fstab
group
hosts
inittab
issue
issue.net
modprobe.conf
named.conf
ntp.conf
passwd
main.cf
resolv.conf
smb.conf
services
shells
dhcpd.conf
xinetd.conf
xinetd.d
cron
I shall try this code...

Last edited by drandre; 04-18-2012 at 04:41 PM. Reason: added reply to another member
 
Old 04-18-2012, 06:06 PM   #5
drandre
LQ Newbie
 
Registered: Apr 2012
Distribution: OpenSUSE 12.1
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by CTM View Post
Code:
cat CONFIGS | rev | cut -d'/' -f1 | rev
This code works perfectly. Thank you for your help!
 
Old 04-18-2012, 06:23 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by drandre View Post
Looked at, yes.

I am working on this as part of a scripting project and I have been told that I "should use the cut command".



I shall try this code...
Fair enough

If you can get away w/ out it - here's a version of your original function which I
believe I modified in a way that would let you use the original file unmodified,
and w/o use of external commands:
Code:
function1()
{
while read configName
configName=${configName##*/}
do 
	if [ -e "$configName" ]
	then
		openssl enc -aes-256-cbc -salt -in $configName -out /home/backup$configName.enc -pass pass:$secondKey #This does not work
	else
		echo -e "ERROR: File \"$configName\" does not exist."
	fi
done < "/root/CONFIGS"
}

echo -en "\nSpecify encryption key: "
read firstKey
echo -en "\nConfirm encryption key: "
read secondKey
if [ "$firstKey" = "$secondKey" ]
then
	function1
else
	echo -e "\nERROR: Encryption key does not match. \n"
	exit 2
fi
}



Cheers,
Tink
 
Old 04-18-2012, 07:29 PM   #7
drandre
LQ Newbie
 
Registered: Apr 2012
Distribution: OpenSUSE 12.1
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Tinkster View Post
Code:
configName=${configName##*/}
I see what you did there. Clever, very clever...

As it so happens, I have a somewhat related question that have run into.

In addition to creating encrypted copies of the files, I also need to checksum each file (I'm using sha256sum), creating a list of checksums in the process, and then creating an encrypted file containing that list (using openssl again). The catch is that the list of checksums cannot be stored in a temporary file.

This presents two issues. First, appending multiple lines of data to something other than a temp file - I'm thinking a variable would be suitable. Based on what I have Googled, it is possible, but I do not understand how. Second, to encrypt a file with openssl you have to specify an input file. I've tried using a variable instead of an input file, but openssl spits errors.

Thoughts?
 
Old 04-18-2012, 07:43 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
You can make openssl read from STDIN, and output to STDOUT.


Cheers,
Tink
 
1 members found this post helpful.
Old 04-18-2012, 07:49 PM   #9
drandre
LQ Newbie
 
Registered: Apr 2012
Distribution: OpenSUSE 12.1
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Tinkster View Post
You can make openssl read from STDIN, and output to STDOUT.


Cheers,
Tink
That was simple enough. The first issue, storing multiple lines of data in something other than a temp file, still stumps me. I'm thinking that appending data to a variable somehow, or concatenating, would be the theoretical answer - although the practical application has me guessing.
 
Old 04-18-2012, 07:58 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
W/o trying to replicate your set-up for testing: you should be able
to do the processing in a loop within a sub-shell, and not have to
store anything, not even in RAM (I would have thought).


Cheers,
Tink
 
Old 04-18-2012, 08:13 PM   #11
drandre
LQ Newbie
 
Registered: Apr 2012
Distribution: OpenSUSE 12.1
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Tinkster View Post
W/o trying to replicate your set-up for testing: you should be able
to do the processing in a loop within a sub-shell, and not have to
store anything, not even in RAM (I would have thought).


Cheers,
Tink
I'm not sure that I understand?

Concatenation, as I have now tried, also works well.

Code:
file="abc.txt"
tempvar=$(echo "$tempvar $file")
echo $tempvar
abc.txt
file="def.txt"
tempvar=$(echo "$tempvar $file")
echo $tempvar
abc.txt def.txt
The only downside is that the data is not separated into unique lines; but as far as I know, variables only contain data as a stream.
 
Old 04-18-2012, 08:29 PM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Maybe I didn't full understand:
Code:
 for i in $(cat files ); do sha256sum $i; done
cb63fb113fecd6ddfdded66056d53ffcd84dcc60373b233aa9d428193fe86454  /etc/fstab
8e7ae18adb8cf63f4a16b972a9ad6c006f26e030ac96261a1bcf103bfdac9ed8  /etc/group
7dd02593eb46aa52b234d3a99b20cfd4730e05e21421f66a42db12fc6727b109  /etc/hosts
a4544d6f0d115de70d5f28bfaa7fa087c1bf1762426958f799e3b0b7b4086062  /etc/inittab
dcb805cb05d78dea3198c43628f7feb80a53b06117450c483d0c01809bee4158  /etc/issue
a7932e9fb9a0c51d71522cc6ba37034f2f2f97e68c5c4fcfed77b82590bd402c  /etc/issue.net
03f7f652376a6c037d6052690a59c741461065a96fee6a6f275ee81e9cba8277  /etc/ntp.conf
220e7d11f4959bce181a45fd0a8519ec0af9db276eaf0f2b98e33586dcaad660  /etc/resolv.conf
41ecfecc953fe2c2d9d5a31d509a318655c120214814f8478465d3522cae9882  /etc/samba/smb.conf
95f8369b7708713f7265cf80e8a2ef312dff455d4dd2d04cd1f81b9c85d90881  /etc/services
d94d37a8f7ae7bf3faffb74bd2a4ecb7e625234b30d6b7caa8edda399fc400f5  /etc/shells

If you slapped that in front of a pipe to openssl for encryption you shouldn't
need to store anything? Obviously the loop would happen over the lines
you read from your files ....



Cheers,
Tink
 
Old 04-18-2012, 08:53 PM   #13
drandre
LQ Newbie
 
Registered: Apr 2012
Distribution: OpenSUSE 12.1
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Tinkster View Post
Maybe I didn't full understand:
Code:
 for i in $(cat files ); do sha256sum $i; done
cb63fb113fecd6ddfdded66056d53ffcd84dcc60373b233aa9d428193fe86454  /etc/fstab
8e7ae18adb8cf63f4a16b972a9ad6c006f26e030ac96261a1bcf103bfdac9ed8  /etc/group
7dd02593eb46aa52b234d3a99b20cfd4730e05e21421f66a42db12fc6727b109  /etc/hosts
a4544d6f0d115de70d5f28bfaa7fa087c1bf1762426958f799e3b0b7b4086062  /etc/inittab
dcb805cb05d78dea3198c43628f7feb80a53b06117450c483d0c01809bee4158  /etc/issue
a7932e9fb9a0c51d71522cc6ba37034f2f2f97e68c5c4fcfed77b82590bd402c  /etc/issue.net
03f7f652376a6c037d6052690a59c741461065a96fee6a6f275ee81e9cba8277  /etc/ntp.conf
220e7d11f4959bce181a45fd0a8519ec0af9db276eaf0f2b98e33586dcaad660  /etc/resolv.conf
41ecfecc953fe2c2d9d5a31d509a318655c120214814f8478465d3522cae9882  /etc/samba/smb.conf
95f8369b7708713f7265cf80e8a2ef312dff455d4dd2d04cd1f81b9c85d90881  /etc/services
d94d37a8f7ae7bf3faffb74bd2a4ecb7e625234b30d6b7caa8edda399fc400f5  /etc/shells

If you slapped that in front of a pipe to openssl for encryption you shouldn't
need to store anything? Obviously the loop would happen over the lines
you read from your files ....



Cheers,
Tink
I think that is on the right track - but for each loop, the checksum data has to be stored incrementally in something other than a temp file, without echoing the checksums back to the screen. Then, once all of the checksums are collected, it is put into openssl and saved as an encrypted file.

Does that make sense? I might be confusing more than I am helping!

Last edited by drandre; 04-18-2012 at 09:22 PM.
 
Old 04-18-2012, 09:05 PM   #14
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by drandre View Post
I think that is on the right track - but for each loop, the checksum data has to be stored incrementally in something other than a variable, without echoing the checksums back to the screen. Then, once all of the checksums are collected, it is put into openssl and saved as an encrypted file.

Does that make sense? I might be confusing more than I am helping!
Sorry, doesn't make sense to me :}

You already have that loop working, right? I don't understand why you think
you need to store the lines w/ the sha's ... can you give us a larger snippet
of your code?

If the desired result is to have ALL checksummed file names into an encrypted
file I can't see any need for storing any output in a variable (or any other
way, for that matter) - just pipe it to openssl, encrypt it from stdin, and
write the results to your (then encrypted) file - voila.



Cheers,
Tink
 
1 members found this post helpful.
Old 04-18-2012, 09:19 PM   #15
drandre
LQ Newbie
 
Registered: Apr 2012
Distribution: OpenSUSE 12.1
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Tinkster View Post
If the desired result is to have ALL checksummed file names into an encrypted
file I can't see any need for storing any output in a variable (or any other
way, for that matter) - just pipe it to openssl, encrypt it from stdin, and
write the results to your (then encrypted) file - voila
That is exactly what I was looking to do. Can you show me how?

My code is included below. Bear with me as I have changed some variable names. The variable containing checksums of files is shown in red, but based on what you have told me, this variable is unnecessary. The code that encrypts the checksums in the variable is shown in blue, but it produces error messages and does not work. The contents of the CONFIGS file is posted earlier in this thread.

Code:
while read filePath #Read file paths from file
do 
	if [ -e "$filePath" ] #If file path exists
	then
		fileName=${filePath##*/} #Variable contains file name from last field
		openssl enc -aes-256-cbc -salt -in $filePath -out /home/backup/$fileName.enc -pass pass:Anonymous 2>/dev/null #Encrypt file, save output to backup directory, and suppress errors
		checksumList=$(echo "$checksumList " $(sha256sum $filePath 2>/dev/null)) #Variable contains checksums of files
	else
		echo -e "ERROR: File \"$filePath\" does not exist." #Error
	fi
done < "/root/CONFIGS" #Read input from file

openssl enc -aes-256-cbc -salt -in $checksumList -out EncryptedChecksums.enc -pass pass:Anonymous ;; #Encrypt checksums

Last edited by drandre; 04-18-2012 at 09:20 PM. Reason: Typo error
 
  


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
sed - use sed to replace multiple instances from "x" to "y" in a line mrodmac Linux - General 4 02-02-2010 11:37 AM
printing hh in hh:mm using "awk '{FS=":";print $1}'" misses first line of output!! mayankmehta83 Linux - Newbie 2 12-03-2009 02:55 AM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM
No UTMPX entry, You must EXEC "login" for the lowest "shell" ooihc Solaris / OpenSolaris 7 03-12-2007 02:09 PM
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 10:47 AM

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

All times are GMT -5. The time now is 11:16 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