Trouble passing commands with expect


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trouble passing commands with expect
# 1  
Old 03-30-2013
Trouble passing commands with expect

Hello All,

I hope someone could help me with this.

I'm creating a shell script to run a process. The trouble is, part of the process has to be ran as a different user. I can 'su' to the user ok, but I'm having trouble passing a 'cd' command as well as some variables I set earlier in the script.

Here's what I have:

Quote:

file=`ls -ltr | tail -1`
expect << eof

spawn su - user2
expect "Password:"
send "password\r"
send "cd /working/dir1/\r"
send "cp $file /working/dir2\r"
send "exit\r"
expect eof
When I execute I get this error:

$ $ cp: SOMETHING.log: A file or directory in the path name does not exist.
$ invalid command name "cp"
while executing

Seems the 'cd' works, but it places a '$' in the beginning of the line, which is throwing off the rest of the script. My questions are, how can I 'cd' properly using expect and is it possible to pass a variable this way?
# 2  
Old 03-31-2013
Try to replace cd with \cd or chdir
And then, shouldn't there be another expect for each $ prompt?

Last edited by MadeInGermany; 03-31-2013 at 08:30 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expect script not passing password / commands ??

Newbie here. My goal is to have the expect script log into the Ubuntu 18.04 server and run two commands (lsb_release -a and ip addr) and eventually pipe the output/results to a file. For now, I would be happy to get this one command or two to run successfully. How to fix this? #!/usr/bin/expect ... (3 Replies)
Discussion started by: jacob600
3 Replies

2. UNIX for Beginners Questions & Answers

Trouble looping commands

Hi, I have encountered a problem that I am unable to find a workaround for. I have 52 numbers and I need to submit an individual job for each pair combination, so too many to do by hand. I have created a submission file (submission_code.sh) which contains the following code: gcta64... (2 Replies)
Discussion started by: tim.morris
2 Replies

3. Shell Programming and Scripting

Trouble using awk and sed commands

Hi i have a control file which i need to read. It is ',' separated. the 3rd parameter will be ';' separated. I have 2 files: /home/orig.txt /home/join.txt I need a O/P file name based on firstparameter_1.txt and it should have the content of /home/orig.txt and appended content from... (2 Replies)
Discussion started by: Ravindra Swan
2 Replies

4. Shell Programming and Scripting

Having trouble using expect to launch vpn program

Hello all. I am a linux and linux scripting newbie so please forgive my ignorance. I have been tasked to write what I thought should be a pretty simple script that does the following: 1) Loads our forticlient ssl vpn command line client 2) sends the vpn password to the client Pretty... (2 Replies)
Discussion started by: wblakenc
2 Replies

5. UNIX for Dummies Questions & Answers

Ps commands trouble

Hi, i am writing a bash file and need to list the command name and user for the busiest process ie the one using the greatest percentage of CPU time) im kind of stuck. i know you should get the highest CPU , sort column 3 then take off the head but i'm un sure how would i do that? echo... (2 Replies)
Discussion started by: ryoukii
2 Replies

6. Shell Programming and Scripting

Expect Script - Pattern Matching Trouble

I am still learning expect, For the below script I always get This is True as the answer. Tried to debug and does not make sense to me. Not sure, where I am doing the mistake. Need Help! - Thanks #!/usr/bin/expect -f set timeout 10 spawn -noecho bash expect { *$* } send "test -d... (3 Replies)
Discussion started by: rmsagar
3 Replies

7. Shell Programming and Scripting

Trouble with passing variable to sed

Here is my code #!/bin/bash username=gnowicki sed '$s/$/ $username/' < sshd_config 1 <> sshd_config what this is supposed to do is take the name gnowicki and put it at the end of the last line of the sshd_config and it works except not using the variable, if I put the name "gnowicki" where... (2 Replies)
Discussion started by: slufoot80
2 Replies

8. Shell Programming and Scripting

Passing values in expect package

i am having specific number of input( as per o/p of another script) say 10 like d1,d2,d3....d10 i want to pass these o/p to expect script.But i don't know how to do that the the input to the expect script should be like this /expect_script.exp $d1 $d2......$dn as this "dn" depends in... (6 Replies)
Discussion started by: sagar_1986
6 Replies

9. Shell Programming and Scripting

Trouble with passing Variable from bash to awk gsub command

Would really appreciate it if someone could point out my mistake in this line of code, i've been staring blankly at it trying everything i can think of some time now and coming up with nothing. #!/bin/bash echo "Enter Username" read Username awk -F: -v var=${Username} '/^var:/... (9 Replies)
Discussion started by: Nostyx
9 Replies

10. Shell Programming and Scripting

passing variable to expect

Please tell me how to pass variable "a b c" to expect from the shell script 1/ example of input file # cat in-file var1 var2 a b c var4 2/ # this is my script - how to pass "a b c" as single variable ? cat in-file | while read x do my-expect x done 3/ # expect script - how to... (0 Replies)
Discussion started by: breaktime123
0 Replies
Login or Register to Ask a Question