Hey all! I'm in an intro to UNIX class at university, and we've just began writing scripts. Naturally I can't get it to do what I want.
Basic script as follows:
COMPARE1=`ls|wc -l`
tar czf archive.tgz ~/path/to/file
COMPARE2=`tar tvzf archive.tgz|wc -l`
if [ $COMPARE1 -eq $COMPARE2 ]
then
echo "They are the same."
else
echo "They are not the same."
fi
So basically making sure that there are the same amount of files in the directory and in the archive. HOWEVER! The script isn't storing my variables. When I close the script and echo either of those variables it gives me a blank line instead of the word count. And obviously the script echos that they aren't the same.
What happens when the listing and the tarball refer to the same directory?
path="~/path/to/file"
COMPARE1=$(ls $path|wc -l)
tar czf archive.tgz $path
COMPARE2=`tar tvzf archive.tgz|wc -l`
if [ $COMPARE1 -eq $COMPARE2 ]
then
echo "They are the same."
else
echo "They are not the same."
fi
As Corona points out above you don't have access to a variable once its parent process is finished. You can of course add debug statements to your script to help you find out what's going wrong.