The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
within shell script send expect and if else mnmonu Shell Programming and Scripting 1 04-20-2009 06:41 AM
Using Expect results in a Shell script kaltekar Shell Programming and Scripting 2 10-17-2008 10:08 AM
Using expect script in a shell script or vice versa nua7 Shell Programming and Scripting 0 07-18-2008 08:16 AM
Need help with Expect and Shell script tonan Shell Programming and Scripting 1 04-10-2008 11:45 PM
Password changing in a Script (shell and expect) chellam Shell Programming and Scripting 1 08-15-2006 12:12 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-22-2009
mnmonu mnmonu is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 32
within shell script send expect and if else

Hi,

I have written one shell script , using that i am able to connect to remote machine but i have to

#!/usr/bin/expect -f
set address [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set OOLpath [lindex $argv 3]
set dbusername [lindex $argv 4]
set dbpasswd [lindex $argv 5]
set tnsname [lindex $argv 6]
set recdbusername [lindex $argv 7]
set recdbpasswd [lindex $argv 8]
set rectnsname [lindex $argv 9]
spawn ssh ${username}@${address}
expect "${username}@${address}'s password:"
send -- "${password}\r"
expect "$ "
send -- "cd ${OOLpath}\r"
expect "$ "
send -- "cd Client\conf\r"
##########IF-ELSE Block



expect eof

but I need to check if one file(named parfile) is exist or not and if exist then change some parameter value

cat parfile
dbtnsname = clienttns

dbuserid = scott

dbpasswd = tiger

and if not exist then copy from some directory and then change parameter dbuserid,dbpasswd value.


Please Please help me.Thanks in advance.
  #2 (permalink)  
Old 04-22-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,311
I have no experience with expect but I checked the man page and didn't find any test conditions. So maybe you are better off writing this part with if [[ -e somefile ]]; then .... fi in some shell script and embedd the expect part into the shell script.
  #3 (permalink)  
Old 04-23-2009
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 717
Hi.

Here is an expect script that connects and then checks for a file t2. Perhaps this will help until someone more knowledgeable stops by:

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

# @(#) e4       Demonstrate running commands on remote computer with expect.

# Identify local computer, version of expect.
puts " Local computer is [eval exec uname -n]"
puts " Version of expect is [exp_version]."
puts ""
# Read login-name, computer-name, password from file.
set file "data1"
if { ! [file exists $file] } {
  puts " File $file not found -- exiting."
  exit 1
}
set input [open $file "r"]
set line [gets $input]
set list [split $line]
set login [lindex $list 0]
set box [lindex $list 1]
set password [lindex $list 2]
puts " login is $login, intended remote computer is $box"
set timeout 10
send_user " spawning: ssh $login@$box\n"
spawn ssh $login@$box
expect \[pP]assword:*
send "$password\r"
# send_user "(Got string Password*, sent password $password.)\n"
expect *$box*
# send "ls\r"
send {
bash <<EOF
echo " Hi from a here document on computer $(uname -n)."
if [ ! -f t2 ]
then
  echo " There is no file t2, creating it."
  touch t2
else
  echo " File t2 exists, using it."
fi
ls -l t2
rm t2
EOF
}
expect *$box*
send "exit\r"
expect "logout*"

Producing:

Code:
% ./e4
 Local computer is leap
 Version of expect is 5.42.1.

 login is vanilla, intended remote computer is vm-lenny
 spawning: ssh vanilla@vm-lenny
spawn ssh vanilla@vm-lenny
vanilla@vm-lenny's password:
Linux vm-lenny 2.6.26-2-686 #1 SMP Thu Mar 26 01:08:11 UTC 2009 i686

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Apr 23 09:37:28 2009 from leap

bash <<EOF
echo " Hi from a here document on computer $(uname -n)."
if [ ! -f t2 ]
then
  echo " There is no file t2, creating it."
  touch t2
else
  echo " File t2 exists, using it."
fi
ls -l t2
rm t2
EOF
vanilla@vm-lenny:~$
vanilla@vm-lenny:~$ bash <<EOF
> echo " Hi from a here document on computer $(uname -n)."
> if [ ! -f t2 ]
> then
>   echo " There is no file t2, creating it."
>   touch t2
> else
>   echo " File t2 exists, using it."
> fi
> ls -l t2
> rm t2
> EOF
 Hi from a here document on computer vm-lenny.
 There is no file t2, creating it.
-rw-r--r-- 1 vanilla vanilla 0 2009-04-23 10:13 t2
vanilla@vm-lenny:~$ exit
logout

If you are going to use expect extensively, I suggest the book below ... cheers, drl

Quote:
Title: Exploring Expect
Subtitle: A Tcl-based Toolkit for Automating Interactive Programs
Author: Don Libes
Edition: First
Date: December 1994
Publisher: O'Reilly
ISBN: 1-56592-090-2
Pages: 602
Categories: scripting, interacting, automating, system administration
Comments: 3.5 stars (25 reviews) Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more
Comments: elderly book, but still useful
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:24 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0