LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 12-03-2014, 08:16 AM   #1
jithuchandran
LQ Newbie
 
Registered: Dec 2014
Posts: 3

Rep: Reputation: Disabled
using spawn command to find the number of files in a directory.


Hi,
I was using spawn command to find the number of files in a directory. And i need to figure out the listing time using timex. i have used the below commands.
/usr/bin/timex /usr/local/bin/expect <<EOF
set timeout 180
spawn ls /tmp/test | wc -l
expect eof
EOF

But I was getting the below error while executing this.

/usr/bin/timex /usr/local/bin/expect <<EOF
> set timeout 180
> spawn ls /tmp/test | wc -l
> expect eof
> EOF
spawn ls /tmp/test | wc -l
|: No such file or directory
wc: No such file or directory
-l: No such file or directory
/tmp/test:

real 0.02
user 0.00
sys 0.00

Can any one please help me?

Regards
Sreejith
 
Old 12-03-2014, 08:54 AM   #2
gdejonge
Member
 
Registered: Aug 2010
Location: Netherlands
Distribution: Kubuntu, Debian, Suse, Slackware
Posts: 317

Rep: Reputation: 73
Hi,

Quote:
Originally Posted by jithuchandran View Post
Hi,
...snip...
spawn ls /tmp/test | wc -l
|: No such file or directory
wc: No such file or directory
-l: No such file or directory
/tmp/test:
...snip...
This normally means the shell or the command tries to use each piece as a single parameter.
You could try to use quotes or double qoutes to group them together.

Cheers
 
Old 12-03-2014, 09:50 AM   #3
jithuchandran
LQ Newbie
 
Registered: Dec 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Hi gdejonge,

i have tried that also before.
Below is the error seen at that time.

/usr/bin/timex /usr/local/bin/expect <<EOF
> set timeout 180
> spawn "ls /tmp/test | wc -l"
> expect eof
> EOF
spawn ls /tmp/test | wc -l
couldn't execute "ls /tmp/test | wc -l": no such file or directory
while executing
"spawn "ls /tmp/test | wc -l""

real 0.02
user 0.00
sys 0.00


/usr/bin/timex /usr/local/bin/expect <<EOF
> set timeout 180
> spawn 'ls /tmp/test | wc -l'
> expect eof
> EOF
spawn 'ls /tmp/test | wc -l'
couldn't execute "'ls": no such file or directory
while executing
"spawn 'ls /tmp/test | wc -l'"

real 0.02
user 0.00
sys 0.00
 
Old 12-08-2014, 01:46 AM   #4
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (Chicago area)
Distribution: Red Hat (8.0, RHEL5,6), CentOS, SuSE (10.x, 11.x, 12.2, 13.2), Solaris (8-10), Tru64, MacOS, Raspian
Posts: 1,113

Rep: Reputation: 65
Quote:
/usr/bin/timex /usr/local/bin/expect <<EOF
> set timeout 180
> spawn 'ls /tmp/test | wc -l'
> expect eof
> EOF
spawn 'ls /tmp/test | wc -l'
couldn't execute "'ls": no such file or directory
while executing
"spawn 'ls /tmp/test | wc -l'"

real 0.02
user 0.00
sys 0.00
It's been a while since I've writen any expect but if you can read the list of files into an array "filelist", you can get the file count using "array size filelist". If you don't need to know the names of the files but only the number of them, this little expect scriptlet does that:
Code:
#!/usr/bin/expect
set fh [open "| find /tmp/test -type f -print | wc -l"]
gets $fh cnt
puts $cnt
If you want more than just the files, omit the "-type f" argument.

Hope this helps some...

--
Rick
 
1 members found this post helpful.
Old 12-08-2014, 10:04 AM   #5
jithuchandran
LQ Newbie
 
Registered: Dec 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Hi Rick,
When I run the script, it is working properly. But I need to integrate the listing with timex command. I need to find how much time the lisitng is taking on the server.
Sreejith
 
Old 12-08-2014, 12:54 PM   #6
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (Chicago area)
Distribution: Red Hat (8.0, RHEL5,6), CentOS, SuSE (10.x, 11.x, 12.2, 13.2), Solaris (8-10), Tru64, MacOS, Raspian
Posts: 1,113

Rep: Reputation: 65
Quote:
Originally Posted by jithuchandran View Post
Hi Rick,
When I run the script, it is working properly. But I need to integrate the listing with timex command. I need to find how much time the lisitng is taking on the server.
Sreejith
So you want to move the "timex" command inside the Expect script.

I was thinking that something like:
Code:
#!/usr/bin/expect
set fh [open "| time find /tmp/test -type f -print | wc -l"]
gets $fh cnt
gets $fh blankline
gets $fh real
gets $fh user
gets $fh sys
puts $cnt
puts $real
puts $user
puts $sys
would work. Unfortunately, "time" (the equivalent of "timex" on my system) sends all its output to stderr so the gets statements that are attempting to read the times never receive anything. Changing the "time" part of the command to "time 2>&1" doesn't work either. I've also tried "time (find ... | wc -l) 2>&1" with no luck as well. Apparently, redirecting stderr is not working the way one would expect (pun not intended but there it is).

Back to the drawing board.

--
Rick
 
  


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
Linux command to find all files in a directory which contain ip addresses RMS# Linux - Newbie 4 04-02-2014 10:17 AM
How to find which directory has the most number of files? ashokingole Linux - Newbie 7 03-22-2013 06:30 PM
Linux command to find all files in a directory which contain ip addresses. Shakthi Yokesh Linux - Newbie 8 08-19-2012 06:31 AM
Find directory older than x and mv directory with sub files command ajhart Linux - Newbie 4 07-15-2011 06:24 AM
ls command and displaying the number of files in your current directory? revsarah Linux - General 5 10-15-2010 07:40 PM


All times are GMT -5. The time now is 10:58 PM.

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