Script does not run properly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script does not run properly
# 1  
Old 09-13-2012
Script does not run properly

Hello everybody

Im learning bash scrpting language and im making a script that read a file line by line and it does a comparison if in a line start with a letter or number and it will delete every ones that start with a letter. But im getting some errors

First of all, this is the script's code

Quote:

#VARIABLES GLOBALES DEL SCRIPT
IP=$1
ARCHIVO=arch.txt

#VARIABLE PARA COMPARAR POR NUMERO AL INICIAR UNA LINEA
NUM0=(0 1 2 3 4 5 6 7 8 9)

cat myarchive.log | grep src="$IP" | grep type=traffic | grep dst_port=80 | grep wan2 | grep -v Youtube | cut -d ',' -f 22 | cut -d '=' -f 2 > $ARCHIVO

while read line;
do
$line
if [ $line -ne $NUM0 ]; then
sed '/${line}/d' $ARCHIVO
fi

done < $ARCHIVO

echo "PROCESO TERMINADO."
The error that im getting is when the process arrive to while. It starts the conditional and never end. It always is running and showing numbers in the command line. I'd like to know how to solve this problem.

PD When i check out the arch.txt i see that the IF works fine, it delete every line that start with a letter. As i explained above, the problem is with while...

Thanks in advantage for any help that you could give me.

Grettings
# 2  
Old 09-13-2012
try this

Code:
awk '/^[0-9]/' file


grep "^[0-9]" file

# 3  
Old 09-13-2012
It doesnt work yet. The trouble is with the loop in the while, the way that i cut out the strings in the ' if ' works fine.

The infinite output is

Quote:
120
121
121
120
120
125
125
125
125
120
120
128
120
126
122
120
and so on...

Thanks in advantage
# 4  
Old 09-13-2012
Please post the input you have and the output you want. Without that, we can't even see how it's going wrong, let alone what or why.

I'm pretty sure you can do what you want with an awk one-liner instead of running sed 9,000 times to process 9,000 lines.
# 5  
Old 09-13-2012
@alienrunes: first you are saving some column from each line in "myarchive.log" into $ARCHIVO (arch.txt), then in the while loop you are doing "sed" for the whole $ARCHIVO for each line in $ARCHIVO...
# 6  
Old 09-20-2012
Hi, sorry for give a answer some late.

I could solve the problem from a different way. I've changed the "while" sentence for the "tr" and "grep" commands.


Quote:
Corona688 Please post the input you have and the output you want. Without that, we can't even see how it's going wrong, let alone what or why.

I'm pretty sure you can do what you want with an awk one-liner instead of running sed 9,000 times to process 9,000 lines.
Well the main idea is to get the TIME spent by an user on internet. So, i pass as parameter the IP address and i get time expressed in seconds, but i need to clean that file from non-numerical character, so i delete any special or alphabetic character from that file. After, i delete the empty lines and add (+) the time 'till to get all seconds. After i convert it in hours:minutes:second. All expressed is the goal of this script. I leave below something i've done.

Code:
 
#SCRIPT GLOBAL VARIABLES
IP=$1
ARCHIVO=file1.txt
ARCHIVO2=file2.txt
ARCHIVO3=file3.txt
ARCHIVO4=file4.txt
TIEMPOTOTAL=file5.txt
SEGUNDOS=$(cat $ARCHIVO4)
TIEMPOF=file6.txt



echo "=============================================================="
echo "ANALIZANDO SOLICITUD DE TRAFICO POR NAVEGACION DE LA IP $IP"
echo "=============================================================="
cat firewallfile.log | grep src="$IP" | grep type=traffic | grep dst_port=80 | grep wan2 | cut -d ',' -f 22 | cut -d '=' -f 2 > $ARCHIVO

echo "=============================================================="
echo "OBTENIENDO INFORMACION DE NAVEGACION DE LA IP $IP"
echo "=============================================================="

cat $ARCHIVO | tr -d '[a-z][A-Z] =;:`"<>,./?!@#$%^&(){}[]' > $ARCHIVO2

echo "=============================================================="
echo "FORMATEANDO INFORMACION SOLICITADA"
echo "=============================================================="

grep . $ARCHIVO2 > $ARCHIVO3


awk '{ s += $1 } END { print s }' $ARCHIVO3 > $ARCHIVO4

echo "=============================================================="
echo "EL TIEMPO TOTAL NAVEGADO ES `cat $ARCHIVO4` SEGUNDOS"
echo "=============================================================="

echo "=============================================================="
echo "CONVIRTIENDO `cat $ARCHIVO4`  EN HORAS:MINUTOS:SEGUNDOS"
echo "=============================================================="


date -d '1970-01-01 '$SEGUNDOS' sec' +"%H:%M:%S" > $TIEMPOTOTAL


#echo - | awk -v "S=$SEGUNDOS" '{printf "%02d:%02d:%02d",S/(60*60),S%(60*60)/60,S%60}' > $TIEMPOTOTAL


echo "=============================================================="
echo "EL TIEMPO TOTAL NAVEGADO ES `cat $TIEMPOTOTAL` "
echo "=============================================================="

in addition with AWK (Commented in the script)

It works well, but when start to convert from seconds to hh:mm:ss , it shows me the following error.

Code:
awk: 1: unexpected character '.'

I've run the command in the console and it works very good. It gives me the time as i want, for instance

Code:
echo - | awk -v S=SECONDS S '{printf "%02d:%02d:%02d",S/(60*60),S%(60*60)/60,S%60}' > $TIEMPOTOTAL

Now, in addition with DATE Command

It show me weird values, if i run it with 3600 seconds, it convert in 01:00:00 as expected. But when it has big values, then show wrong value in the converter, for instance:


Quote:
user@myserver:/home/fortinet_syslog# date -d '1970-01-01 3600 sec' +"%H:%M:%S"
01:00:00
user@myserver:/home/fortinet_syslog# date -d '1970-01-01 15000 sec' +"%H:%M:%S"
04:10:00
user@myserver:/home/fortinet_syslog# date -d '1970-01-01 2356174 sec' +"%H:%M:%S"
06:29:34
user@myserver:/home/fortinet_syslog# date -d '1970-01-01 235617 sec' +"%H:%M:%S"
17:26:57
What can i do in these cases? Because i need to get real values from the converter between seconds to hours:minutes:seconds . If there is a way more easy to do it, just let me know how make it.

Thanks in advantage.

Greetings
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script fails to run properly when run from CRONTAB

Hello all, I'm trying to write a script to gather and send data and it works just fine at the bash command line, but when executing from CRON, it does not run properly. My scripting skills are pretty limited and there's probably a better way, but as I said it works at the command line, but... (12 Replies)
Discussion started by: rusman
12 Replies

2. Shell Programming and Scripting

Shell script not working properly

Hello, i have below shell script to process ftp get from server and create file list afte finish. this shell scipt has 6 parameter input. unfortunately, it is only working to get the file and terminated before creating file list. please help. thanks, #!/bin/ksh ## example usage :... (3 Replies)
Discussion started by: daryatmo
3 Replies

3. Shell Programming and Scripting

script line not working properly

Hi , I am using the below command in script. $_IDLETIME is returning value if i execute the script manually. sar > $_LOCATION/sar.txt _IDLETIME=`tail -2 $_LOCATION/sar.txt | head -1 | tr -s ' ' ' ' | cut -d ' ' -f8 | cut -d '.' -f1`; if But it s not returning any value if it put the... (3 Replies)
Discussion started by: ahamed
3 Replies

4. Shell Programming and Scripting

Shell Script Email not working Properly

Hi GURU's, I'm using a Shell Script to send email's as an attachment. I'm Storing the email address in a table and catching in a variable. MAILLIST=`noarg sqlplus -s $OraUsr << EOF set heading off set feedback off set echo off SELECT email_ids FROM tpemail_table WHERE... (9 Replies)
Discussion started by: karthikraj
9 Replies

5. Homework & Coursework Questions

Script does not execute properly

I am trying to create a Unix script that when ran will provide a veiw of the files of the week that the user inputs, I have this one down. Then I would like them to be able to type in one of the files that is in that week and it displays that file. The script that I have keeps looping the week... (4 Replies)
Discussion started by: Goob
4 Replies

6. Shell Programming and Scripting

Script not running properly when run from Crontab

Hi I am a novice Linux/Perl user and am struggling to overcome what I am sure is a simple problem. I am using a perl program to create a shell script daily containing between 10 and 30 "at -f" commands for the same day. Then I change the file attributes to allow the file to be executed. When... (2 Replies)
Discussion started by: simoncjones
2 Replies

7. Shell Programming and Scripting

cron does not execute script properly

I have a simple script that checks for certain printers and records them to a file. When I run the script manually at the command prompt, it works perfect, but when I run the script via cron, nothing happens. No errors reported, and no records are written out. I'm using Solaris 10. Below is the... (4 Replies)
Discussion started by: lmatlebyane
4 Replies

8. UNIX for Dummies Questions & Answers

Script not running properly

I created a script before which was this :- cd /mike/dataxfer for dir in *; do if ; then ssh -l abc nissan "mkdir -p /mike/web/$dir" scp -r ${dir}/priceops nissan:mike/web/${dir}/ fi done exit 0 This script worked fine for me with no problem. It will... (5 Replies)
Discussion started by: chris1234
5 Replies

9. UNIX for Dummies Questions & Answers

Cron won't run properly

I am new to unix, and this is my 1st post on this board. Looking for some advice about a cron job in my server. I am running a cron task that references a script which runs several other scripts and compiles them into a report and emails it to me. If I run the script manually, I will... (2 Replies)
Discussion started by: Steeler_fan
2 Replies
Login or Register to Ask a Question