3 ms·
I still use Perl every now and again for powerful one-liners and I'm really glad it's around. Here is a bash function I use which takes args and merges and dedu
by gkfasdfasdf 6y ago
I still use Perl every now and again for powerful one-liners and I'm really glad it's around. Here is a bash function I use which takes args and merges and deduplicates PATH-like expressions:
merge-args ()
{
perl -e 'print join ":", grep {!$h{$_}++} split ":", join ":", @ARGV' "$@"
}
And I use as follows:
export PATH="$(merge-args "${SCRIPT_DIR}"/clang-tidy-ex /usr/local/opt/llvm/bin "${INSTALL_DIR}"/{,samples/}bin "${PATH}")"
ASAN_OPTIONS="$(merge-args "${ASAN_OPTIONS}" detect_leaks=1 check_initialization_order=1 detect_stack_use_after_return=1 strict_init_order=1 strict_string_checks=1 detect_odr_violation=0)"
I know there are other ways of doing this in other languages and even in bash itself, but to me this is simple and elegant.
- Spivak 6y agoA pure bash alternative for comparison. merge-args() { local res for i; do case ":${res}:" in *:"$i":*) ;; *) res=${res+$res:}$i esac done echo $res }
- gkfasdfasdf 6y agoThat is pretty neat, I did not know that 'for i' on it's own could iterate the args like that.