You need to remove the double-quotes. It is trying to run a command called "ulimit -Hn" as a single command, spaces and all. -s
needs to be the last sudo
option on the sudo
command line, and all following arguments are passed to $SHELL -c
to execute.
sudo -u elasticsearch -s ulimit -Hn
I guess the way -s
is processed has changed, as the current way allows you to pass arguments with spaces to $SHELL
by escaping them on the command line:
$ touch '/tmp/foo bar'
$ sudo -s rm '/tmp/foo bar'
The old method of argument handling would split that '/tmp/foo bar'
argument into two, breaking the command.
Zsh has a built-in way to sort lists. However, I don't think there's a way to sort the values while keeping the correlation with the keys using parameter expansion flags and subscript flags, which means that an explicit loop is necessary. Assuming that your values don't contain a null character, you can build an array containing the values and keys concatenated with a null character in between, and sort that.
keys=("${(@k)A}")
values=("${(@v)A}")
combined=()
for ((i=1; i <= $#values; i++)) { combined[i]=($values[i]$'\0'$keys[i]); }
keys_sorted_by_decreasing_value=("${${(@On)combined}#*$'\0'}")
keys_of_the_top_two_values=("${(@)keys_sorted_by_decreasing_value[1,2]}")
EDIT by @sch: the first 4 lines can be simplified to
combined=()
for k v ("${(@kv)A}") combined+=($k$'\0'$v)
The variables keys
and values
contain the keys and values of A
in an arbitrary but consistent order. You can write keys=(${(k)A})
if there are no empty keys, and similarly for values. keys_sorted_by_decreasing_value
sorts keys lexicographically, add the n
flag to sort numerically (9
before 10
) and remove O
if you want to sort in increasing order (in which case the top two values can be obtained with the subscript [-2,-1]
).
Ksh93 has a way to sort the positional parameters only, with set -s
; this also exists in zsh but not in bash 4.2. Assuming your values don't contain newlines or control characters that sort before newlines:
keys=("${!A[@]}")
combined=()
for ((i=0; i <= ${#keys}; i++)); do combined[i]=(${A[${keys[$i]}]}$'\n'${keys[$i]}); done
set -A sorted -s "${combined[@]}"
top_combined=${sorted[${#sorted[@]}-1]} # -2 for the next-to-largest, etc.
top_key=${top_combined#*$'\n'}
This is all pretty complex, so you might as well go for the external sort, which is a lot easier to write. Assuming that neither keys nor values contain control characters, in ksh or bash:
IFS=$'\n'; set -f
keys_sorted_by_decreasing_value=($(
for k in "${!A[@]}"; do printf '%s\t%s\n' "${A[$k]}" "$k"; done |
sort | sed $'s/\t.*//'
))
Best Answer
You need to use
"$(somecmd "$file")"
.Without the quotes, a path with a space will be split in the argument to
somecmd
, and it will target the wrong file. So you need quotes on the inside.Any spaces in the output of
somecmd
will also cause splitting, so you need quotes on the outside of the whole command substitution.Quotes inside the command substitution have no effect on the quotes outside of it. Bash's own reference manual isn't too clear on this, but BashGuide explicitly mentions it. The text in POSIX also requires it, as "any valid shell script" is allowed inside
$(...)
:Example:
a. No quotes,
dirname
processes both./space
andhere/foo
:b. Quotes inside,
dirname
processes./space here/foo
, giving./space here
, which is split in two:c. Quotes outside,
dirname
processes both./space
andhere/foo
, outputs on separate lines, but now the two lines form a single argument:d. Quotes both inside and outside, this gives the correct answer:
(that would possibly have been simpler if
dirname
only processed the first argument, but that wouldn't show the difference between cases a and c.)Note that with
dirname
(and possibly others) you also need want to add--
, to prevent the filename from being taken as an option in case it happens to start with a dash, so use"$(dirname -- "$file")"
.