A Question Regarding Timers and Output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A Question Regarding Timers and Output
# 8  
Old 04-04-2012
Ow, if there is Solaris you'll probably want to use the utilities that are in /usr/xpg4/bin instead of the regular ones and try the script with this shell:
Code:
/usr/xpg4/bin/sh

Which is supposed to be a POSIX compliant shell as opposed to /bin/sh
# 9  
Old 04-04-2012
If your happy with times down to the second why not use the bash SECONDS env var:

Code:
START=$SECONDS
echo Do some work here.;echo This is what we did here.
echo "Work took $((SECONDS-START)) secs"

# 10  
Old 04-05-2012
$SECONDS also works in ksh and the Posix Shell.
It's the number of seconds since the Shell started.
# 11  
Old 04-05-2012
I don't know about POSIX:

Code:
$ bash
$ echo $SECONDS
7
$ ksh
$ echo $SECONDS
5.652
$ dash
$ echo $SECONDS

$

# 12  
Old 04-05-2012
@Scrutinizer
I see what you mean. ksh93 is giving time with decimal places, whereas older ksh gives whole seconds.
For consistency in a portable script we'd have to round the given figure (or just throw away the decimal places).

Code:
echo $SECONDS|cut -f1 -d'.'

The script should also test the first value of $SECONDS to make sure that the Shell has provided a value and exit if the Shell does not have the value (e.g. for original Bourne Shell) recommend that the script is run with a modern Shell.

Are there computers with just dash ? Does dash have any other useful variable?
# 13  
Old 04-05-2012
yes, but also dash, which is a no-thrills POSIX compliant shell, does not offer $SECONDS, and I cannot find it in the POSIX specifications either. So I think it is a bash/ksh thing and not a POSIX shell thing. So I think this will maybe also not be present in /usr/xpg4/bin/sh and if it is then it should be considered an extension.


--
Sidenote: The decimal places to $SECONDS are available in ksh93, but I don't think we need to cut off the decimal places, since ksh93 can do full floating point arithmetic, so all it does is give us some extra precision, no?

Last edited by Scrutinizer; 04-05-2012 at 09:32 AM..
# 14  
Old 04-05-2012
Looks like another mistake in the Posix specification (even though all the Posix-compliant Shells I have do set $SECONDS).

The Posix specification lists $SECONDS as if it is a reserved variable but then does not describe the variable later on the page.
Environment Variables
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

INPUT/OUTPUT question

Hi All, Is it wrong to do something like this: ssh -T $PROXY_USER@$PROXY_SERVER < script.txt > ssh_output.log I ran and it works fine and does what I need. I wanted to pass a set of commands to the ssh session and store an output in the log file. Thanks (4 Replies)
Discussion started by: rdogadin
4 Replies

2. Programming

pthread_mutex_timedlock and Timers option

in its man-page pthread_mutex_timedlock documents that the absolute timeout parameter should be based on the CLOCK_REALTIME clock or in the system clock. All this depending on whether the 'Timers option' is supported or not. What do they mean by 'Timers option'? How could I tell for sure what... (8 Replies)
Discussion started by: ramestica
8 Replies

3. Shell Programming and Scripting

Command Output Question

Hi everyone-- I'm new to these forums and shell scripting, and I'm trying to write a script that checks if a particular ip is pingable My idea was to check if the output of the command ping <some ip> -c 1 -w 1 had the string: "1 packet transmitted, 1 received"How would I go about doing this?... (2 Replies)
Discussion started by: Prodiga1
2 Replies

4. Solaris

Question on kstat output

I'm having a little trouble understanding the output I'm seeing from kstat for the "net" class. I'm seeing a lot of entries with "mac" as the name, like this. module: bge instance: 3 name: mac class: net ... (3 Replies)
Discussion started by: Midcain
3 Replies

5. Programming

Doubts about timers in linux kernel

Hi , I am trying to learn timers in linux kernel. I am trying to write a program where I can configure a timer to tick in every 5 seconds and a function should thus exicute in every five seconds. I tried one program with the help of linux/timer.h headerfile but I couldnt get the... (4 Replies)
Discussion started by: iamjayanth
4 Replies

6. Shell Programming and Scripting

Question about output to file

Hi, I am try to setup a FOR loop script to find out all the existing linux workstations in the network w/ ip address, hostname and linux version. I created a basic FOR loop script: for i in $(seq 1 254) do echo 10.72.169.$i >> result ssh -o ConnectTimeout=3 root@10.72.169.$i "hostname"... (1 Reply)
Discussion started by: beeloo
1 Replies

7. Shell Programming and Scripting

display and output...question

Hi, I have a small question about the value cannot display correctly: MSG=log fruit=apple print "No $fruit in the store" > "$MSG/fruit_message.txt" output: No $fruit in the store should be: No apple in the store AND $MSG/fruit_message.txt ----------> cannot find the... (5 Replies)
Discussion started by: happyv
5 Replies

8. UNIX for Dummies Questions & Answers

Question on prtdiag output ...

Hello all , This is the output of my prtdiag command ...The speed of each of the CPUs is listed below (1281 MHz ) ..That's fine ..I'm confused about the (System clock frequency: 183 MHZ ) ..What is the difference between System Clock freq and CPU freq ...THanks.. System Configuration: Sun... (5 Replies)
Discussion started by: luft
5 Replies

9. Programming

A question about output.

I am learning output the data to a file using "ofstream". I need to read data from a file and output the result to the other file in 2 different ways. To do that I have to provoke two functions, and they have to output the result to the same text file. My problem is: they are correct on the screen... (2 Replies)
Discussion started by: HOUSCOUS
2 Replies

10. Programming

unix and timers

Hello there.. I need to know when i start indipendant timers how can i recognize which of the active timers have made timeout alarm. The number of started timers depends on the running app I'd appreciate your help Thanks in advance (0 Replies)
Discussion started by: nightcat
0 Replies
Login or Register to Ask a Question