Things I found out/rediscovered during experiments: `echo` bash built-in modifies the input (escape interpretation, trailing newline) and not all versions have options to suppress those behaviors. Using printf instead to get a "clean" echo. `sed` on mac does not support group-0 reference, created an extra capture group and reference that instead "${var/?/Y/Z}" is a perfect substring replacement for our needs. Is supported by bash, zsh, and ksh93 but is not supported by the dash (ubuntu) or ksh88 (older AIX?) shells. `awk` and `sed` will both consume trailing newlines and/or whitespace from the input. Resolved by padding input value with non-whitespace and strip result afterwards. http://pubs.opengroup.org/onlinepubs/9699919799/ section 2.6.2 of Shell volume documents ${x%w} ${x#w} expansions
The basic code that seems to work for my on ubuntu+dash+gnu_sed and mac +bash+bsd_sed (which is the default for those platforms): esc_arg="X${arg}X" esc_arg="$(printf '%s' "$esc_arg" | sed -e 's@\([$"\\`]\)@\\\1@g')" esc_arg="${esc_arg#X}" # posix remove single leading X if present esc_arg="${esc_arg%X}" # posix remove single trailing X if present http://pubs.opengroup.org/onlinepubs/9699919799/ quoted_arg="\"$esc_arg\"" ant_exec_args="$ant_exec_args $quoted_arg" This pattern passes a checkbashisms script cleanly. Concerns: is awk or sed a better choice for cross platform consistency did we still need the trailing newline for sed now that the value is padded? I did not see that as necessary in my testing on either mac or ubuntu once padded and had removed it. The branch name is a bit of a misnomer since I removed the actuall shell detection, but wanted to leave that commit in the history locally in case we have reason to adopt that sort of detection. The trade off is doing ugly'ish zsh/ksh93/bash detection for the benefit of keeping everything internal and not having the child tools do it. I have more comments inline https://github.com/jwadamson/ant-1/blob/shell-detection/src/script/ant as well as an awk pattern alternative to sed ( commit c591ade4aa7c19ef7e640ed9759fd95e8c4582d8) Regards, Jeff Adamson From: Stefan Bodewig <bode...@apache.org> To: dev@ant.apache.org Date: 10/12/2016 05:57 AM Subject: Re: ant wrapper script testing On 2016-10-11, Jeff Adamson wrote: > I have been doing some work on trying to correct errors in the ant shell > script with how special characters and whitespace and handled in arguments. Many thanks for that. > The only solution I have found to date that avoids all behavior quirks such > as echo, sed, awk, and whitespace handling is a bashism/ksh/zsh-ism. So > that does not work for ubuntu's /bin/sh. I have not figured out a universal > correct script for dash (ubuntu) yet. > It may be worth detecting those shells to leverage that when we can. understood. If we are able to get it to work without relying on sed which seems to be way more platform specific than I had thought, then I'd prefer such a solution. > I have run the script with bash, ksh, zsh, and dash under Ubuntu and Mac. > Csh and tcsh do not seem to work (do we expect them to?) No, we don't expect the csh family of shells to be able to run /bin/sh scripts. Stefan --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@ant.apache.org For additional commands, e-mail: dev-h...@ant.apache.org