LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 05-05-2012, 03:27 PM   #1
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
an other BASH function question


This function is supposed to check the contents of a tarball against the directory it was created from, but im not seeing how it is checking the data in the tarball with the data in the, lets call it /home/user, directory.

What I see is the tar command to view the contents and pushes that information into a file called /tmp/bz-check.list, but i dont see it comparing that with /home/user.

So just what is this function using to match the files in the tarball with the /home/user directory? I am missing something here, or is this function incomplete?

Thanks in advance for the help.

Code:
### Check and verify tarball
#####################################

checkTar ()

{

tar tfj /tmp/${tarfilename} &> /tmp/tar2; bzerr="$?"; echo ${bzerr} > /tmp/bz-check.list

echo "Verifying compressed data." | tee -a ${LOG}
VERSTAT=`cat /tmp/bz-check.list`

if [ ${VERSTAT} -eq 0 ]
then
	echo "Verification of compressed data was successfull." | tee -a ${LOG}
else
	echo "Compressed data appears to be corrupt.  Error ${VERSTAT}." | tee -a ${LOG}
	exit 1
fi

    echo "Encrypting ${tarfilename}" | tee -a ${LOG}
    openssl des3 -a -salt -in /tmp/${tarfilename} -out /tmp/${tarfilename}.enc -pass pass:xxxxxxxxxx
    echo "${tarfilename}.enc Encrypted" | tee -a ${LOG}
	NEWTAR=/tmp/${tarfilename}.enc

# to decrypt: openssl des3 -a -d -salt -in 2011-12-20.tar.bz2.enc -out 2011-12-20.tar.bz2 -pass pass:xxxxxxxxxx

}
 
Old 05-05-2012, 03:54 PM   #2
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
because it does not verify the data, it just verifies if the tar command was successfully executed.
 
Old 05-05-2012, 04:00 PM   #3
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
thanks, didnt think it did. is there a way to verify that the data in the tarball is the (at least same number of files and same name of files) as the original location?
 
Old 05-05-2012, 04:05 PM   #4
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
yes, there is a way, just read the man page of tar (there is a t - test flag)
 
Old 05-05-2012, 04:07 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Not only that, it does it in about the most complex and inefficient way possible.

It first saves the "$?" exit code to a second variable, then echo's that variable into a text file. Then it cat's the file contents back into another variable, and finally checks that for success.



I won't even mention the confusing formatting or the unquoted variables.
Oh wait, I guess I just did.
 
1 members found this post helpful.
Old 05-06-2012, 11:35 AM   #6
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
this is not my code, at least that function is not. pan64, thanks ill read up on the -t for tar and see what i can glean from there to improve that section of the overall script.
 
Old 05-06-2012, 11:54 AM   #7
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by David the H. View Post
Not only that, it does it in about the most complex and inefficient way possible.

It first saves the "$?" exit code to a second variable, then echo's that variable into a text file. Then it cat's the file contents back into another variable, and finally checks that for success.



I won't even mention the confusing formatting or the unquoted variables.
Oh wait, I guess I just did.
David, the quoted variables, should they be like the following, (im still a mega newbie at code)

Code:
i [ "${VERSTAT}" -eq 0 ]
I that what you are referring to? or did I do that wrong. I have only seen variables either in or out of {} with a leading $

ex:
Code:
JUNK=junk

$junk
${junk}
the only time i have seen a variable "" is if it was a command or a part of a test...

Code:
if [ "${junk}" != "stuff" ]
should all of the variables in the #!/bin/bash script be ""?
 
Old 05-06-2012, 12:44 PM   #8
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
ran into a bit of an issue, when i use tar cvjf it creates a tar that when i untar with -xvf it keeps the directory structor of the original location, ex:

/home/user/stuff/more/stuff/foo.sh

if i then take my tar and open it, it will create that directory tree under where i am:

/home/user/stuff/more/stuff/home/user/stuff/more/stuff/foo.sh

That makes it an issue for using tar --diff -vf foo.tar.bz2

now what?
 
Old 05-06-2012, 12:46 PM   #9
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
actually for $? you do not need ", so "$?" and $? mean exactly the same. { and } needed only if otherwise the variable name cannot be identified, so $JUNK and ${JUNK} are identical (and both will mean junk, and do not forget, $junk is empty), but: $JUNK_EMTPY is empty, ${JUNK)_EMPTY will be junk_EMPTY.
if you are really interested read the man page and practice, practice and practice
 
1 members found this post helpful.
Old 05-06-2012, 12:54 PM   #10
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
I am using the following to create a tarball:

tar cvjf foo.tar.bz2 /path/to/files/

What I would like to be able to do is before i copy this tarball to some device, flash drive, external HDD, NAS device, etc... i want to run a --diff -vf on it to try to verify that the files in the original folder and that of the tarball are the same.

Yes this goes along with the bash function question i had earlier.

what I have discovered is the cvjf is not only creating the tarball, compressing in bz2 format, but it is preserving the directory structure of the folder. this is an issue when i run --diff -vf as i get the following results:

[server@user rx30]$ tar --diff -vf test.tar.bz2 /www/http/docs/rx30/*.sh
tar: /www/http/docs/rx30/autoftp.sh: Not found in archive
tar: /www/http/docs/rx30/autoftpd.sh: Not found in archive
tar: /www/http/docs/rx30/back.sh: Not found in archive
tar: /www/http/docs/rx30/connect.sh: Not found in archive
tar: /www/http/docs/rx30/freespc.sh: Not found in archive
tar: /www/http/docs/rx30/modemflash.sh: Not found in archive
tar: /www/http/docs/rx30/nasftp.sh: Not found in archive
tar: /www/http/docs/rx30/nasftpd.sh: Not found in archive
tar: /www/http/docs/rx30/nastftp.sh: Not found in archive
tar: /www/http/docs/rx30/nastftpd.sh: Not found in archive
tar: /www/http/docs/rx30/rsync.sh: Not found in archive
tar: /www/http/docs/rx30/rsyncd.sh: Not found in archive
tar: /www/http/docs/rx30/vgback.sh: Not found in archive
tar: Error exit delayed from previous errors

Those files are there when i navigate to them, and they are in the original folder were i created the tarball. this is an issue.

What can i do to prevent this, or what needs to be done to get around this issue?

i hope that is a bit more clear.

Thanks in advance.
 
Old 05-06-2012, 12:55 PM   #11
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by pan64 View Post
actually for $? you do not need ", so "$?" and $? mean exactly the same. { and } needed only if otherwise the variable name cannot be identified, so $JUNK and ${JUNK} are identical (and both will mean junk, and do not forget, $junk is empty), but: $JUNK_EMTPY is empty, ${JUNK)_EMPTY will be junk_EMPTY.
if you are really interested read the man page and practice, practice and practice
thanks pan, what was david getting at then? you explained it exactly the way i thought i understood it.
 
Old 05-06-2012, 01:15 PM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I'd gathered that it wasn't your code from the way you talked about it. You wouldn't be asking us what it does if you'd written it yourself.


As for the variables, you're exactly correct. According to the shell parsing order, after a parameter is expanded (or a command substitution made), the results are word-split into separate elements according to the characters set in the IFS variable (space,tab,newline by default). Then after that, pathname expansion (globbing) is performed, which could further alter the final results.

Placing double quotes around the parameter protects the expanded contents from shell interpretation, preventing the last two steps from happening.

Of course, if you are certain that the value contains nothing that will split or expand, you can leave them off, and sometimes you may actually want splitting to occur, but otherwise it never hurts to be safe.

There are also a few places where the shell doesn't perform the following expansions, such as inside the "[[..]]" double bracket test or when doing "var1=$var2". But it still doesn't hurt to use quotes even then (the right hand side of the double-bracket test is a partial exception, as any globbing or regex tests need to remain unquoted in order to match properly).


So in short, unless you have a good reason to do otherwise, you should always quote.


Argument parsing is a vitally important concept in scripting, so train yourself to do it correctly now.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


The use of the full "${var}" form of the variable in the code above was one of the things I rolled my eyes at. While not wrong in any way, it simply serves no good purpose, and just clutters up the code. The only times you need the brackets are when doing parameter substitutions, expanding positional parameters higher than nine, or when you need to clearly separate the variable name from the following text, like this:

Code:
echo "${foo}bar"
Without the brackets, the shell would attempt to expand a variable called "foobar" instead.
 
Old 05-06-2012, 01:32 PM   #13
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
thanks david, and thanks for the links. i just got in the habit of ${var} and try to do that when i can. i know it is not required, but i feel better safe then sorry. as for the "${var}" the only time ive seen that is in the example i provided in the test. not sure it is needed there either except for the "stuff" portion in the test.
 
Old 05-06-2012, 01:34 PM   #14
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
Quote:
Originally Posted by David the H. View Post

So in short, unless you have a good reason to do otherwise, you should always quote.
Superfluous quotes make the code unnecessarily noisy. I do not like them, so I use them only when they really have meaning.
 
Old 05-06-2012, 02:49 PM   #15
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
good stuff on the code, what about the tar --diff -vf issues?
 
  


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
BASH function using if,then,else mrmnemo Programming 7 05-02-2011 03:13 AM
[SOLVED] Setting function default in a bash script - Bash sixtensixone Linux - General 13 01-08-2011 01:44 PM
Bash array Add function example using indirect array reference as function argument bobywelsh Programming 10 07-05-2010 04:44 AM
Question about Bash and the "until" function daYz Programming 11 08-14-2006 11:34 AM
Bash looping or function within a function FirmbIT Programming 2 04-24-2006 01:06 PM

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

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