LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   omitting space in shell script (https://www.linuxquestions.org/questions/linux-newbie-8/omitting-space-in-shell-script-499312/)

Fond_of_Opensource 11-07-2006 12:18 AM

omitting space in shell script
 
HI friends,

I want to find the number of files in a given directory

ls -1 directoryname | wc -l

It gives the number of files in the given directory. But I want to store the number of files given as output of this command in a shell variable and autoincrement it.

Please help.
Thanks in advance...

Tinkster 11-07-2006 02:21 AM

I think I understand the "store in shell variable" bit, but on the auto-
increment you lost me ... what are you trying to achieve?

export LS_NUM=` ls -1 directoryname | wc -l`


Cheers.
Tink

Oliv' 11-07-2006 12:40 PM

Hi,

I think that auto-increment is maybe to count for example the number of files which are under /var. So he may want to do a loop and auto-increment its variable.
in that case you can do:
LS_NUM+=$(($LS_NUM+` ls -1 directoryname | wc -l`))
or
LS_NUM=`expr $LS_NUM + ` ls -1 directoryname | wc -l``

Oliv'

matthewg42 11-07-2006 12:47 PM

Note: the execute and put the results here syntax with the `back-ticks` can also be achieved using $(dollar-braces), e.g.
Code:

echo "The number of files is $(ls |wc -l)"
This has the advantage of being nestable. The example above with nested back-ticks fails because the shell can't work out if it's nested executions, or two next to one another.

When you say "auto-increment" what do you mean? I read that as wanting:
Code:

LS_NUM=$(ls |wc -l)
let LS_NUM+=1

Is that what you wanted?

Quigi 11-07-2006 02:44 PM

What does this have to do with omitting space?

Fond_of_Opensource 11-08-2006 01:19 AM

hi,


I want to treat it as an integer in for loop in shell script.

my.dying.bride 11-08-2006 03:03 AM

Quote:

Originally Posted by Quigi
What does this have to do with omitting space?

......lol :)

matthewg42 11-08-2006 04:08 AM

Quote:

Originally Posted by Fond_of_Opensource
I want to treat it as an integer in for loop in shell script.

You don't give a lot away do you? Do you mean this?
Code:

LS_NUM=$(ls |wc -l)
for (( i=$LS_NUM ; i>0; i-- )); do
    echo "$i binary files, getting piped into wall"
    echo "$i binary files, getting piped into wall"
    echo "and if one little file, gets bitrot and sent to /dev/null"
    echo "...there'll be $(($i - 1)) file(s) left"
    echo ""
done

OK, I'm a bum poet. It's hard to know what you want to do without more information. I hope this helps.

unSpawn 11-08-2006 07:02 AM

Yeah, post the script.

Fond_of_Opensource 11-09-2006 03:45 AM

thanks to all.

problem solved.....:)

archtoad6 11-18-2006 09:18 AM

Please post the solution, you got a lot help here & part of the culture of LQ is to post the solution for others to benefit from. Besides, several of your helpers are curious about the triggering problem.


All times are GMT -5. The time now is 01:49 AM.