scripting headache... loops & variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting scripting headache... loops & variables
# 1  
Old 02-06-2006
scripting headache... loops & variables

Surely there's an easier way to do this, lets see if anyone knows! I am new to scripting so go easy on me!

I have the following script and at the moment it doesn't work and I believe the problem is that I am using a while loop within a while loop. When I run the script using sh -x I can see that it is grepping each $DAACFILE but gets no result, so I think $IP has no value set when running this part of the script, which results in the file, gettrack, containg no data... so the result is that MSISDN.out is empty. Any help much appreciated.

LOGDIR=/usr/local/tibco/adapters/DAAC/bin/archive/
TEMPDIR=/usr/local/tibco/adapters/DAAC/bin/archive/temp/

rm $TEMPDIR/getMSISDN.sh
rm $TEMPDIR/MSISDN.out
touch $TEMPDIR/getMSISDN.sh
chmod +x $TEMPDIR/getMSISDN.sh

echo "Enter the month (e.g Jan) you wish to extract MSISDN's for : "
read MONTH
echo "Enter the number of the day (1-31) you wish to extract MSISDN's for : "
read DAY

DAYLOG=$TEMPDIR"$MONTH$DAY"

#CREATES MSISDN.OUT BY COPYING RELEVANT DAYS LOG FILES TO TEMP LOG DIRECTORY, EXTRACTS IP FROM extract??.EST.txt AND GREPS DAAC LOGS FOR MSISDN BASED ON IP ADDRESS AND TRACKINGID.

echo "DMS transactions by MSISDN" > $TEMPDIR/MSISDN.out
ls $DAYLOG > $TEMPDIR/list_files
# exec < $TEMPDIR/list_files
# while read DAACFILE
cat $TEMPDIR/list_files | while read DAACFILE
do
{
#awk '{print $7}' $TEMPDIR/extract$DAY.EST.txt|while read IP
awk '{print $7}' $TEMPDIR/extract22.EST.txt|while read IP
do
# grep $IP $DAYLOG$DAACFILE | grep tracking > $TEMPDIR/gettrack;
grep $IP $DAYLOG$DAACFILE | grep tracking >> $TEMPDIR/gettrack;
awk '{print$15}' $TEMPDIR/gettrack| sed 's/tracking=/grep "/' | sed "s/zzw\#/\" $DAACFILE \| grep \"MSISDN :\"/" >> $TEMPDIR/getMSISDN.sh;
$TEMPDIR/getMSISDN.sh | awk '{print $1, $2, $3, $4, $5, $6, $12, $13}' >> $TEMPDIR/MSISDN.out
done
}
# rmdir $DAYLOG
done
# 2  
Old 02-07-2006
Next time, please put your code within the code tags.

Try this.

Code:
#! /bin/sh

LOGDIR=/usr/local/tibco/adapters/DAAC/bin/archive/
TEMPDIR=/usr/local/tibco/adapters/DAAC/bin/archive/temp/

> "$TEMPDIR/getMSISDN.sh"

echo "Enter the month (e.g Jan) you wish to extract MSISDN's for : "
read MONTH
echo "Enter the number of the day (1-31) you wish to extract MSISDN's for : "
read DAY

DAYLOG=$TEMPDIR"$MONTH$DAY"

#CREATES MSISDN.OUT BY COPYING RELEVANT DAYS LOG FILES TO TEMP
#LOG DIRECTORY, EXTRACTS IP FROM extract??.EST.txt AND GREPS DAAC 
#LOGS FOR MSISDN BASED ON IP ADDRESS AND TRACKINGID.

echo "DMS transactions by MSISDN" > $TEMPDIR/MSISDN.out

ls $DAYLOG > $TEMPDIR/list_files

while read DAACFILE 
do 
{
   while read line
   do
   IP=$(echo $line | awk '{ print $7 }')

   grep "$IP" "$DAYLOG$DAACFILE" | grep tracking >> $TEMPDIR/gettrack;
   awk '{print $15}' $TEMPDIR/gettrack | sed -e "s_tracking=_grep \"_" -e "s/zzw\#/\" $DAACFILE \| grep \"MSISDN :\"/" >> TEMPDIR/getMSISDN.sh; 
   awk '{print $1, $2, $3, $4, $5, $6, $12, $13}' "TEMPDIR/getMSISDN.sh"  >> $TEMPDIR/MSISDN.out
   done < "$TEMPDIR/extract22.EST.txt"
}
done < "$TEMPDIR/list_files"

# 3  
Old 02-07-2006
Now getting `IP=$' unexpected

Is the code you've given me specific to a shell type or OS ??

Thanks
# 4  
Old 02-07-2006
What is the first line in your script ? Is it something like #! /bin/sh.
# 5  
Old 02-07-2006
yep, I tried /bin/sh, /bin/ksh and /bin/bash none of which worked :-( (all had same error)
# 6  
Old 02-07-2006
I have tried another method, and believe I have it working. I'll post what I've done once I know it's worked.

Thanks for the help.
# 7  
Old 02-07-2006
Replace $(...) with `...`

` are the backticks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Loops & Variable

I am using this code: for e in {1..14} do awk '{gsub(/^.*GGGGGG|TTTTT.*$/,"",$0)} 1' $e.1 > ${e}.2 done However, in the second loop instead of GGGGGG|TTTTT, I should use AAAAAA|CCCCCC. For third loop CCAAAA|CCCCAA, so on and so forth. Is there any way to accomplish this without writing... (20 Replies)
Discussion started by: Xterra
20 Replies

2. Shell Programming and Scripting

Unset variables in shell when it running two different loops

I have a script to start/stop/restart the tomcat application. When we run the script first time i.e stop/start it set all env variables(DISTRIB_ID,NAME,TOMCAT_CFG,....etc),but when we restart the tomcat it is running in the same shell.....I need to set the variables when i restart the tomcat(in the... (1 Reply)
Discussion started by: praveen265
1 Replies

3. Shell Programming and Scripting

Two variables in output file name nested for loops

I am trying to use two nested for loops to process some files and then create a new file using both variables in the output file name. I have several files in this naming style: S1_L3_all_R1.fastq S1_L3_all_R2.fastq S1_L4_all_R1.fastq S1_L4_all_R2.fastq . . S1_L8_all_R1.fastq... (3 Replies)
Discussion started by: aminards
3 Replies

4. Shell Programming and Scripting

while loops and variables under bash

Hi, This is probably going to be very simple but i came across something i can't quite explain. Here is the situation: i have a list of files, which i'd like to process one by one (get the size, make some tests, whatever) and generate some statistics using different variables. Something... (5 Replies)
Discussion started by: m69w
5 Replies

5. Shell Programming and Scripting

For loops with multiple variables

Hi script gurus. I have need to know how to use for loop with multiple variable. Basically lets take for example /etc/passwd file has following entries The above cat command will basically first greps the real users that have email addresses then converts ':' to '+' then using cut... (4 Replies)
Discussion started by: sparcguy
4 Replies

6. Shell Programming and Scripting

Problem in loops in shell scripting

Hi, #!/bin/ksh $v="" for ((i = 1 ; i <= 5 ; i++ )) do v="THerrFile_$i.err"; grep -i "$i:Error" $v >>oraerror_output.txt done My requirement is to dynamically create variable like THerrFile_1.err,THerrFile_2.err etc. where my grep needs... (5 Replies)
Discussion started by: sudhir_83k
5 Replies

7. Shell Programming and Scripting

Nested while loops (ksh scripting)

You can use one while inside another? I made the following script (without really knowing if I can use two while) to get 3 numbers different from each other at random: num1=$(( $RANDOM % 10 )) num2=$num1 while do num2=$(( $RANDOM % 10 )) done num3=$num1 while do while do... (1 Reply)
Discussion started by: ale.dle
1 Replies

8. Shell Programming and Scripting

bash scripting: using multiple 'for loops'??

Hey guys, I'm kinda a noob at scripting. I am trying to create a script that uses multiple for loops with the lsiutility to monitor disk health on a system. The script runs, but it will continually echo an infinite number of LogVolumes when there are only 2 per virtual disk on my server. It's... (2 Replies)
Discussion started by: tank126
2 Replies

9. UNIX for Dummies Questions & Answers

Variables being worked on inside of loops

I have a while read loop that reads values inside of a file and then performs an expr operation on each. Everything works fine, the way it's supposed to but, once the loop is finished, I can not access the variable that I used inside of the loop (but that variable was created outside of the... (7 Replies)
Discussion started by: yongho
7 Replies

10. IP Networking

DHCP client & ipchains headache

Hi all, I upgraded my SUSE 6.1 to SUSE 7.1 and at once the following things won't work anymore: 1) My DHCP client is not able anymore to retrieve my IP address from the @home server. It times out all the time. If I use a fixed IP I can get on the net, so there is no physical problem. 2) I... (3 Replies)
Discussion started by: Micky
3 Replies
Login or Register to Ask a Question