LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 06-13-2012, 02:19 AM   #1
uk.engr
Member
 
Registered: Apr 2012
Posts: 131

Rep: Reputation: Disabled
Problem in exporting variable from bash script to expect script


Assalam o Alaikum.

I can easily export variable from bash to bash script but not from bash to expect script. Here are my scripts:
[root@Zohaib 04]# vim initiate_operation.sh
#!/bin/bash
gxip= 192.168.1.121
mkdir -p /tmp/logs_auto
cp -n Email_words.txt /tmp/
export gxip
expect auto.sh

[root@Zohaib 04]# vim auto.sh
#!/usr/bin/expect -f
echo "$gxip"
spawn ssh root@$gxip
expect "*password*" {
send "123\r"
}

[root@Zohaib 04]# . ./initiate_operation.sh

can't read "gxip": no such variable
while executing
"echo "$gxip" "
(file "auto.sh" line 8)

Last edited by uk.engr; 06-13-2012 at 02:26 AM.
 
Old 06-13-2012, 09:57 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

Code:
gxip= 192.168.1.121
     ^
There can't be any spaces around the equals sign when setting a shell variable.
 
Old 06-13-2012, 10:48 AM   #3
micropanther
LQ Newbie
 
Registered: Oct 2007
Location: Charlotte, NC
Distribution: Ubuntu 10.04, Davinci (MacOS/X)
Posts: 20

Rep: Reputation: 0
Wow, that took a lot of digging.

Your answer is found in the TCL/TK Wiki

I hate redirection as an answer so here is what I got to work
  1. There is a space after the "=" in your gxip= 192. ... statement. No spaces allowed unless you put quotes around the string.
  2. echo is not an expect command. Use puts instead.
  3. The variable you want to use is a global environment variable in the environment of the bash shell. Tcl/Tk gets all the global environment vars into an array named env. You get to env by referencing $::env(name_of_the_global_env_var). So in your case you need to puts $::env(gxip). Similar fixes for other lines where gxip is used.
 
Old 06-14-2012, 01:57 AM   #4
uk.engr
Member
 
Registered: Apr 2012
Posts: 131

Original Poster
Rep: Reputation: Disabled
Thanks you so much. it works thanks a lot............................................
 
  


Reply



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
Problem running an Expect script within a Bash script mbeipi Programming 9 02-10-2018 05:00 AM
[SOLVED] /usr/bin/expect : Script to check server load using both expect and bash Soji Antony Programming 1 07-27-2010 11:27 PM
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 03:08 AM
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 02:40 PM
Bash Script Variable Problem jimmy512 Programming 5 05-11-2007 07:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:38 AM.

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
Open Source Consulting | Domain Registration