LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 03: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, 03:25 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

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

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

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

Rep: Reputation: 140Reputation: 140
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, 05: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, 05:26 PM   #7
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 414Reputation: 414Reputation: 414Reputation: 414Reputation: 414
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, 09:49 PM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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, 09:57 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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, 11:20 PM   #10
nishanthp82
LQ Newbie
 
Registered: Nov 2011
Posts: 15

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

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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-26-2012, 12:11 AM   #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, 02:53 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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, 03:04 AM   #14
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,130

Rep: Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272
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, 10:04 AM   #15
nishanthp82
LQ Newbie
 
Registered: Nov 2011
Posts: 15

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 05: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 05: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 02:59 AM
Page break command gubak Linux - Newbie 1 03-13-2007 05:27 AM
Telnet break command (RedHat) rainman4_8 Linux - Software 1 03-22-2006 08:25 AM


All times are GMT -5. The time now is 02:11 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration