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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-18-2008, 06:18 AM
|
#1
|
|
Member
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 129
Rep:
|
Any expect script experts
I have a problem
Quote:
#!/projects/john/bin/expect
set var "f in `ls -tr /projects/johndir/john/expect/backupdir/testtest/*.txt`"
send "ssh -p 22 john@server.com for $var;do mv $f /projects/johndir/john/expect/backupdir;done\n"
expect "h>"
|
I cant able to do this, it gives an error
Quote:
can't read "f": no such variable
while executing
"send "ssh -p 22 john@server.com for $var;do mv $f..."
(file "./mytesting.exp" line 19)
|
but when i execute the
Quote:
for $var;do mv $f /projects/johndir/john/expect/backupdir;done
|
it works, ...whats the problem in expect script..please help
|
|
|
|
07-18-2008, 06:51 AM
|
#2
|
|
Member
Registered: Feb 2006
Distribution: Debian, Maemo
Posts: 341
Rep:
|
Instead of using the set command, why don't you use a foreach?
Code:
foreach file [ls] {
#do something
}
Last edited by arungoodboy; 07-18-2008 at 06:52 AM.
Reason: bad formatting
|
|
|
|
07-18-2008, 06:51 AM
|
#3
|
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
If you want to execute a non-interactive command on a remote server with ssh, you simply don't need to use expect. Set up public key authentication (so you don't need a password to log in), and then use a shell script, like this:
Code:
#!/bin/bash
ssh john@server.com 'mv /projects/johndir/john/expect/backupdir/testtest/*.txt /john/expect/backupdir/'
You only need to use expect if the command you wish to execute prompts you for input with the keyboard. For security reasons, you should not use expect to respond to password prompts (because your expect script would have to contain a copy of your unencrypted password).
|
|
|
|
07-20-2008, 09:26 PM
|
#4
|
|
Member
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 129
Original Poster
Rep:
|
if normally in an expect script, u do this means in works
Quote:
spawn /bin/ksh
send "ssh -p 22 username@server.com cd /any/directory;ls -la file.txt\n"
expect "h>"
#note..already set up the server in such a way so the password is disabled.
|
the code aboe works, spawn a shell and then send commands to the waiting shell..
but y this doesnt work
Quote:
send "ssh -p 22 username@server.com for f in `ls -tr /projects/ilinterf/john/expect/backupdir/testtest/*.txt`;do mv $f /projects/johndir/john/expect/backupdir;done\n"
expect "h>"
|
it shows the error
Quote:
can't read "f": no such variable
while executing
"send "ssh -p 22 username@server.com for f in `ls -tr /projects/ilinterf/john/expect/backupdir/testtest/*.txt`;do mv $f..."
(file "./mytesting.exp" line 19)
|
because if i execute the script below on a shell means, it works. Y its not working on the expect script, which suppose to execute the shell script commands....
Quote:
for f in `ls -tr /projects/ilinterf/john/expect/backupdir/testtest/*.txt`;do mv $f /projects/johndir/john/expect/backupdir;done
|
pls help
|
|
|
|
07-21-2008, 02:27 AM
|
#5
|
|
Member
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 129
Original Poster
Rep:
|
is this correct
Quote:
for f in `ls -ltra /projects/ilinterf/john/expect/backupdir/testtest/*.txt`; do scp $f username@server.com:/projects/ilinterf/john/expect/backupdir; done
|
|
|
|
|
07-21-2008, 05:21 AM
|
#6
|
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
You expect script doesn't work because you have not spawned a process to expect output from or send input to.
In the vase of this:
Code:
for f in `ls -ltra /projects/ilinterf/john/expect/backupdir/testtest/*.txt`; do
scp $f username@server.com:/projects/ilinterf/john/expect/backupdir
done
A few comments: - You would be better off not calling ls with backtick expansion. The shell is already doing the expansion of the file pattern /projects/ilinterf/john/expect/backupdir/testtest/*.txt into a list of files - calling ls on that list has no use, and can cause a problem when there are spaces in one or more of the file names.
- You should get into the habit of quoting your expanded variables in the shell. The case where there is a space in a file name will cause the scp line to malfunction the way you did it.
These things considered, a better version would be:
Code:
for f in /projects/ilinterf/john/expect/backupdir/testtest/*.txt; do
scp "$f" username@server.com:/projects/ilinterf/john/expect/backupdir/
done
Even better, scp can take a list of files as arguments, so there's no need for the loop at all.
Code:
scp /projects/ilinterf/john/expect/backupdir/testtest/*.txt username@server.com:/projects/ilinterf/john/expect/backupdir/
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:23 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|