[Shell] -c и параметры командной строки
The first non-option argument specified on the command line will beТо есть наличие опции -c не отменяет того факта, что первый аргумент трактуется как имя файла со скриптом? Но тогда непонятно, почему sh не пытается выполнить команды, содержащиеся в этом файле?
treated as the name of a file from which to read commands (a shell
script and the remaining arguments are set as the positional parameters
of the shell ($1, $2, etc). Otherwise, the shell reads commands from its
standard input.
Ведь в том же мануале написано, что
The -c option causes the commands to be read from the string operandТо есть команда, указанная после -c выполняется вместо команд, поступающих со стандартного ввода, а не вместо команд, поступающих из файла, указанного в качестве первого аргумента...
instead of from the standard input. Keep in mind that this option only
accepts a single string as its argument, hence multi-word strings must be
quoted.
sh -c[-abCefhimnuvx][-o option][+abCefhimnuvx][+o option]command_string
[command_name [argument...]]
...
-c Read commands from the command_string operand. Set the value of special parameter 0 (see Special Parameters ) from the value
of the command_name operand and the positional parameters ($1, $2, and so on) in sequence from the remaining argument oper-
ands. No commands shall be read from the standard input.
Поясню своими словами: выполняется команда из строки command_string ("echo $1" в твоем случае). Потом идет строка, на которую заменяется $0 в строке command_string ("Hello" в твоем примере потом остальные $1, $2 и т.д. (World") Все работает так, как и должно работать.
Твой мануал лучше, чем мой мануал
Оставить комментарий
VitMix
Под FreeBSD командаsh -c 'echo $1' Hello World
печатает "World", хотя на мой взгляд должна бы печатать "Hello".
Более того, команда
sh -c 'echo $0' Hello World
Печатает "Hello", хотя в мануале написано, что: "$0 (zero) Expands to the name of the shell or shell script."...
Кто-нибудь может мне объяснить. в чём тут дело?