Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

READ line by line a file 1

Status
Not open for further replies.

aoroca

IS-IT--Management
Oct 3, 2003
39
CO
How I can create a function that read line by line a file, and this data can be included in other script and performed activity, then continue with 2nd line.


FILE 1
192.168.23.4 Hola
198.65.45.6 Mundo

FILE 2 Script

get file 1 column 1 = 192.168.23.4
execute something with 192.168.23.4 and execute
echo " ALL file 1"

then ....

get file 2 column 1 = 192.168.23.4
execute something with 192.168.23.4 and execute
echo " ALL file 2"
 
I need get in a temporal VARIABLE data by data and execute something when i read line by line, then performe something with script
 
This might give you a start

for the info in file f1
--------------
192.168.23.4 Hola
198.65.45.6 Mundo
--------------

This script
--------------------------------------------
typeset -i cnt=0

while read LINE
do
(( cnt = 1 ))
for FIELD in ${LINE}
do
echo "VALUE in field #${cnt} is -- ${FIELD}"
(( cnt = cnt + 1 ))
done
done <f1
--------------------------------------------
Should produce the following results
--------------------------------------------
VALUE in field #1 is -- 192.168.23.4
VALUE in field #2 is -- Hola
VALUE in field #1 is -- 198.65.45.6
VALUE in field #2 is -- Mundo

JRjr[morning]
 
Try something like this in sh or ksh:
Code:
i=0 export i
while read ip buf
do
  i=`expr $i + 1`
  yourCommand $ip
  echo &quot; ALL file $i ($ip)&quot;
done<&quot;/path/to/FILE 1&quot;

Hope This Help
PH.
 
PHV thanks a lot. I vahe this info

cat ip_add.txt
192.168.28.2 , VoiceMail
192.168.32.3 , Avisor Internet SMS Comtor
172.28.3.10 , Avisor Operadora Teledatos
172.28.3.11 , Avisor Operadora Teledatos
10.2.78.36 , PC Alex

And I need get 1st field (IP add) SUCCESSFUL, and
get all line IPADD + INFO in 2nd variable... how i can do it?
 
#!/bin/ksh

while IFS=',' read ip rest
do
v1=&quot;${ip}&quot;
v2=&quot;${ip}${rest}&quot;

echo &quot;v1->[${v1}] v2->[${v2}]&quot;
done < ao.txt


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
vgersh99

How I can integrate your script with PHV script in order to read line by line and get a loop
 
Try something like this in sh or ksh:
Code:
i=0 export i
while IFS=',' read ip buf
do
  i=`expr $i + 1`
  yourCommand $ip
  echo &quot; ALL file $i ($ip , $buf)&quot;
  v1=$ip; v2=&quot;$ip , $buf&quot;
  doWhatYouWant $v1 &quot;$v2&quot;
done<&quot;/path/to/FILE 1&quot;

Hope This Help
PH.
 
PHV Ok It looks good, thanks a lot, but i have one problem in myscript with some variable (ARRIBA=1), is it possible you can check and tellme why dont works 2nd part of my script when IP add is down and get UP.

Thank you

#''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''#

path=$HOME/Alex
path2=$HOME/Alex/ip_add.txt
date=`date -u`
arriba=1 export arriba

while [ $arriba ]
do

i=0 export i
while IFS=',' read ip buf
do
i=`expr $i + 1`
echo &quot; ALL file $i ($ip , $buf)&quot;
SERVER=$ip
SERVINF=&quot;$ip $buf&quot;
arriba=`expr $arriba`
a=`netstat | grep smpp | grep $SERVER | grep ESTABLISHED`

if [ $arriba -eq 1 ]
then
if [ -z &quot;$a&quot; ]
then
MSG=&quot;SMSC::SMPP '($SERVINF)' is DOWN&quot;
for MOBILE in `cat $path/mobiles.txt`
do
print &quot;submit\n$MOBILE\n\n5710000001\n\n\n\n\n1\n\n\n\n\n\n\n$MSG\ny\nexitn\&quot; | sms_operator > /dev/null 2>&1
sleep 1
done
echo &quot;`date` $MSG&quot; >> $path/NetLogChk
arriba=0
if [ $arriba -eq 0 ]
then
if [ -n $a ]
then
MSGS=&quot;SMSC::SMPP '($SERVINF)' is UP&quot;
for MOBILE in `cat $path/mobiles.txt`
do
print &quot;submit\n$MOBILE\n\n5710000001\n\n\n\n\n1\n\n\n\n\n\n\n$MSGS\ny\nexit\n
\&quot; | sms_operator > /dev/null 2>&1
sleep 1
done
echo &quot;`date` $MSGS&quot; >> $path/NetLogChk
arriba=1
fi
fi
sleep 1
done < $path2
done

----------------------------------------------------------
 
1st remark (without any understanding of what this script is supposed to do), they are 2 missing
Code:
 fi
.
I guess they should be inserted here:
Code:
        echo &quot;`date` $MSG&quot; >> $path/NetLogChk
        arriba=0
fi
Code:
fi
Code:
    if [ $arriba -eq 0 ]

Hope This Help
PH.
 
I have some problems. I use KSH and have problems with 2 variable, why:

if [ -z $a ]

if [ -n $a ]

How I can configure these 2 lines:

May be $a must be &quot;$a&quot;,

a => some characteres

and I need know when $a is different to 0 and equal to 0 in long of chars, in order to check and perform the activities
 
As the variable a may be empty or have some embedded space, I agree the correct coding should be:
Code:
[ -z &quot;$a&quot; ] && echo empty
[ -n &quot;$a&quot; ] && echo non empty
to avoid side effect or even syntax error for the test builtin function.
Another syntax problem I can see is the escaped quote at the end of the 2 strings you print to sms_operator.
Finally, I think that the logic flow of the arriba is broken as is it the same variable for all your ip add.
IMHO you should use arrays, something like this:
Code:
#!/bin/ksh
path=$HOME/Alex
# Load the ip and inf arrays by reading ip_add.txt
eval $(awk -F',' '{
printf &quot;ip[%d]=\&quot;%s\&quot;;inf[%d]=\&quot;%s\&quot;\n&quot;,NR,$1,NR,$0
}' $path/ip_add.txt
# Preload arriba array with value 2 (unseen)
type -i nb=${#ip[*]} i=0
while [ $i -lt $nb ]; do
 i=$((1+i)); arriba[$i]=2
done
# Forever loop
while :; do
 # For each ip add loop
 i=0; while [ $i -lt $nb ]; do
  i=$((1+i)); SERVER=&quot;${ip[$i]}&quot;; SERVINF=&quot;${inf[$i]}&quot;
  echo &quot;ALL file $i ($SERVER,$SERVINF)&quot;
  a=`netstat | grep smpp | grep $SERVER | grep ESTABLISHED`
  t=${arriba[$i]}; MSG=&quot;&quot;
  if [ -z &quot;$a&quot; ]; then
   if [ $t -eq 1 -o $t -eq 2 ]; then
    t=0; MSG=&quot;SMSC::SMPP '($SERVINF)' is DOWN&quot;
   fi
  else
   if [ $t -eq 0 -o $t -eq 2 ]; then
    t=1; MSG=&quot;SMSC::SMPP '($SERVINF)' is UP&quot;
   fi
  fi
  if [ -n &quot;$MSG&quot; ]; then # State change
   for MOBILE in $(<$path/mobiles.txt); do
    print
YourSubmitStringHere
Code:
 |
     sms_operator >/dev/null 2>&1
    sleep 1
   done
   echo &quot;`date` $MSG&quot; >>$path/NetLogChk
   arriba[$i]=$t
  fi
  sleep 1
  done
done
With this draft you have to kill and restart the script if the ip_add.txt file changes.

Hope This Help
PH.
 
When I run script, i can see an error at line 4

[smsadm@smsbog]$sh -x data3
+ alias SMOperator=sms_operator
+ path1=/usr/EBS/ADCSoftware/smsadm/Alex
data3[4]: 0403-057 Syntax error at line 4 : `(' is not matched.
 
Sorry for the typo:
Code:
}' $path/ip_add.txt)
                   ^
 
aoroca, please let the members know if your problem is solved, and if not with a response here, how you solved it.
 
PHV, i have other issues, please review this info. Thanks a lot.

[smsadm@smsbog]$sh -x data3
+ alias SMOperator=sms_operator
+ path1=/usr/EBS/ADCSoftware/smsadm/Alex
+ awk -F, {
printf &quot;ip[%d]=\&quot;%s\&quot;;inf[%d]=\&quot;%s\&quot;\n&quot;,NR,$1,NR,$0
} /usr/EBS/ADCSoftware/smsadm/Alex/ip_add.txt
+ eval ip[1]=&quot;192.168.32.3 &quot;;inf[1]=&quot;192.168.32.3 , Avisor Internet SMS Comtor&quot; ip[2]=&quot;172.28.3
.10 &quot;;inf[2]=&quot;172.28.3.10 , Avisor Operadora Teledatos&quot; ip[3]=&quot;172.28.3.11 &quot;;inf[3]=&quot;172.28.3.1
1 , Avisor Operadora Teledatos&quot; ip[4]=&quot;10.2.78.36 &quot;;inf[4]=&quot;10.2.78.36 , PC PRUEBAS&quot;
+ ip[1]=192.168.32.3
+ inf[1]=192.168.32.3 , Avisor Internet SMS Comtor ip[2]=172.28.3.10
+ inf[2]=172.28.3.10 , Avisor Operadora Teledatos ip[3]=172.28.3.11
+ inf[3]=172.28.3.11 , Avisor Operadora Teledatos ip[4]=10.2.78.36
+ inf[4]=10.2.78.36 , PC PRUEBAS
+ whence -v -i nb=4 i=0
data3[7]: whence: 0403-010 A specified flag is not valid for this command.
+ [ -lt ]
+ data3[10]: 1+i: 0403-009 The specified number is not valid for this command.
 
Are you sure you use ksh as I suggested in previous post?
 
Yes, I am working with KSH over AIX.
#!/bin/ksh
 
Soorry for the typo.
Replace the following line:
Code:
type -i nb=${#ip[*]} i=0
by this:
Code:
typeset -i nb=${#ip[*]} i=0


Hope This Help
PH.
 
I change the script in typo's and add new info

print &quot;submit\n$MOBILE\n\n5710000001\n\n\n\n\n\n\n\n\n\n\n\n$MSG\ny\nexit\n\&quot;|
sms_operator >/dev/null 2>&1


but have problems with 35 line. why?

---------------------------------------------

[smsadm@smsbog]$vi data3
&quot;data3&quot; 41 lines, 1074 characters
#!/bin/ksh
path1=$HOME/Alex
# Load the ip and inf arrays by reading ip_add.txt
eval $(awk -F',' '{
printf &quot;ip[%d]=\&quot;%s\&quot;;inf[%d]=\&quot;%s\&quot;\n&quot;,NR,$1,NR,$0
}' $path1/ip_add.txt)
# Preload arriba array with value 2 (unseen)
typeset -i nb=${#ip[*]} i=0
while [ $i -lt $nb ]; do
i=$((1+i)); arriba[$i]=2
done
# Forever loop
while :; do
# For each ip add loop
i=0; while [ $i -lt $nb ]; do
i=$((1+i)); SERVER=&quot;${ip[$i]}&quot;; SERVINF=&quot;${inf[$i]}&quot;
echo &quot;($SERVER,$SERVINF)&quot;
a=`netstat | grep smpp | grep $SERVER | grep ESTABLISHED`
t=${arriba[$i]}; MSG=&quot;&quot;
if [ -z &quot;$a&quot; ]; then
if [ $t -eq 1 -o $t -eq 2 ]; then
t=0; MSG=&quot;SMSC::SMPP '($SERVINF)' is DOWN&quot;
fi
else
if [ $t -eq 0 -o $t -eq 2 ]; then
t=1; MSG=&quot;SMSC::SMPP '($SERVINF)' is UP&quot;
fi
fi
if [ -n &quot;$MSG&quot; ]; then # State change
for MOBILE in $(<$path1/mobiles.txt); do
print &quot;submit\n$MOBILE\n\n5710000001\n\n\n\n\n1\n\n\n\n\n\n\n$MSG\ny\nexit\n\&quot;|
sms_operator >/dev/null 2>&1
sleep 1
done
echo &quot;`date` $MSG&quot; >>$path1/NetLogChk
arriba[$i]=$t
fi
sleep 1
done
done


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top