PATH Variable with the $User variable

ammonsc
Contributor II

I have a script that has the the following variables

## Get User Name.
loggedInUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`

PATH1='/Users/$loggedInUser/path/to/folder/'

Then it uses expect to pass the path to a SCP command. When passing the path it leaves the text as "$loggedInUser" and does not put the username in

/usr/bin/expect<<EOD

# Set Timeout of Expect

set timeout -1

spawn /usr/bin/scp -r $PATH1 $USERNAME@$HOST:/Volumes/Storage/sftp/$loggedInUser/

expect "Password:"
send "$PASSWD
"
EOD

Any ideas how to get the path to contain the username?

5 REPLIES 5

hkabik
Valued Contributor

Remove both ' from the variable. If you have spaces and such and don't want to deal with backslashes you could always use double quotes (") instead.

ammonsc
Contributor II

That worked. Now the script just exits and never passes the password with the expect.

hkabik
Valued Contributor

Have you tried just putting the entire script in expect?

!/usr/bin/expect -f

Or is this part of a greater script? I've never really worked with expect calls within another script so I'm not sure if anything else is necessary.

In any event, the guy in this link is attempting to do something similar and someone uses a similar script to yours but appends:

expect " "
send " "

After the password send. Not sure what's happening there exactly but worth a shot.

http://serverfault.com/questions/209248/can-i-use-expect-with-scp-to-bypass-the-password-prompt

ammonsc
Contributor II

Tried using the /usr/bin/expect

#!/usr/bin/expect -f

## Set variables here
## JSS parameters are -1

set user [lindex $argv 2]
set host [lindex $argv 3]
set password *putpasswordhere*
set path1 [lindex $argv 5]
set path2 [lindex $argv 6]
set path3 [lindex $argv 7]
set path4 [lindex $argv 8]
set path5 [lindex $argv 9]
set path6 [lindex $argv 5]

spawn scp -r $path1 root@$host:/Volumes/Storage/sftp/$user/
expect {
    "*?word:" {
    send "$password
"
    exp_continue
    }
}

spawn scp -r $path2 root@$host:/Volumes/Storage/sftp/$user/
expect {
    "*?word:" {
    send "$password
"
    exp_continue
    }
}

The path variables are not able to use the $user in the parameter and you have to type in the username .

like /Users/username/path/to/folder/

I would like it to be /Users/$user/path/to/folder/

hkabik
Valued Contributor

This may sound dumb... but have you tested it in non-script form? Are you maybe getting an ECDSA key fingerprint warning? That would bring up this query and maybe interfere with your expect:
"Are you sure you want to continue connecting (yes/no)?"