LinuxQuestions.org
Register a domain and help support LQ
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 11-07-2006, 12:18 AM   #1
Fond_of_Opensource
Member
 
Registered: May 2006
Posts: 55

Rep: Reputation: 15
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...
 
Old 11-07-2006, 02:21 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
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
 
Old 11-07-2006, 12:40 PM   #3
Oliv'
Senior Member
 
Registered: Jan 2004
Location: Montpellier (France)
Distribution: Gentoo
Posts: 1,014

Rep: Reputation: 36
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'
 
Old 11-07-2006, 12:47 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
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?
 
Old 11-07-2006, 02:44 PM   #5
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
What does this have to do with omitting space?
 
Old 11-08-2006, 01:19 AM   #6
Fond_of_Opensource
Member
 
Registered: May 2006
Posts: 55

Original Poster
Rep: Reputation: 15
hi,


I want to treat it as an integer in for loop in shell script.
 
Old 11-08-2006, 03:03 AM   #7
my.dying.bride
LQ Newbie
 
Registered: Nov 2006
Distribution: Vector Soho Edition 5.1(Best yet)
Posts: 25

Rep: Reputation: 15
Quote:
Originally Posted by Quigi
What does this have to do with omitting space?
......lol
 
Old 11-08-2006, 04:08 AM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
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.
 
Old 11-08-2006, 07:02 AM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,988
Blog Entries: 54

Rep: Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743
Yeah, post the script.
 
Old 11-09-2006, 03:45 AM   #10
Fond_of_Opensource
Member
 
Registered: May 2006
Posts: 55

Original Poster
Rep: Reputation: 15
thanks to all.

problem solved.....
 
Old 11-18-2006, 09:18 AM   #11
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 230Reputation: 230Reputation: 230
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 06:29 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
cp: omitting error jsolanky Linux - Software 10 11-28-2004 09:09 AM
formating console output (omitting parts you do not want) bpk Linux - Newbie 6 06-10-2004 07:32 PM
Space Key in Shell Script joshlamerritt Linux - General 1 02-20-2004 01:31 PM


All times are GMT -5. The time now is 10:21 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration