Visit Our UNIX and Linux User Community


Variable usage issue


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Variable usage issue
# 1  
Old 08-11-2013
Variable usage issue

Hi,

I need a help in setting scope of the variable. I want to use the below logic right before the "break" statement
Code:
if [ ${trailer} -eq $eof ]; then
echo $header
echo $trailer
fi

But due the scope of the variable it is causing issues. I tried using "export" statement. But it changes the output completely
Code:
#!/bin/sh
file="drop/test1"
filename="2154/drop/abc.emi"
eof=`wc -l $filename | awk '{print $1;}'`
blk_val=99999
while read j
do
prev=$j
prev1=$j
blk_flag=0
loop_count=0
blk_size=0
while read j
do
 if [ ${j} -gt ${prev} ]; then
 next=$j
 blk_size=`expr $next - $prev + $blk_size + 1 `
	 if [ ${blk_size} -ge ${blk_val} ]; then
		 if [ ${blk_flag} -eq 1 ]; then
			    if [ ${loop_count} -eq 0 ]; then
				 header=$prev1
				 trailer=$next
				 prev=`expr $next + 1`
				 echo h0=$header
				 echo t0=$trailer
				 blk_size=0
				 blk_flag=0
				 loop_count=1
				 prev1=`expr $trailer + 1`
			    else 
				 prev1=`expr $trailer + 1`
				 trailer=$next
				 prev=`expr $next + 1`
				 header=$prev1
				 echo h1=$header
				 echo t1=$trailer
				 blk_size=0
				 blk_flag=0
			    fi	 
		 else 
			 header=$prev
			 trailer=$next
			 prev=`expr $next + 1`
			 blk_size=0
			 blk_flag=0
			 echo h2=$header
			 echo t2=$trailer 	        
		 fi	 
	 else
		 prev=`expr $next + 1`
		 blk_flag=1
	 fi	

 fi
done < $file      
break
done < $file

I/P file is a big file like:
Code:
1
6
7
14
15
20
21
96
97
115
.
.
.
1322610
1322620
1322621
1322654
1322655
1322657
1322658
1322661
1322662
1322664

Current O/p:
Code:
h0=1
t0=100008
h1=100009
t1=201120
h1=201121
t1=301524
h1=301525
t1=401938
h1=401939
t1=503160
h1=503161
t1=604185
h1=604186
t1=704649
h1=704650
t1=806089
h1=806090
t1=909204
h1=909205
t1=1009463
h1=1009464
t1=1109665
h1=1109666
t1=1210242
h1=1210243
t1=1311035


Expected O/P:
h0=1
t0=100008
h1=100009
t1=201120
h1=201121
t1=301524
h1=301525
t1=401938
h1=401939
t1=503160
h1=503161
t1=604185
h1=604186
t1=704649
h1=704650
t1=806089
h1=806090
t1=909204
h1=909205
t1=1009463
h1=1009464
t1=1109665
h1=1109666
t1=1210242
h1=1210243
t1=1311035
h1=1311036
t1=1322664

Can someone help

Last edited by Scott; 08-11-2013 at 07:07 PM.. Reason: Returning banned user - Thread closed.
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #213
Difficulty: Medium
OSPF-TE is an extension to OSPF extending the expressivity to allow for traffic engineering and use on non-IP networks.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Usage of # in assignment of variable

guys, i would like to know what does the below does. tr=`echo $bfile | cut -d"." -f4` tr=${tr#TR} i tried with assigning a value and executed second line. but after that also value of tr remains same. thanks in advance .. (1 Reply)
Discussion started by: expert
1 Replies

2. UNIX for Dummies Questions & Answers

Variable usage

Hi , I am using a simple loop:- for PID in `ps -auxww| grep -v grep | awk '{print $2}'` do echo "Found : $PID : Killing..." kill -9 ${PID} done. I want to know what will be the difference for killing the PID by using :- ${PID} , $PID, `$PID` and '$PID'. I want to... (4 Replies)
Discussion started by: Raj999
4 Replies

3. Shell Programming and Scripting

Variable value substitution issue with awk command issue

Hi All, I am using the below script which has awk command, but it is not returing the expected result. can some pls help me to correct the command. The below script sample.ksh should give the result if the value of last 4 digits in the variable NM matches with the variable value DAT. The... (7 Replies)
Discussion started by: G.K.K
7 Replies

4. Shell Programming and Scripting

sed usage with Variable

Gurus, I am trying to display a match (single character) from beginning of the line in a file using a variable. I tried using sed ... not sure where am doing it wrong... sed -n "/^\$variable/p" FileName.shor sed -n "/^\${variable}/p" FileName.shBoth of the above are not working.....Thanks... (4 Replies)
Discussion started by: Kevin Tivoli
4 Replies

5. Shell Programming and Scripting

Array usage issue with AWk

Hi friends, I m trying to write small script in awk alone. And I have tried below logic for one of my automation - taking the first column of the file in array and will read in for loop for each time when i grep the array value in the same file I should get complete output whichever is matching... (3 Replies)
Discussion started by: Shahul
3 Replies

6. Shell Programming and Scripting

Confused with the usage of one variable usage

Hi All I am not able to understand the usage of d# in the below variable declaration. FILE_LOC contains the directory path And also help me to know about what will be saved in the variable j. Thanks!!! j=${d#${FILE_LOC}/} (2 Replies)
Discussion started by: mohanm
2 Replies

7. UNIX Desktop Questions & Answers

Issue with disk space usage

Issue with disk space usage I have the following line in my "df -h" output: Filesystem Size Used Avail Capacity Mounted on /dev/ad4s1a 496M 495M -39M 109% / What is the issue with having 9% excess utilisation? How can I find out what this partition is... (2 Replies)
Discussion started by: figaro
2 Replies

8. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

9. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

10. UNIX for Advanced & Expert Users

Usage of $ as variable name

Hi I have to use a Environment variable and that variable has $ prefixed to its name like, $var=/home/source/test/ i need to use the variable as i have show above. :confused: Help requested..... Thanks in advance... (6 Replies)
Discussion started by: shreekrishnagd
6 Replies

Featured Tech Videos