Tho*_*ner 5 shell freebsd debian arguments
摘自FreeBSD 的sh手册页(因为它是最方便的在线工具,如果重要的话,目标平台是 Debian):
SH(1) FreeBSD General Commands Manual SH(1)
NAME
sh -- command interpreter (shell)
SYNOPSIS
sh [-/+abCEefhIimnPpTuVvx] [-/+o longname] [script [arg ...]]
sh [-/+abCEefhIimnPpTuVvx] [-/+o longname] -c string [name [arg ...]]
sh [-/+abCEefhIimnPpTuVvx] [-/+o longname] -s [arg ...]
...
Run Code Online (Sandbox Code Playgroud)
我对以下用例特别感兴趣:
sh [-/+abCEefhIimnPpTuVvx] [-/+o longname] -c string [name [arg ...]]
Run Code Online (Sandbox Code Playgroud)
# Normal:
myscript hello world
myscript hello world >/var/log/myscript.combined 2>&1
# Constrained:
# - Arguments 'hello' and 'world' are suffixed by calling program.
myscript >/var/log/myscript.combined 2>&1 hello world
# Constrained Further:
# - Wrapped in `sh` to catch system messages, like Segmentation Fault
sh -c 'myscript $@' _ >/var/log/myscript.combined 2>&1 hello world
Run Code Online (Sandbox Code Playgroud)
我注意到第一个参数没有传递给myscript
,并且文档暗示了一个name
参数,我没有看到 doc 部分。在我的示例中,我添加了 ad_
代替了 name 参数,但是:
name
什么?表格:
sh -c '...' name arg1 arg2 ...
Run Code Online (Sandbox Code Playgroud)
称为内联脚本,通常与find ... -exec sh -c '...' find-sh {} +
. 在 inline-script 中,$0
将设置为name
,其余参数填充为$@
。
通常,您应该将其设置为有意义的值,因为它将用于例如显示错误消息:
sh -c 'echo "${1?}"' foo
foo: 1: foo: 1: parameter not set
Run Code Online (Sandbox Code Playgroud)
但是您可以设置name
为您喜欢的任何单词,以指示内联脚本的名称:
sh -c 'printf "%s\n" "$0"' custom-sh 1 2 3
custom-sh
Run Code Online (Sandbox Code Playgroud)
此行为由POSIX定义。
小智 5
Linux 中的手册页详细了一点。
sh -c [-aCefnuvxIimqVEb] [+aCefnuvxIimqVEb] [-o option_name] [+o option_name] command_string [command_name [argument ...]]
-c 从 command_string 操作数而不是从标准输入读取命令。特殊参数 0 将从 command_name 操作数设置,位置参数($1、$2 等)将从其余参数操作数设置。
command_name执行 command_string 中的命令时分配给特殊参数 0 的字符串。如果未指定 command_name,则特殊参数 0 应设置为从其父级传递给 sh 的第一个参数的值(例如,C 程序的 argv[0]),通常是用于执行 sh 实用程序的路径名.
归档时间: |
|
查看次数: |
2038 次 |
最近记录: |