Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-04-2010, 02:54 AM   #1
Registered: Mar 2009
Posts: 64

Rep: Reputation: 16
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.
Old 05-05-2010, 10:05 PM   #2
Senior Member
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
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'

Last edited by vikas027; 05-05-2010 at 10:06 PM.
Old 05-09-2010, 02:51 AM   #3
Registered: Mar 2009
Posts: 64

Original Poster
Rep: Reputation: 16
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.


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
How To pass argument in Expect command? jprathap Linux - Newbie 3 08-27-2009 05:58 PM
How to pass the result of a command to another command (like grep) desb01 Programming 4 06-25-2009 01:09 PM
How to pass a result of exec command in perl to a variable??? HyperTrey Programming 3 05-23-2008 01:47 PM
What's the shortest amount of time to expect to pass the RHCE/T MasterC Linux - Certification 10 01-30-2006 01:58 PM
How do I pass a C variable to a Bash command ? Linh Programming 6 07-07-2003 04:12 PM > Forums > Non-*NIX Forums > Programming

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration