Value of variable not getting passed in child script


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Value of variable not getting passed in child script
# 1  
Old 06-21-2011
Value of variable not getting passed in child script

Hi,
I am facing a challenge in fixing an issue in my installation scripts.Here is a situation:
There are 3 files which are invoked at a below given order:
Installer.ksh----->Installer.xml(Ant script)------->common.ksh
I am outputting a message from common.ksh at a terminal, after that trying to read a variable. Problem is that value of a variable is not coming in common.ksh.
Here is my code used in common.ksh:
print "Do you want to continue (y/n) ? " | tee -a /dev/tty
read answer
if [[ "$answer" = "y" || "$answer" = "Y" ]];

I could not see $answer in my logs.
# 2  
Old 06-21-2011
Ksh has possibility to read with a prompt:
Code:
~ [12]$ read var?"What? " 
What? no
~ [13]$ echo $var 
no

# 3  
Old 06-25-2011
Quote:
Originally Posted by yazu
Ksh has possibility to read with a prompt:
Yes, but the problem is not with the prompting in this case, it is with the read.

Quote:
Originally Posted by baig_1988
Here is my code used in common.ksh:
Code:
print "Do you want to continue (y/n) ? " | tee -a /dev/tty
read answer
if  [[ "$answer" = "y" || "$answer" = "Y" ]];

I could not see $answer in my logs.
Given that you are having to tee the prompt to /dev/tty suggests that one of the earlier scripts has closed/redirected stdout, and if this has been done, it is quite likely that stdin has also been redirected or closed. Have you tried this:

Code:
read answer </dev/tty

I would also add a bit of debugging after the read if it's still failing:

Code:
read answer </dev/tty
echo "after read, answer=$answer" >/dev/tty

You'd at least know what it got, and whether or not it actually paused for the input. If stdin was closed or coming from a device other than the tty, then there'd be no wait, but if your script isn't writing anything else to the tty device you'd not notice that without some other message to the tty.

Hope this helps a bit.
# 4  
Old 06-27-2011
Quote:
print "Do you want to continue (y/n) ? " | tee -a /dev/tty
read answer
if [[ "$answer" = "y" || "$answer" = "Y" ]];
The above code is very unusual.
This would normal if you just want to read what someone typed in response to the question:
Code:
print "Do you want to continue (y/n) ? "
read answer
if  [[ "$answer" = "y" || "$answer" = "Y" ]];


Code:
I could not see $answer in my logs.

It would be helpful to see all the scripts.
If you are trying to read the value of $answer in the calling process, this is impossible. A variable created in a sub-script is not available to the calling script. There are techniques to get round this.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash variable not being passed

In the bash below the variable date displays in the echo. However when I use it in the for loop it does not. Basically, the user inputs a date then that date is converted to the desired format of (month-day-year, no leading 0). That input is used in the for loop to return every file that matches... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Variable passed as argument

I have a script. #!/bin/sh cur_$1_modify_time=Hello echo "cur_$1_modify_time" When I run like sh /root/script1 jj I expect value "Hello" being assigned to variable "cur_jj_modify_time" and output being "Hello" ie echoing $cur_jj_modify_time But the output comes as # sh... (3 Replies)
Discussion started by: anil510
3 Replies

3. Shell Programming and Scripting

In the sh file variable is not being passed on.

I am having difficulties with the fllowing script: !/bin/sh voicemaildir=/var/spool/asterisk/voicemail/$1/$2/INBOX/ echo `date` ':' $voicemaildir >> /var/log/voicemail-notify.log for audiofile in `ls $voicemaildir/*.wav`; do transcriptfile=${audiofile/wav/transcript} ... (4 Replies)
Discussion started by: ghurty
4 Replies

4. UNIX for Dummies Questions & Answers

update value in a file using variable passed via unix script

I have a file in my unix directory called "restart_job1.job". Below is a sample of the script where I am doing a 'grep' to check specifically for an oracle error. If the value 'ORA-" is found, I set a variable to hold the return code value (job1_return_code). If the return code is non zero, I... (2 Replies)
Discussion started by: ncsthbell_dr
2 Replies

5. Shell Programming and Scripting

My variable cannot be passed through into my path

Hi, Can you please help. I am scripting in sh and I am trying to simply copy one directory to another but for some reason my variables are not recognised? echo "The latest version of the program is being found......." cd $SOFTWARE/src/$progname version=`ls $SOFTWARE/src/$progname | grep... (13 Replies)
Discussion started by: cyberfrog
13 Replies

6. UNIX for Dummies Questions & Answers

to read variable from child script

Hi I have two shell scripts A and B A calls B I would like to use a variable in A the value of which is assigned by B after calling B Thanks in advance Suresh (8 Replies)
Discussion started by: ssuresh1999
8 Replies

7. Shell Programming and Scripting

Passing a variable from a child script back to the parent

Hi I have written a script using ftp to get files from one server and copy them to 2 dirrerent servers. I wish to call this script from a parent script that will check the number of files copied and run a check sum for each file. As the filenames for the files in the get portion of the script... (3 Replies)
Discussion started by: Andy82
3 Replies

8. Shell Programming and Scripting

Perl script variable passed to Oracle query

Hi All, I pass a Perl script variable, whch is passed to a query to be prepared. But the problem is I have special character like '&' in this variable which are handled in a special way by the Oracle query parser. How do I get over this? my $cust_name='A&B'; my $sql="Select cust_short_name... (1 Reply)
Discussion started by: rahulrathod
1 Replies

9. UNIX for Dummies Questions & Answers

variable passed to awk

Does anybody know how to print a variable passed to awk command? awk -F"|" 'BEGIN {print $job,"\n","Question \n"} {print $1,$2$4," ",$3}' "job=$job1" file1 I am trying to pass job the variable job1. the output is blank. ?? (3 Replies)
Discussion started by: whatisthis
3 Replies

10. UNIX for Dummies Questions & Answers

variable passed to awk

Anybody know what's wrong with this syntax? awk -v job="$job" 'BEGIN { FS="|"} {print $1,$2," ",$4," ",$3\n,$5,"\n"}' list It's keeping give me this message: awk: syntax error near line 1 awk: bailing out near line 1 It seems awk has problem with my BEGIN command. Any... (8 Replies)
Discussion started by: whatisthis
8 Replies
Login or Register to Ask a Question