Well guys I have done some some experiments printf .
(All 'printf' statements now changed inside AudioScope.
Now consider the code below:-
Results from 'ShellCheck':-
Now line 13 is obviously wrong so ignore that but why is line 5 NOT posix compliant but line 18 IS compliant?
Most of the AudioScope.sh printf statements follow the line 9 format...
Terminal results; OSX 10.7.5, default terminal calling 'sh'.
TIA...
In line 5 the expansions $x and $y are not quoted. If the expansion of $x or $y contains any IFS field separator characters, your format string argument to printf will terminate at the 1st whitespace character in the resulting set of strings passed to printf by the shell after the shell expands those variables.
When a variable name in a variable expansion is followed by characters that could be part of a variable name, use:
instead of:
You don't have to quote and unquote every reference to a variable in a quoted string. The following all have exactly the same results in a shell script:
The normal way to write the printf command:
even though the above will not do what you want if $y or $x expands to a string containing any whitespace characters (since both expansions are unquoted), is:
which will work correctly even if $x and $y expand to strings that contain percent signs and/or whitespace characters.
This User Gave Thanks to Don Cragun For This Post:
Your explanation is thorough and finding this sort of information on the WWW is near impossible.
It looks lke I will have to go through the AudioScope code again and clean up. There are only a few lines that contain variables to be expanded.
(BTW I meant to quote line 7 and line 18 - NOT 5. Humble apologies...)
The only difference is ";" and ';' .
Quote:
even though the above will not do what you want if $y or $x expands to a string containing any whitespace characters (since both expansions are unquoted), is:
I would probably never have found this as 'x' and 'y' will always be integers.
Once again you have put me on the straight and narrow.
Finally, do I assume that the line below is not a valid posix 'printf' statement as it is lacking the '%b'?
It does work in 'sh' on this MBP, OSX 10.7.5 default terminal running sh.
(It seems '%b' is a default modifier for 'printf'.)
Finally thanks again...
Your explanation is thorough and finding this sort of information on the WWW is near impossible.
It looks lke I will have to go through the AudioScope code again and clean up. There are only a few lines that contain variables to be expanded.
(BTW I meant to quote line 7 and line 18 - NOT 5. Humble apologies...)
The only difference is ";" and ';' .
I will make the wild assumption that ShellCheck is just checking the printf utility format string operand (and perhaps checking that there are enough operands supplied to match %format-specifier and * in %*.*format-specifier occurrences in the format string operand). Since a format string can be reused if more operands are present than format specifiers, it would be a lot harder for it to determine when an unquoted shell variable is expanded in an operand is wrong versus when an unquoted shell variable in a format string could cause early termination of the format string.
Quote:
I would probably never have found this as 'x' and 'y' will always be integers.
Once again you have put me on the straight and narrow.
Finally, do I assume that the line below is not a valid posix 'printf' statement as it is lacking the '%b'?
It does work in 'sh' on this MBP, OSX 10.7.5 default terminal running sh.
(It seems '%b' is a default modifier for 'printf'.)
Finally thanks again...
No. There is nothing wrong with this this printf command (other than the fact that it won't do what you want on a machine using an EBCDIC codeset and will only work on certain output devices that understand the escape sequence you have encoded at the start of that output). There are no variables in that format string that could be expanded to require additional operands. That command (with double-quotes as you used them or with the double-quotes replaced by single-quotes) produces a partial line of output that is perfectly well-formed. And, since that format string operand contains no percent sign characters, the following commands would also be valid commands producing exactly the same output (more slowly):
(and, in both of those commands the 1st two single-quotes can be changed to double-quotes and the last two single-quotes can be changed to double-quotes with no difference in the output (but it will run slightly more slowly)).
Last edited by Don Cragun; 06-12-2016 at 06:47 AM..
Reason: PS removed; it wasn't in the standards even though it would work on some systems.
This User Gave Thanks to Don Cragun For This Post:
Since you guys have given valid crticism of AudioScope.sh it has made me look more deeply into posix compliance, so......
I typed this directly into ShellCheck and got the bizarre results below.
It means I have to check absolutelty everything from variable constructs to printing to 'STDOUT'.
It looks like ShellCheck successfully finds any variables that seem unused; however...
Ignore the printf STDOUT printing _errors_ that could be produced as I am now fully aware of those...
I have made a rod for my own back here...
Hi wisecracker,
There are lots of things to consider here. Strings used in variable assignments follow slightly different rules than strings used in other shell operations (such as command-line arguments to utilities). See the following script for example:
which produces the output:
So, in your statement:
it does what you want even though the expansions $y and $x are not quoted (because it is an assignment statement). But, why are you adding the extra double-quotes? They don't do anything but make the code harder to read. Variable expansions, command substitutions, and arithmetic expansion are all processed in double-quoted strings, so why not simplify your code and make it easier to read and understand:
(which is a single double-quoted string). Or, if you are really against using braces to keep variable names separate from following text:
(which has the constant characters in the two single-quoted strings and the variable expansions in a single double-quoted string).
And, besides being easier to read and understand, I would think either of these should make shellcheck happy too. (But I don't have shellcheck installed on my system; so I haven't verified that.)
I should also have mentioned in earlier posts on this subject that \x followed by two hexadecimal digits is a common extension provided by many implementations of the printf utility (both stand-alone and as shell built-ins), but it is not mentioned in the standards. Similarly, \e is a common extension that expands to an escape character in the current locale provided by many implementations of the printf utility, but it is not mentioned in the standards either. With any standards conforming version of printf, you can use the octal escape sequence \033 to specify an escape character in locales with an underlying codeset that is a superset of ASCII.
The shell maintains only a single switch for "inside a quoted string" which can be true or false. Therefore it is not possible to nest any quotations. We have often the misunderstanding here that it works like this:
That is: a single-quoted string inside a double-quoted string. The truth is it is like this:
because the single-quotes lose their special meaning as quoting characters inside a quoted string. The shell, upon encountering the first double quote, flips its flag to "inside a quoted string" and it takes every character literally until encountering the second double-quote, when it flips the flag back to "outside a quoted string".
There is actually only a single exception to this rule and it happens with subshells:
is actually one double-quoted string "foo<something>bar", where <something> is the output of the command command "arg1" "arg2".
One more word about your escape-sequences: You might make your script a lot more flexible by using the respective terminals escape-queneces instead of using fixed ones. I haven't looked up what \x1B[0m is for, but lets suppose it is for moving the cursor to row 0, col 0. A better way would be to do it like this:
Dear all,
I have a new Oracle Blade X4-2B server, running Solaris 10.
The server comes with a HBA card that will not be used now. It has not fibers connected to it. As a consequence, its leds never stop flashing.
My question is: how to disable this HBA card, without removing it physically... (2 Replies)
Hello,
When creating shared memory in C, should be remove shared memory with shmctl function when don't need it. If it didn't remove, occupied shared memory stay and remain.
If we create shared memory repeatedly without removing unusable shared memory, /dev/shm will full.
Does Unix or... (1 Reply)
Hi,
In our AIX 5.2 server , we have one unused ethernet adapter which doesn't have cable connection . For this interface , we are getting alerts in errpt .
Could you suggesthow to stop this alert ? And sametime i would like to keep this device in ODM .
Is there... (1 Reply)
i Have alloted 20G in my vmware for solaris 10, upon installation, and some distribution of disk space to /,/opt,swap i just use 19G.
Can i still use the 1G? How? how to see the 1G? that i did not use? how can i use it?
appreciate your responce (17 Replies)
Hello,
I have a system with HP-UX 11.23 installed on it. There are ~36GB of unused space on the HDD. I did a very basic installation, and it created the usual volume group /dev/vg00. When I look at the output of ioscan -funC disk, I see this (and more, but irrelevant to this post):
disk ... (1 Reply)
Hi,
How do I query for unused partition in AIX 4.3 with DAS and SAA storage?
I know most unix administrator don't put all the capacity on the system at once.
thanks,
vene (1 Reply)
One of my Solaris 8 machines hd was about to die. So I used g4u to create an image of the 9gb drive and I put it in a 36gb drive. That solved my dieing hd problem.
But....
How do I get my machine to see the unused 27gb of space?
Any help would be greatly appreciated. (1 Reply)