LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-12-2005, 09:49 AM   #1
David_Moses
LQ Newbie
 
Registered: Jul 2004
Location: uk
Distribution: Ubuntu
Posts: 27

Rep: Reputation: 15
a little help with a script


Hi guys

I just need some help with a really really simple script.


# Wireless script for scanning AP's continusly
while x=[0]; do

time iwlist ath0 scanning >>scan_op.txt

done

#EOF

this script just conntinuly scans local AP's, the problem is i need to know how long each scan takes. but the time command only outputs to the console and not the file.

I would like to add the output from time to a seperate file. Or even to scan_op.txt

Also it is easy to say only runn the scan 100 times? how would i decrement the value of X ever time.

Any help would be greatly appreciated.

DM
 
Old 04-12-2005, 10:54 AM   #2
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Re: a little help with a script

The loop is not too hard:-
Code:
x=0
max=100
while [ $x -lt $max ]; do
time iwlist ath0 scanning >>scan_op.txt
let x=x+1
done
But as for redirecting the output from time to a file, I can't seem to do it
 
Old 04-12-2005, 11:08 AM   #3
David_Moses
LQ Newbie
 
Registered: Jul 2004
Location: uk
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: 15
Unhappy :(

Hi there...

firstly thanks for your help!!!

That code just seems to run the loop continusly as well. I understand the concept thought.

this is my file exactly....


#!sbin/bash
# Wireless script for scanning AP's continusly

x=0
max=1

while [ $x -lt $max ] ; do

time iwlist ath0 scanning >>scan_op.txt



done

#EOF

ideas??
 
Old 04-12-2005, 11:44 AM   #4
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Yes, you left out the
Code:
let x=x+1
so, of course, x never gets incremented.

Easily done

Last edited by ahh; 04-12-2005 at 11:46 AM.
 
Old 04-12-2005, 01:59 PM   #5
David_Moses
LQ Newbie
 
Registered: Jul 2004
Location: uk
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: 15
Thumbs up

i'm stupid, sorry.


have you had any thoughts about the time problem.

What i'm actually trying to do is run say 100 scans and take the average db reading from each AP and the average time it takes to scan the whole range of freqs.

I can use grep and cut commands to get the following info the the file, but i need a way of appling a time stamp to each scan.

thanks for your help
Cheers

DM
 
Old 04-12-2005, 02:04 PM   #6
David_Moses
LQ Newbie
 
Registered: Jul 2004
Location: uk
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: 15
what about using the CAT command to pipe the time stamp to the end of each scan.

would you have any idea how to do this..
 
Old 04-12-2005, 02:20 PM   #7
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
I played with the "time" command, but I can't find a way to get it to write to a file. I tried "tee", redirection, pipes etc. but no luck. Weird.

The man page for time says that the GNU time v.1.7 has this ability, so perhaps you should investigate that?
 
Old 04-12-2005, 02:55 PM   #8
zeos
Member
 
Registered: Aug 2003
Posts: 150

Rep: Reputation: 15
you could set a variable inside your loop and echo the variable out....

MYTIME=`date +%r`
export MYTIME

echo $MYTIME >> myfile.txt


EDIT**
Doh ...nevermind ...I just realized what you're trying to do ...that'll teach me to post without fully reading ...sorry...

Last edited by zeos; 04-12-2005 at 02:56 PM.
 
Old 04-12-2005, 02:57 PM   #9
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
I tried this on the command line with a separate program of my own, and it appeared to work:
Code:
( time program_name program_arguments ) > output_file
The parentheses cause the shell to execute the command in a subshell, and the redirection causes the output of the entire subshell process to be written to a file.

It is odd that my version of time (1.7) has a man page that says the output is sent to standard output, but normal redirection does not seem to affect it.
 
Old 04-12-2005, 05:07 PM   #10
David_Moses
LQ Newbie
 
Registered: Jul 2004
Location: uk
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: 15
Unhappy

That for all your segestions guys.


Its still not working for. I'm not even bothered if the time stamp goes to a seperate file, it just needs to go somewhere other than the screen!!

As a last resort, is there any way to add a break between the scans. like using CAT or something to add the word new scan. I'm trying to get the average signal sevel from say 100 scans. So i need a way of telling them apart.

Again thank you all for your kind help and support.
This is one of the things that makes linux so great. The willingness for people to share their knowledge

Dm
 
Old 04-12-2005, 05:13 PM   #11
David_Moses
LQ Newbie
 
Registered: Jul 2004
Location: uk
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: 15
how do i check what version of time i am running?

..... got the gnu 1.7 version of time.

This is kind of doing what i need. but it adds the total time that it takes to run all scans. I need the time it take to run indivdual scans......

there must be a way to do it. I just don't know how to find it.

.

Last edited by David_Moses; 04-12-2005 at 05:42 PM.
 
Old 04-13-2005, 01:04 AM   #12
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Ugh... I should have realized this earlier.

Here's the crux of the problem: There are two distinct time commands (at least). A stand-alone executable, and (for bash) a shell built-in command.

When you specify to execute "time something", it defaults to the shell built-in, and as far as I can tell, there's no way to pass that version any information (such as "send your output to a file").

If you invoke the command with an absolute/relative path to the stand-alone executable, then you can give it arguments. For instance, on my system, the time program is installed in /usr/bin:
Code:
/usr/bin/time du -hs ${HOME}
Anyway, you mentioned you have version 1.7 (same as on mine), and there's a -o option to direct where to send the output. In fact, you have a lot of flexibility in what information it reports; all of it documented in the man page of course.

So I would suggest a script like this:
Code:
#!/bin/bash

MAX_CHECKS=100

current_check=0
elapsed_time=0
while [ ${current_check} -lt ${MAX_CHECKS} ]
do
  /usr/bin/time -f "%e" -o /tmp/current_time_used iwlist ath0 scanning >>scan_op.txt
  current_time=$( cat /tmp/current_time_used )
  let elapsed_time=elapsed_time+current_time
  let current_check=current_check+1
done

let elapsed_time=elapsed_time/${MAX_CHECKS}
echo "average time per check: ${elapsed_time}"
Something to note: I haven't even attempted to run that script. There may be (probably are) problems with it, but they'll probably be easy to iron out. Also, the shell does integer division, meaning your average time at the end will be reported as whole seconds (not fractional). If that's not what you want, then you'll need to use something like bc to get greater accuracy.

Last edited by Dark_Helmet; 04-13-2005 at 01:05 AM.
 
Old 04-13-2005, 01:39 PM   #13
David_Moses
LQ Newbie
 
Registered: Jul 2004
Location: uk
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: 15
thank for all of your help, that really great.

You will have to bare with me as i'm certainly no programmer.

I understand the concept of thi program....

i get this error when running it

# sh time_scan
time_scan: line 13: let: 2.66: syntax error in expression (error token is ".66")
time_scan: line 13: let: 2.68: syntax error in expression (error token is ".68")
time_scan: line 13: let: 2.68: syntax error in expression (error token is ".68")
average time per check: 0


Now i'm thinking that the program does not understand the decimal points system. Should i be using bc instead fo let?

The scaning part of this program works.

Another thing that i though of is that my GNU1.7 time only seems to run from the dirrectory that i compiled it in. For example if i try and run the time program from the standard /# with the -o option i get the message invalid option "-o". to run GNU time i need to type ./time from the dirrectory that it compiled into. Im not sure if this is probelm or not.

Thanks for all of your help

Dm
 
Old 04-13-2005, 02:48 PM   #14
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Quote:
time_scan: line 13: let: 2.66: syntax error in expression (error token is ".66")
time_scan: line 13: let: 2.68: syntax error in expression (error token is ".68")
time_scan: line 13: let: 2.68: syntax error in expression (error token is ".68")
...
Now i'm thinking that the program does not understand the decimal points system. Should i be using bc instead fo let?
Yeah, that's what the problem is. Just like the division I mentioned later, the shell expects integer expressions. So it freaks out about the decimal point. Using bc will fix this. Something like:
Code:
elapsed_time=$( echo "${elapsed_time} + ${current_time}" | bc -l )

...

elapsed_time=$( echo "${elapsed_time} / ${MAX_CHECKS}" | bc -l )
Quote:
Another thing that i though of is that my GNU1.7 time only seems to run from the dirrectory that i compiled it in.
Yup. That's what I was pointing at earlier. If you don't give a specific path to the executable, then your shell will execute its internal version. If you compiled it in /home/dmoses/work/compile, then you can execute it from any directory by issuing /home/dmoses/work/compile/time. Use that same path in the script.
 
Old 04-14-2005, 10:35 AM   #15
David_Moses
LQ Newbie
 
Registered: Jul 2004
Location: uk
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: 15
Thumbs up



THAT WORKS!!!!!

thank you, thank you.


Just one more quick question. That gives the average time for the total number of scans.

Is it possible to find the file where these are stored. It seemed to be /tmp/Current_time_used

The file would contain all of time taken for each scan

does the /tmp file get removed when the program finishes running? I want to keep the file so i can say...

scan 1 took 2.67 sec
scan 2 took 2.77 sec
...
...

...
.
DM
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 10:23 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
linux 9 and java script error - premature end of script header sibil Linux - Newbie 0 01-06-2004 04:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:41 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
Open Source Consulting | Domain Registration