LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   using spawn command to find the number of files in a directory. (https://www.linuxquestions.org/questions/linux-newbie-8/using-spawn-command-to-find-the-number-of-files-in-a-directory-4175527190/)

jithuchandran 12-03-2014 07:16 AM

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

gdejonge 12-03-2014 07:54 AM

Hi,

Quote:

Originally Posted by jithuchandran (Post 5278739)
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

jithuchandran 12-03-2014 08:50 AM

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

rnturn 12-08-2014 12:46 AM

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

jithuchandran 12-08-2014 09:04 AM

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

rnturn 12-08-2014 11:54 AM

Quote:

Originally Posted by jithuchandran (Post 5281346)
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


All times are GMT -5. The time now is 05:18 AM.