All Questions
Tagged with portability posix
25 questions
2
votes
1
answer
1k
views
Is it safe to snprintf with output to NULL and size 0?
Is it safe to call snprintf(NULL, 0, "…", …)? I can also ask in other way: Does snprintf write the NUL char if size is 0?
The example in Linux man-pages manpage printf(3) provides an example ...
2
votes
0
answers
49
views
Does POSIX specify sed -E? [duplicate]
sed uses basic regular expressions (BRE) by default. Some implementations offer an option to switch to extended regular expressions instead.
GNU sed introduced the -r option to do so in version 3.01 ...
0
votes
3
answers
753
views
sed portability: extended regex vs. backslash
We can write the next command in two ways:
# using extended regex
$ echo foobar | sed -E 's/(foo)(bar)/\2\1/'
barfoo
And:
# using backslashes
$ echo foobar | sed 's/\(foo\)\(bar\)/\2\1/'
barfoo
...
1
vote
1
answer
490
views
Are octal escape sequences more portable than hex ones for shell scripts
As part of a scheme for writing shell code that's capable of processing arbitrary data, I was looking for a way to convert null bytes to some other character. I found that tr requires arbitrary bytes ...
2
votes
0
answers
798
views
Portable way of finding files whose birth date is after a given timestamp
According to How to find creation date of file?, in Linux filesystems, such as ext4, Btrfs and JFS, that store the file creation time (aka birth time), it is possible to display the file birth time (...
2
votes
2
answers
128
views
Why is the which command so hard to eradicate? [closed]
I'm coming from places like Why not use "which"? What to use then? and https://stackoverflow.com/questions/592620/how-to-check-if-a-program-exists-from-a-bash-script and am myself a long-...
0
votes
0
answers
33
views
Is it possible to have bash but not awk? [duplicate]
Here on Stack Exchange we often see answers on bash questions, that use tools other than the bash language, such as awk, sed, grep, etc.
These tools are specific in POSIX, so they should be ...
2
votes
1
answer
2k
views
POSIX and Portability | shell scripts | grep -s, grep -q
I am all in for portability in regards to shell scripts.
But I am unsure if I am overdoing it right now.
In this example, we have a function called confirmation, which accepts the very first ...
4
votes
1
answer
796
views
Does POSIX make not support globs in prerequisites?
I was reading the POSIX 7 Make definition, and I've noticed that prerequisites are almost always described as “files”:
The make utility examines time relationships and shall update those derived ...
1
vote
1
answer
791
views
Wildcards/Globbing: Are character ranges problematic?
In The Linux Command Line William Shotts claims that character ranges can be problematic. See the relevant excerpt below, emphasis is mine.
Character Ranges
If you are coming from another Unix-like ...
10
votes
3
answers
7k
views
Can I read a single character from stdin in POSIX shell?
Only read -r is specified by POSIX; read -n NUM, used to read NUM characters, is not. Is there a portable way to automatically return after reading a given number of characters from stdin?
My usecase ...
4
votes
2
answers
6k
views
Is the use of sed's "a" command to prefix a line portable?
I noticed the following interesting behavior:
$ printf '%s\n' line{1..2} | sed $'1a\\\nPREFIX'
line1
PREFIXline2
$
Interestingly, this behavior is only possible for the last command in a Sed script,...
3
votes
3
answers
769
views
Is it portable to indent the argument to sed's 'i\' command?
I was under the impression from the POSIX specs for sed that it is necessary to left-align the text on the line following the i\ command, unless you want leading whitespace in the output.
A quick ...
15
votes
5
answers
3k
views
Portable POSIX shell alternative to GNU seq(1)?
I've noticed you can't really count on seq(1) being available on anything but GNU systems. What's a simple reimplementation of seq(1) I can bring with me written in POSIX (not bash) shell?
EDIT: Note ...
4
votes
1
answer
153
views
Bash script for showing newly introduced env variables
I'm making this test script to help me reverse engineer scripts in charge of setting the dev environment.
The script:
#env-changes
#!/bin/bash
TESTED_SCRIPT=$1
shift
ENV_BEFORE=$(env | sort)
. $...
18
votes
4
answers
2k
views
checkbashisms-compliant way to determine the current shell
In my .profile, I use the following code to ensure that Bash-related aliases and functions are only sourced if the login shell actually is Bash:
# If the current (login) shell is Bash, then
if [ "${...
4
votes
1
answer
483
views
List all processes without controlling terminal (only)?
Is there a portable way to do this?
On Linux, I can use ps a -N
but this option isn't available on other (POSIX) systems.
Of course I can use grep '^?' with, say, -o tty,... but is there something ...
1
vote
1
answer
186
views
How reliable/portable are Nix built-ins/commands (echo, ps, sort, uniq) from Debian to other distros
I've coded a script and am wondering about it's reliability / portability.
#!/bin/bash
threadsPlease() {
ps -mo lwp,c -p $1 | sort -gk 2 | uniq -f 1
}
THREAD_LINE=$( threadsPlease $1 | grep -v - |...
5
votes
1
answer
852
views
awk/nawk alternative on SunOs and Linux
I have a script that basically checks the Java version on the box it's running on and does whatever based on the version number.
My problem is that I want to use the same command on both SunOs and ...
7
votes
1
answer
509
views
POSIX alternative to GNU find's -printf predicate
I'd like to rewrite these 2 commands so they will use only POSIX-compliant switches:
find "$TARGET_DIR" -maxdepth 1 -type d -printf '(DIR) %f\n'
find "$TARGET_DIR" -maxdepth 1 -type f -printf '%s %...
48
votes
7
answers
13k
views
Is test or [ or [[ more portable both between bash shells and between other shells?
I see I can do
$ [ -w /home/durrantm ] && echo "writable"
writable
or
$ test -w /home/durrantm && echo "writable"
writable
or
$ [[ -w /home/durrantm ]] && echo "writable"
...
5
votes
1
answer
1k
views
Single or double brackets and portability
I have found some very good answers here on the differences between [ and [[ in 'if' statements. For specific named shells, it seems to be a good idea to use [[ over [ (and it is faster, too).
I'm ...
17
votes
2
answers
5k
views
Which is the most portable of sed, awk, perl and sh?
Can someone put these tools in order of portability? Which of these is certain to be found on even the most minimal *nix systems? Is any of them 100% sure to be present? My guess is that the order is ...
1
vote
2
answers
76
views
Can I use `type` to check whether an arbitrary name is a valid command name?
The bash built-in type can be used for this purpose by checking its exit status:
Exit Status:
Returns success if all of the NAMEs are found; fails if any are not found.
How portable is it? The POSIX ...
78
votes
6
answers
25k
views
How portable are /dev/stdin, /dev/stdout and /dev/stderr?
Occasionally I need to specify a "path-equivalent" of one of the standard IO streams (stdin, stdout, stderr). Since 99% of the time I work with Linux, I just prepend /dev/ to get /dev/stdin, etc., ...