Visit Our UNIX and Linux User Community


Simple echo problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple echo problem
# 1  
Old 11-14-2011
Question Simple echo problem

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:

Code:
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.

Help!
# 2  
Old 11-14-2011
What do you mean by 'close the script'?

The variables are only valid inside the script.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 11-14-2011
What happens when the listing and the tarball refer to the same directory?
Code:
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.
# 4  
Old 11-14-2011
Quote:
Originally Posted by Skrynesaver
As Corona points out above you don't have access to a variable once its parent process is finished.
... unless you source the script:
Code:
$ . /path/to/myScriptFile.sh
$ echo $COMPARE1 $COMPARE2

# 5  
Old 11-14-2011
Well that explains a lot! The more you know...

And the script is telling me the variables aren't equal because a tar file includes the directory, and an ls doesn't.

Last edited by nickzourdos; 11-14-2011 at 06:14 PM..
# 6  
Old 11-14-2011
tar will list the base directory as well I think:

Code:
tar -vtf filename
base_folder/
base_folder/file1
base_folder/file2
...

while ls would only list file1, file2, ...

tar also recurses, while ls doesn't.

How about find instead of ls? It prints output more like tar does...

Code:
find /path/to/folder | wc -l

It also has powerful options for filters and ways to do things using the filenames it finds, but if all you want is the listing...
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 11-14-2011
Quote:
Originally Posted by Corona688
How about find instead of ls? It prints output more like tar does...

Code:
find /path/to/folder | wc -l

It also has powerful options for filters and ways to do things using the filenames it finds, but if all you want is the listing...
And just like that... hours and hours of work is fixed. That's what I get for being a noob haha Smilie

Many thanks!

Previous Thread | Next Thread
Test Your Knowledge in Computers #913
Difficulty: Medium
In bash, grep -v -e '^[[:space:]]*$' filename.txt will filter all the blank lines from filename.txt and display the results.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

need simple echo question

Hi I want to use echo command as below echo 'dir=' $1 ' dir|file|home' i need output like below : echo 'dir=' $1 ' dir|file|home' pp13dff Output dir=pp13dff dir|file|home (4 Replies)
Discussion started by: asavaliya
4 Replies

2. Shell Programming and Scripting

Simple cat and echo question

Apologies, probably a really simple problem: I've got a text file (nh.txt) with this in it: user1 email1 email2 user2 email1 email2 etc With the following basic script: for tline in $(cat nh.txt) do echo "**********" echo $tline done ... (3 Replies)
Discussion started by: nelmo
3 Replies

3. Shell Programming and Scripting

"Simple" echo/reading variable question...

Hello, I have a simple(I think) question! Although simple, I have been unable to resolve it, so I hope someone can help! OK, here it is: 1)I have an awk script that prints something, such as: awk '{print $2}' a > x so x might hold the value of say '10' 2)Now, I just want to check to see if... (4 Replies)
Discussion started by: astropi
4 Replies

4. Shell Programming and Scripting

echo problem

hi all i have little problem below is my shell script a=`sqlplus fss_cst/fss_cst@dolp1 << EOF SET PAGESIZE 0 FEEDBACK OFF TRIMOUT ON; select process from lfs$ta_process where valid_to_dat=to_date('9/16/2010','mm/dd/yyyy'); EOF` echo ${SQL} the script name is test2.sh when i execute... (5 Replies)
Discussion started by: aishsimplesweet
5 Replies

5. Shell Programming and Scripting

Problem with echo *

Hello all, Please help with the below. I have a requirement where in I have to read a pattern and print it as shown below. Patterns will be as below. Input Output Pattern Should be printed as below with spaces such that I can awk. -*--* - * - - * *--**... (2 Replies)
Discussion started by: tenderfoot
2 Replies

6. UNIX for Dummies Questions & Answers

echo $ problem

Hi I am using tcsh. I want display in a file_1 like this. $VARIBALE I gave in a termianl > echo "\$VARIBALE" > file_1 Its not workning. It was giving VARIBALE: Undefined variable. I gave \ before $, but why it was giving undefined varible? Please help me. Thanks in advance (4 Replies)
Discussion started by: chaitubek
4 Replies

7. UNIX for Dummies Questions & Answers

Simple 'echo' question

Hi, I would like to output the identical line to 2 text files, ie output='blah' echo $output > test1.txt echo $output > test2.txt Is there a way I could do that output with ONE command, ie output='blah' echo $output > test1.txt & test2.txt (I know that doesn't work) Thanks for any... (1 Reply)
Discussion started by: msb65
1 Replies

8. Shell Programming and Scripting

echo problem

echo "XXXXX" >> /xx/output.txt cat /xx/file.txt| awk '{tony=tony+$1+$2; print tony/$3*100}' >> /xx/output.txt Dear all, In this situation i will have 2 lines in the output file. What i want is to have only one output line. e.g: XXXXX "value" HOW to put the output of the "cat and awk" in... (1 Reply)
Discussion started by: tontal
1 Replies

9. Shell Programming and Scripting

echo problem

Hi, I have given the following statement in a script to put the values of variables (VAR1, VAR2,...) in a file. echo " $VAR1 $VAR2 $VAR3 $VAR4 $VAR5" >> filename But the output is not coming properly. Variables VAR5, VAR4 are replacing the first (VAR1, VAR2,..). I can't... (5 Replies)
Discussion started by: abrd600
5 Replies

10. UNIX for Dummies Questions & Answers

`echo` problem.

Have the following lines in a script: echo "-----------------------------------------------"\ "---------------------" >> $xdfrpt echo "- Date - - / - /stand - /u - /u1 - /u2 -"\ "/x1 - /x2 - /x3 -" >> $xdfrpt echo... (1 Reply)
Discussion started by: Cameron
1 Replies

Featured Tech Videos