LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-05-2013, 10:46 AM   #1
kingissingh@gmail.com
LQ Newbie
 
Registered: May 2013
Distribution: Ubuntu,Fedora,CentOS,Solaris 10, RHEL
Posts: 6

Rep: Reputation: Disabled
Cool expect script returns "bash:unary operator expected"


Hi Guys,

I have a bash script that runs without any error, but when spawned with an expect script, it returns "Unary operator expected" error.

Bash Script(/home/bashscrpt):

#!/bin/bash
read -p "Enter details(Firstname Lastname id):" name1 name2 aid

ssh hostname.example.net "useradd -c '$name1 $name2' -g grp1 -n -s /bin/ksh $aid"
exit 0;


Expect script(/home/expectscrt):

#!/usr/bin/expect -f
set timeout 30
spawn /home/bashscrpt
expect {
-nocase "password:" {
send "mypassword\r"
exp_continue
}
}

Below is the o/p:

1) Executing the bash scrt directly - runs fine
[root@localhost]# ./bashscrpt
Enter details (Firstname Lastname Aid): test user tu1234
root@hostname.example.net's password:
[root@localhost]#



2) Now executing via the expectscrt - returns error

[root@localhost]# ./expectscrt
spawn /home/bashscrpt
Enter details (Firstname Lastname Aid): test user tu1234
[root@localhost]# test user tu1234
bash: test: user: unary operator expected
[root@localhost]#


Someone please tell me why I'm getting this error and way to workaround this.
 
Old 10-05-2013, 11:23 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
i would work around it by not using expect to send clear text passwords. instead use ssh with keys.

it seems like expect is trying to run the test command.
 
1 members found this post helpful.
Old 10-05-2013, 11:32 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,600

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
I think your ssh command line needs the ssh option -t, so that a terminal session is associated with it.

the "[root@localhost]# test user tu1234" prompt tells you it is a local prompt, and test is a expression evaluator... so it should report invalid operator, as there isn't one.
 
1 members found this post helpful.
Old 10-06-2013, 11:33 AM   #4
kingissingh@gmail.com
LQ Newbie
 
Registered: May 2013
Distribution: Ubuntu,Fedora,CentOS,Solaris 10, RHEL
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thank you schneidz and jpollard for the info, it was really helpful.
 
  


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
Bash problem : -bash: [: /bin/bash: unary operator expected J.A.X Linux - Software 1 09-22-2011 06:52 AM
NEWBIE ....need some help with "unary operator expected" error msg learning78 Linux - Newbie 6 11-11-2010 01:21 AM
Unary Operator expected. Bash script Blackout_08 Programming 2 06-22-2006 03:21 PM


All times are GMT -5. The time now is 03:58 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration