-   Programming (
-   -   How do you pass an expect command result into a variable and then use it again? (

edomingox 05-04-2010 02:54 AM

How do you pass an expect command result into a variable and then use it again?
I'm trying to create a program that would locate the oldest file of a certain type on a server. Here's the commands:

OLDEST_PATH=`find -L / -depth -maxdepth 6 -mindepth 6 -type d | sort -f | head -1`

OLDEST_FILE=`find -L $OLDEST_PATH | grep .mp3 | sort -f | head -1`


I'm writing this all in expect but I'm having problems. The main problem I have is whenever I try to run the first command, I can't seem to isolate the result of the OLDEST_PATH so that the 2nd command will work. There always seems to be a newline in the variable and the result is only "find -L" command running and it bypasses the variable. If I can just figure out how to get the 1st and 2nd command to work, then I can figure out the 3rd. Here's some code:


send "find -L / -depth -maxdepth 6 -mindepth 6 -type d | sort -f | head -1\r"
sleep 20
expect -re "(.*)\n"
set OLDEST_PATH [lindex [split $expect_out(buffer) " "] 16]
set OLDEST_PATH [string trimleft "$OLDEST_PATH" "-1"]
set OLDEST_PATH [string trim "$OLDEST_PATH" "\n"]

send "find -L $OLDEST_PATH | grep al2 | sort -f | head -1\r"
sleep 5

I know there is a better way to write this. I've tried multiple ways and this just happens to be the last way I've tried it. If you try running this, you'll notice that there is still carriage returns after the result of OLDEST_PATH and it prevents the 2nd "find" command from working properly. Any help would be great.

vikas027 05-05-2010 10:05 PM

Are you sure you can use -L switch ?

It is giving me an error in my RHEL machine.


-sh-3.00$ find -L / -depth -maxdepth 6 -mindepth 6 -type d | sort -f | head -1
find: invalid predicate `-L'

edomingox 05-09-2010 02:51 AM

The -L switch works for me. i'm using fedora 8.

I found a way to get the variable. Kind of a round about, unclean way to get it but i got it.

what I do is get the length of the string and just copy over the string minus 2 characters.

expect -re "(.*)\n"
set OLDEST_PATH [lindex [split $expect_out(buffer) "\n"] 1]
set LENGTH [string length "$OLDEST_PATH"]
set LENGTH [expr $LENGTH - 2]
set OLDEST_PATH [string range "$OLDEST_PATH" 0 $LENGTH]

this seems to work to get rid of the \n character.

All times are GMT -5. The time now is 04:19 PM.