LinuxQuestions.org
Review your favorite Linux distribution.
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 04-25-2012, 02:21 PM   #1
nishanthp82
LQ Newbie
 
Registered: Nov 2011
Posts: 15

Rep: Reputation: Disabled
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
 
Old 04-25-2012, 02:25 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
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.
 
Old 04-25-2012, 02:29 PM   #3
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
Add at the beginning of your script
Code:
RUNCNTL=""
export RUNCNTL
 
Old 04-25-2012, 02:30 PM   #4
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
You need to export the variable inside of the loop not before it.
 
Old 04-25-2012, 02:57 PM   #5
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
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
 
Old 04-25-2012, 04:07 PM   #6
nishanthp82
LQ Newbie
 
Registered: Nov 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-25-2012, 04:26 PM   #7
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
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
 
Old 04-25-2012, 08:49 PM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by Kustom42 View Post
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
 
Old 04-25-2012, 08:57 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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
 
Old 04-25-2012, 10:20 PM   #10
nishanthp82
LQ Newbie
 
Registered: Nov 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
still not working guys!
 
Old 04-25-2012, 10:47 PM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Then it would be helpful to see your latest script, the input file, the command you used to run the script and the output.
 
Old 04-25-2012, 11:11 PM   #12
nishanthp82
LQ Newbie
 
Registered: Nov 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
#!/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
 
Old 04-26-2012, 01:53 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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
 
Old 04-26-2012, 02:04 AM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by Kustom42 View Post
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.
 
Old 04-26-2012, 09:04 AM   #15
nishanthp82
LQ Newbie
 
Registered: Nov 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
you guys rock!!! its working!!!..Thanks
 
  


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
How to break down a grep command dimeetrees Linux - Newbie 9 04-09-2011 04:17 PM
saving a page using wget+command -O not found+each time i'v to break manually Kakarot_Rathish Programming 5 04-13-2010 04:06 AM
how to break a kernel 2.6.28.13 of ubuntu 9.04 using command line begawa Linux - Newbie 3 07-09-2009 01:59 AM
Page break command gubak Linux - Newbie 1 03-13-2007 04:27 AM
Telnet break command (RedHat) rainman4_8 Linux - Software 1 03-22-2006 07:25 AM

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

All times are GMT -5. The time now is 09:32 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