LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 05-04-2010, 01:54 AM   #1
edomingox
Member
 
Registered: Mar 2009
Posts: 49

Rep: Reputation: 15
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`

ls -al $OLDEST_FILE

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:

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, 09:05 PM   #2
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,267

Rep: Reputation: 99
Are you sure you can use -L switch ?

It is giving me an error in my RHEL machine.

Code:
-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 09:06 PM.
 
Old 05-09-2010, 01:51 AM   #3
edomingox
Member
 
Registered: Mar 2009
Posts: 49

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


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


All times are GMT -5. The time now is 11:13 PM.

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