Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can't put value in variable, why not?

Status
Not open for further replies.

rcoston

Programmer
Oct 22, 2003
7
US
nbroffilles = ls -l *.Z | wc -l
echo $nbroffiles

The above script does not return the number of *.Z files in the directory that I run the script from. The error message I get is:
nbroffiles: not found

IF I run the command - ls -l *.Z | wc -l from the command line it returns a number equal to the number of *.Z files in the directory I'm in.

How can I deposit this number into a variable in a ksh script?
 
First you need to lose some spaces. Try this...
Code:
nbroffilles=`ls -1 *.Z | wc -l`
echo $nbroffiles
Hope this helps.

 
nbr='ls -l *.Z | wc -l'
echo $nbr

When I try this I get a result that looks like:

ls -l a.Z b.Z c.Z d.Z where a b and c are names of *.Z files.

What I really want is to have the variable $nbr be a number 4 or whatever number equals the number of *.Z files in that directory so that I may check it in an if statement later on such as if there are *.Z files in a certain directory uncompress them and move them. What appeared simple is now taking all my time on elementary UNIX. Frustrated, and I still need help...
 
Thanks works like a charm, you're awesome!!!
 
rcoston,

You're using the wrong characters! It's...
Code:
    nbr=`ls -1 *.Z | wc -l`
...not...
Code:
    nbr='ls -l *.Z | wc -l'
It's not the single quote, it's the "back tick" which is under the tilde character (~) on the keyboard.

Hope this helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top