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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
04-25-2012, 02:21 PM
|
#1
|
LQ Newbie
Registered: Nov 2011
Posts: 15
Rep:
|
Break command in Linux
Hi
cat $DATAFILE | while read LINE
do
REC_ID=`echo $LINE | awk -F: '{print $1}'`
echo "rec_id value is:$REC_ID"
if [ $REC_ID = $1 ]
then
echo "iam in step1"
RUNCNTL=$REC_ID
echo "runcntl is $RUNCNTL"
break
fi
echo "runcntl is $RUNCNTL"
after the break command,the value for RUNCNTL gets lost!!
do you know why? or any other command can i use in place of break command,so value does not lost?
Thanks for reply
|
|
|
04-25-2012, 02:25 PM
|
#2
|
Senior Member
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604
|
With for and while loops the variables inside of the them are local to the loop only and are not global. You can export the variable and make it global. Do some searching on exporting variables.
Also please use the '[CODE]' tags for your code.
Last edited by Kustom42; 04-25-2012 at 02:27 PM.
|
|
|
04-25-2012, 02:29 PM
|
#3
|
Member
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594
Rep:
|
Add at the beginning of your script
Code:
RUNCNTL=""
export RUNCNTL
|
|
|
04-25-2012, 02:30 PM
|
#4
|
Senior Member
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604
|
You need to export the variable inside of the loop not before it.
|
|
|
04-25-2012, 02:57 PM
|
#5
|
Member
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594
Rep:
|
Why?
Code:
#!/bin/bash
RUNCNTL=""
export RUNCNTL
for ((i=1; i<=100; i++))
do
RUNCNTL=$i
if (( i == 50 ))
then
break
fi
done
echo $RUNCNTL
Code:
bash-4.1$ ./s.sh
50
|
|
|
04-25-2012, 04:07 PM
|
#6
|
LQ Newbie
Registered: Nov 2011
Posts: 15
Original Poster
Rep:
|
Still not working
#!/bin/sh
RUNCNTL=""
export RUNCNTL
cat $DATAFILE | while read LINE
do
REC_ID=`echo $LINE | awk -F: '{print $1}'`
echo "rec_id value is:$REC_ID"
RUNCNTL=$REC_ID
#echo $RUNCNTL
if (( $REC_ID == $1 ))
#if [ $REC_ID = $1 ]
then
break
fi
done
echo $RUNCNTL
output:
./nishanth1.sh aa_distributed
rec_id value is:aa_distributed
|
|
|
04-25-2012, 04:26 PM
|
#7
|
Senior Member
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604
|
Try:
Code:
#!/bin/sh
RUNCNTL=""
cat $DATAFILE | while read LINE
do
REC_ID=`echo $LINE | awk -F: '{print $1}'`
echo "rec_id value is:$REC_ID"
RUNCNTL=$REC_ID
export RUNCNTL
#echo $RUNCNTL
if (( $REC_ID == $1 ))
#if [ $REC_ID = $1 ]
then
break
fi
done
echo $RUNCNTL
|
|
|
04-25-2012, 08:49 PM
|
#8
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by Kustom42
With for and while loops the variables inside of the them are local to the loop only and are not global
|
That is not correct:
Code:
c@CW8:/tmp$ foo=
c@CW8:/tmp$ for ((i=0; i<5; i++)); do foo=bar; break; done
c@CW8:/tmp$ echo $foo
bar
|
|
|
04-25-2012, 08:57 PM
|
#9
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
In pipelines the individual commands are run in subshells so any changes made to variables while executing the pipeline are not available to the calling shell:
Code:
c@CW8:/tmp$ foo=
echo 'a
b' | while read line; do echo $line; foo=bar; echo $foo; break; done
a
bar
c@CW8:/tmp$ echo $foo
[no output]
A workaround is to use input redirection into the loop:
Code:
c@CW8:/tmp$ foo=
c@CW8:/tmp$ while read line; do echo $line; foo=bar; echo $foo; break; done < input.txt
a
bar
c@CW8:/tmp$ echo $foo
bar
|
|
|
04-25-2012, 10:20 PM
|
#10
|
LQ Newbie
Registered: Nov 2011
Posts: 15
Original Poster
Rep:
|
still not working guys!
|
|
|
04-25-2012, 10:47 PM
|
#11
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Then it would be helpful to see your latest script, the input file, the command you used to run the script and the output.
|
|
|
04-25-2012, 11:11 PM
|
#12
|
LQ Newbie
Registered: Nov 2011
Posts: 15
Original Poster
Rep:
|
#!/bin/sh
RUNCNTL=""
export RUNCNTL
BASEDIR=/psoft/batch/fsdev
DATAFILE=$BASEDIR/scripts/alloc_runctl_groups.dat
cat $DATAFILE | while read LINE
REC_ID=`echo $LINE | awk -F: '{print $1}'`
do
RUNCNTL=$REC_ID
if (( $1 == $REC_ID ))
then
break
fi
done
echo $RUNCNTL
----------------------------------------
output:
$ ./nishanth1.sh ca_distributed_allocs(Output is blank)
----------------------------------------------
Input file:
cat alloc_runctl_groups.dat
ca_distributed_allocs:CAEMP/EMaaaEE/RENT/
ca_canada:CACAJJKKLLNADA/CANbbbxxxmmmADA
ca_ou_fte:CAOUFTE/FTEzzaa
ca_ou_alloc_stip:CAOUOOKKJJHSTIP/OU ALIKLAYLOCATIONS STIP-ICP#CDOPPYYGGHHUSTIP/OU AKKLLHBYLLOCATIONS
|
|
|
04-26-2012, 01:53 AM
|
#13
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Of course it does not work like that. All you had to do was change to the solution given in post 9:
Code:
#!/bin/bash
RUNCNTL=""
export RUNCNTL
BASEDIR=/psoft/batch/fsdev
DATAFILE=$BASEDIR/scripts/alloc_runctl_groups.dat
#cat $DATAFILE | while read LINE
while read LINE
REC_ID=`echo $LINE | awk -F: '{print $1}'`
do
RUNCNTL=$REC_ID
if (( $1 == $REC_ID )); then
break
fi
#done < $DATAFILE
done < $DATAFILE
echo $RUNCNTL
|
|
|
04-26-2012, 02:04 AM
|
#14
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,782
|
Quote:
Originally Posted by Kustom42
You need to export the variable inside of the loop not before it.
|
if you use that RUNCNTL only inside the script you do not need to export it.
|
|
|
04-26-2012, 09:04 AM
|
#15
|
LQ Newbie
Registered: Nov 2011
Posts: 15
Original Poster
Rep:
|
you guys rock!!! its working!!!..Thanks
|
|
|
All times are GMT -5. The time now is 11:45 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|