POSIX compliance...


 
Thread Tools Search this Thread
Operating Systems OS X (Apple) POSIX compliance...
# 8  
Old 07-09-2016
Quote:
Originally Posted by Don Cragun
[..]
Code:
n=0
while [ $((++n <= 100)) -eq 1 ]
do	echo "process n=$n"
done

Support for pre-/postfix operators like in ++n is not a requirement in the POSIX specification.
Quote:
The arithmetic expression shall be processed according to the rules given in Arithmetic Precision and Operations, with the following exceptions:

[..]
The sizeof() operator and the prefix and postfix "++" and "--" operators are not required.
Shell Command Language:Arithmetic Expansion


I suggest using something like this instead:
Code:
n=0
while [ $(( n+=1 )) -le 100 ]
do
  echo "process n=$n"
done


Last edited by Scrutinizer; 07-09-2016 at 11:58 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 9  
Old 07-10-2016
Quote:
Originally Posted by wisecracker
Well I have been experimenting with sleep <secs> to give me a smaller time accurate to about + or - 5mS on this MacBook Pro, OSX 10.7.5.

It would be good enough for the Egg_Timer.sh and passes the ShellCheck test.

It is fully posix compliant with SLEEPs unity seconds timer only.

Boy am I gonna get some flak over this one, (yes I bend the rules a little)... ;o)
Code:
#!/bin/sh
# delay.sh <secs> <millisecs[0-999]>
secs="$1"
millisecs=$( awk -v mS="$2" -v correction=0.94 ' BEGIN { print (( mS / 2 ) * correction ); } ' )
millisecs="${millisecs%.*}"

# Accuracy on my MBP OSX 10.7.5 + or - 5mS of FSD.
mS()
{
	loop=0
	while [ "$loop" -le "$millisecs" ]
	do
		sleep 0
		wait
		loop=$(( loop + 1 ))
	done
}
# Test the mS function only.
time mS
sleep "$secs"

As I quoted in a previous post I could create a resonably accurate timer using 'afplay', 'aplay' or '/dev/dsp'. I might use afplay and make it Apple dedicated...

Thanks Don for the info...

I await the flak... ;o)
I'm not sure if this is flak or not, but I'm not sure that I understand what this script is trying to do.

Just using POSIX-defined features in your shell script does not mean that your shell script will give you the same results on all POSIX-conforming systems.

If the idea is to sleep for a particular number of seconds and milliseconds on OS X version 10.7.5 with the particular amount and type of memory present on your system with the same CPU you're using and with the same disk drive you're using under similar load conditions, the code above might come close to doing what you want. But, for small sub-second sleeps, the amount of time spent forking and execing awk and sleep may overwhelm your other calculations. And, if you invoke this script several times, the first time it runs is likely to be slower than subsequent runs since awk, sleep, and this script will need to be loaded from disk the first time it runs, while subsequent runs in the not too distant future can be loaded from the cache.

So, although the way you are performing the calculations in this script is portable, the calculations you are performing are not portable to any other hardware and software configuration because what you are really calculating is how long it will take to fork(), exec sleep, run sleep 0, and waitpid() for the exit status from the sleep 0. All of this is very dependent on your hardware and is not portable to any different configuration. Note also that the wait in your loop is a relatively quick no-op. The wait would only take any significant amount of time if your script had started background jobs that had not previously been waited for. (Your script never starts any background jobs, so all the wait built-in does is loop through the zero background jobs running in the current shell waiting for each one to complete before it returns a zero exit status.)

Although:
Code:
sleep sss.mmm

where sss is a given number of seconds and mmm is a given number of milliseconds to sleep is not portable between various systems, it is portable and will give you much more consistent sleep times between various versions of hardware running various versions of OS X (at least for releases of OS X greater than or equal to 10.0). It will also work on other BSD or Linux based systems and, if you use a 1993 or later ksh instead of bash, on any system using that shell. (ksh93 has a built-in sleep that accepts fractional seconds; bash version 3.2.57 does not have a built-in sleep on OS X.)

If you want a portable way to sleep for a given number of seconds and nano-seconds in a shell script that will work on any POSIX-conforming system, you'll need to write your own nanosleep utility in C that calls the nanosleep() function with the seconds and nanoseconds specified by operands given when it is invoked and then call that utility from your script. Or you can configure your script to invoke the system's sleep utility if it supports sub-second sleeps, or invoke your nanosleep utility otherwise.
This User Gave Thanks to Don Cragun For This Post:
# 10  
Old 07-10-2016
Hi.

Also:
Code:
            For delays of finer granularity than one second, the Time::HiRes
            module (from CPAN, and starting from Perl 5.8 part of the standard
            distribution) provides usleep(). You may also use Perl's
            four-argument version of select() leaving the first three
            arguments undefined, or you might be able to use the "syscall"
            interface to access setitimer(2) if your system supports it. See
            perlfaq8 for details.

Excerpt from perldoc -f sleep

For example:
Code:
#!/usr/bin/env perl

# @(#) p1       Demonstrate usleep;

use Time::HiRes qw( usleep nanosleep );

$microseconds = 2500000;
usleep($microseconds);

# nanosleep ($nanoseconds);

exit(0);

which would produce something like:
Code:
time ./p1

real    0m2.514s
user    0m0.008s
sys     0m0.000s

On an older OS/X:
Code:
OS, ker|rel, machine: Apple/BSD, Darwin 9.8.0, Power Macintosh
Distribution        : Mac OS X 10.5.8 (leopard, workstation)
perl 5.8.8

real    0m2.578s
user    0m0.045s
sys     0m0.021s

Best wishes ... cheers, drl

Last edited by drl; 07-10-2016 at 11:02 PM..
This User Gave Thanks to drl For This Post:
# 11  
Old 07-11-2016
Hi Don...
You are of course correct in what you have quoted. I was well aware of my sleep alternative's limitations so I decided to embed a delay into a shell script. You suggested a C program to do the job, so here goes...
Code:
#!/bin/sh
# delay <milliseconds>
# MacBook Pro, 13 Inch, Circa August 2012, OSX 10.7.5, using sh and limited /bin/echo command.
clear

# Use /bin/echo here for minimalist echo command.
/bin/echo '/* delay.c */
/* Usage:- delay <milliseconds[0 to 1000000]> */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
	unsigned int milliseconds;
	milliseconds = 0;

	/* This allows for NULL millisecond argument and more arguments too. */
	if (argc <= 1 || argc >= 3)
	{
		printf ("\nUsage:- delay <milliseconds[0 to 1000000]>\n\n");
		return 1;
	}

	/* Check for number of characters, no less than 1 and no more than 7. */
	if (strlen(argv[1]) <= 0 || strlen(argv[1]) >= 8) (argv[1]) = "0";

	/* Characters in function atoi() returns integer 0. */
	milliseconds = atoi(argv[1]);

	/* Finally, ONLY allow 0 to 1000000 range. */
	if (milliseconds <= 0 || milliseconds >= 1000001) milliseconds = 0;

	usleep (milliseconds * 1000);
	return 0;
}' > /tmp/delay.c
echo "Show C source..."
cat /tmp/delay.c
echo "Compile the C source..."
gcc /tmp/delay.c -o /tmp/delay
ls -l /tmp/delay*
echo "Delay for 3 seconds - /tmp/delay 3000..."
/tmp/delay 3000
echo "Return code is $?..."
echo "Now show error report - /tmp/delay..."
/tmp/delay
echo "Return code is $?..."

Results:-
Code:
Last login: Mon Jul 11 22:09:24 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./mS_delay.sh


Show C source...
/* delay.c */
/* Usage:- delay <milliseconds[0 to 1000000]> */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
	unsigned int milliseconds;
	milliseconds = 0;

	/* This allows for NULL millisecond argument and more arguments too. */
	if (argc <= 1 || argc >= 3)
	{
		printf ("\nUsage:- delay <milliseconds[0 to 1000000]>\n\n");
		return 1;
	}

	/* Check for number of characters, no less than 1 and no more than 7. */
	if (strlen(argv[1]) <= 0 || strlen(argv[1]) >= 8) (argv[1]) = "0";

	/* Characters in function atoi() returns integer 0. */
	milliseconds = atoi(argv[1]);

	/* Finally, ONLY allow 0 to 1000000 range. */
	if (milliseconds <= 0 || milliseconds >= 1000001) milliseconds = 0;

	usleep (milliseconds * 1000);
	return 0;
}
Compile the C source...
-rwxr-xr-x  1 barrywalker  wheel  8824 11 Jul 22:14 /tmp/delay
-rw-r--r--  1 barrywalker  wheel   802 11 Jul 22:14 /tmp/delay.c
Delay for 3 seconds - /tmp/delay 3000...
Return code is 0...
Now show error report - /tmp/delay...

Usage:- delay <milliseconds[0 to 1000000]>

Return code is 1...
AMIGA:barrywalker~/Desktop/Code/Shell> _

ShellCheck results for the script:-
Code:
$ shellcheck myscript
 
Line 7:
/bin/echo '/* delay.c */
          ^-- SC2028: echo won't expand escape sequences. Consider printf.

$ _

I don't want to expand any escape sequences...

Hi drl...
I could do it in python too.
Code:
#!/usr/bin/python
# fpt.py <floating point time>
import time
import sys
fpt=float(sys.argv[1])
time.sleep(fpt)
exit(0)

Thanks both for the feedback...

Last edited by wisecracker; 07-12-2016 at 06:24 AM.. Reason: Correct typo.
# 12  
Old 07-20-2016
I thought you guys migh be interested in this...

The code in my post #11 works without a hitch on a stock AMIGA A1200(HD), AMIGA-OS 3.0x, with and wihout a 4MB Fastram upgrade, running the 'ADE' unix install for the AMIGA...

Is it slow to run the shell script? Yes.
Are there any errors? No.
Even /usr/bin/echo exists - cool.

It uses 'ksh[88?]' inside the default AMIGA shell window which has a subset of terminal escape codes.

KSH version is:-
@(#)PD KSH v5.2.12 Geek Gadgets Amiga port 97/08/13 .

GCC version is:-
Code:
Reading specs from /gg/lib/gcc-lib/m68k-amigaos/2.95.3/specs
gcc version 2.95.3 20010315 (release)

AFAIAC this is solved.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. HP-UX

Password compliance setting

I need to set password compliance for some servers in my company. However, the requirements are that we need to set different password policies for 3 different user groups within the company. These are : System Users: i.e root, etc Batch/Application Users: oracle, bscs, etc Standard User:... (0 Replies)
Discussion started by: anaigini45
0 Replies

2. Red Hat

Looking for PCI Compliance tool for Redhat Lix.

Hi i am in new to Linux world . I have been assigned to a project to find out a tool that will fulfill the PCI compliance for Linux servers for Audit process. anyone have any recommendation on that. Do Rad hat have any native application or plug-ins which we can use for that. (1 Reply)
Discussion started by: sahasuman
1 Replies

3. Cybersecurity

PCI DSS Compliance : Insecure Communication Has Been Detected

From the nessus scanner tool report i got below vulnerability PCI DSS Compliance : Insecure Communication Has Been Detected http://www.tenable.com/plugins/index.php?view=single&id=56208 As per the description given in above link - I am not able to understand How to find insecure port... (2 Replies)
Discussion started by: saurabh84g
2 Replies

4. UNIX for Dummies Questions & Answers

man synopsis standard compliance

In different online sources, I found bits and pieces of information about those square and angular brackets and pipes. From what I have read, I can conclude it looks like this: 1. Options outside any brackets are mandatory 2. Options inside these < .. > are mandatory too 3. Options inside ... (4 Replies)
Discussion started by: vkleban
4 Replies

5. UNIX for Advanced & Expert Users

sudo & Sox compliance

Hello, I am trying to convince my boss to stop allowing our users to login as root (superuser). Currently our users login to our unix server with their own account, then as needed, they will do an su and put in the root password. This scares me, for a bunch of reasons. Mainly, one is that we... (1 Reply)
Discussion started by: rwallaceisg
1 Replies
Login or Register to Ask a Question