How to add if statement in expect script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add if statement in expect script
# 1  
Old 05-26-2011
How to add if statement in expect script

Hi,

I am new to expect script and I am having difficulty in adding an if statement into a expect FTP login script. Here is the code:

Code:
#!/usr/local/bin/expect -f

set FTP_SITE [lindex $argv 0]
set FTP_USER [lindex $argv 1]
set FTP_PASS [lindex $argv 2]
set FTP_FILE [lindex $argv 3]

spawn ftp
match_max 100000
expect -exact "ftp> "
send -- "open $FTP_SITE\r"
expect -re "Name "
send -- "$FTP_USER\r"
expect -re "Password:"
send -- "$FTP_PASS\r"
expect "ftp> "
send -- "lcd /tmp\r"
expect "ftp> "
send -- "ascii\r"
expect "ftp> "
if [ $FTP_USER = "p00df0rs20" ];
then
send -- "cd /new"
fi
send -- "put $FTP_FILE\r"
expect "ftp> "
send -- "bye\r"
expect eof

What I need to achieve is that if the user name is "p00df0rs20", change the FTP destination directory to '/new'. The if statement I added doesn't work and I received such an error message:
Code:
ftp> invalid command name "p00df0rs20"
    while executing
"$FTP_USER = "p00df0rs20" "
    invoked from within
"if [ $FTP_USER = "p00df0rs20" ]"
    (file "/DEV/db/devappl/fnd/11.5.0/bin/msc_ftp.sh" line 21)

Transfer failed

Appreciate if anyone could help me out here.

Thanks,

Jeff

Last edited by pludi; 05-27-2011 at 04:20 AM..
# 2  
Old 05-27-2011
Hi.

Use tcl if statement form:

Expect - Wikipedia, the free encyclopedia

Tcl Tutorial

and so on ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Expect script returning string following a found expect.

I'm fairly new to scripting so this might not be possible. I am using Expect with Cisco switches and need to capture the string after finding the expect request. For example, when I issue "show version" on a Nexus switch, I'm looking to capture the current firmware version: #show version ... (0 Replies)
Discussion started by: IBGaryA
0 Replies

2. Shell Programming and Scripting

If statement in expect script

Hi, Can anyone please help me with the below script, I'm trying to match the prompt output in "if statement" if it matches then send "y" else send "n" and come out. I'm not sure about the expect syntax. pls help me out. #!/usr/bin/expect -f set INPUT set timeout 60 spawn su - xxuser ... (0 Replies)
Discussion started by: Jai ganesh
0 Replies

3. Programming

Calling another expect script inside an expect script

I have an expect script called remote that I want to call from inside my expect script called sudoers.push, here is the code that is causing me issues: set REMOTE "/root/scripts/remote" ... log_user 1 send_user "Executing remote script as $user...\n" send_user "Command to execute is: $REMOTE... (1 Reply)
Discussion started by: brettski
1 Replies

4. Programming

Calling expect script inside another expect

Hi, Am very new to expect scripting.. Can You please suggest me how to call an expect script inside another expect script.. I tried with spawn /usr/bin/ksh send "expect main.exp\r" expect $root_prompt and spawn /usr/bin/ksh send "main.exp\r" expect $root_prompt Both... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

5. Shell Programming and Scripting

Do expect script have "or" statement?

Do expect script have "or" statement? sound like this: expect { "A" or "B" { send "123" } "C" { send "567" } I know expect can separate that to do this expect { "A" { send "123" } "B" { send "123" } "C" { send "567" } } (8 Replies)
Discussion started by: sk860811
8 Replies

6. Linux

Modify expect script with control statement

Ok, so I have this script that was provided to me by one of the posters on this site. This script seems to be perfect. However, since this is a telnet script, i need to add an if then statement to it but dont know how to do it. What i want to do is to have this script spit out a certain... (3 Replies)
Discussion started by: SkySmart
3 Replies

7. Shell Programming and Scripting

If and else statement with Expect

Hi, I have a script which is logging in to network devices via ssh using expect programming. My problem is that if I do Code: ssh host -l uname It only works if y ssh_config is setup to use Protocol 1,2 and my network device I am trying to connect has ssh 1 but not 2. If the... (0 Replies)
Discussion started by: kminev
0 Replies

8. Shell Programming and Scripting

Need help with Expect script for Cisco IPS Sensors, Expect sleep and quoting

This Expect script provides expect with a list of IP addresses to Cisco IPS sensors and commands to configure Cisco IPS sensors. The user, password, IP addresses, prompt regex, etc. have been anonymized. In general this script will log into the sensors and send commands successfully but there are... (1 Reply)
Discussion started by: genewolfe
1 Replies

9. Shell Programming and Scripting

strange expect script behavior, or am i misunderstanding expect scripting?

Hello to all...this is my first post (so please go easy). :) I feel pretty solid at expect scripting, but I'm running into an issue that I'm not able to wrap my head around. I wrote a script that is a little advanced for logging into a remote Linux machine and changing text in a file using sed.... (2 Replies)
Discussion started by: v1k0d3n
2 Replies

10. Shell Programming and Scripting

Expect control statement

I am using expect I dont know tcl but trying to use a control statement to send requests from an input file - dont know what I am doing to be honest as I dont know tcl and dont use expect too much... Any help? See below Basically I am opening a telnet session to a server which works fine... (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question