LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-08-2008, 07:33 AM   #1
amit_pansuria
Member
 
Registered: Sep 2006
Posts: 73

Rep: Reputation: 15
how to get second shell script flag value from the first shell script


helo I have two shell script.I m calling second script from first script in which in second script i will set one flag and exit script 2. now I want to get value of the flag in my first script how do i get that?

script 1:


#!/bin/bash

echo " calling script 2"
. ./script2
if [ $flag=0 ]
then
echo "flag = $flag"
echo " exiting script 1"
exit 0;
else
echo "do some thing"

exit 0;


script 2:

#!/bin/bash
flag=1
if [ -s /tmp/time.cfg ]
then
echo " file found"
else
export flag=0;
exit 0; { here I must have to exit script2 if file is not found }
fi

echo " do some process"

exit 0;


I dont get value of flag in script1

Regards,
Amit
 
Old 08-08-2008, 08:17 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Please post code in [code] tags to aid readability.

These can't be the actual scripts you're using. Script 1 has several errors in the code - the comparison in the if statement should have spaces between the = and the operands of the comparison, and you have no closing "fi".

You also do not need ; after a command unless you want to follow it with another command on the same line.

I did a test, but with scripts which do not contain errors - works OK for me:
Code:
$ cat script1.sh
#!/bin/bash

var2="apples"
echo "this is script1.sh, about to source script2.sh var1=\"$var1\", var2=\"$var2\""
. ./script2.sh
echo "this is script1.sh, after sourcing script2.sh var1=\"$var1\", var2=\"$var2\""

$ cat script2.sh
#!/bin/bash

var1="oranges"
var2="bananas"
echo "this is script 2, var1=\"$var1\" var2=\"$var2\""

$ ./script1.sh
this is script1.sh, about to source script2.sh var1="", var2="apples"
this is script 2, var1="oranges" var2="bananas"
this is script1.sh, after sourcing script2.sh var1="oranges", var2="bananas"
 
Old 08-08-2008, 08:19 AM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
edit script 2 so that you are doing:
"export flag=1" instead of just "flag=1"
You then don't need to export in the else you can just do "flag=0".

The first set is initializing the variable but isn't exporting so if you don't get to the else conditional it is never getting back to the first script.
 
  


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
Menu bashed shell script for users shell 0.o Linux - Server 6 02-14-2013 06:00 AM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 06:29 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:16 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
Open Source Consulting | Domain Registration