forked from shfshanyue/Daily-Question
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
913ac3b
commit d2cf8e6
Showing
6 changed files
with
108 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# function | ||
|
||
shell 中函数语法与其它语言类似,而调用函数,如同使用命令行。 | ||
|
||
``` bash | ||
hello () { | ||
echo hello | ||
} | ||
|
||
// 调用函数 | ||
//=> hello | ||
hello | ||
``` | ||
|
||
## 传递参数 | ||
|
||
在传递参数时,使用 `$1`、`$2`、`$3`... 接收参数,而 `$0` 指函数名。 | ||
|
||
``` bash | ||
hello () { | ||
echo $0 $1 $2 $3 $4 | ||
} | ||
|
||
# 调用函数 | ||
#=> hello a b c d | ||
hello a b c d | ||
``` | ||
|
||
## 特殊变量 | ||
|
||
除此之外,在函数中还有以下特殊的变量 | ||
|
||
+ `$#`: 参数数量 | ||
+ `$*`: 所有参数,字符串形式 | ||
+ `$@`: 所有参数,数组形式,其中 `${@[1]}` 等同于 `$1` | ||
|
||
如果你第一次接触以上变量,有可能傻傻分不清楚。但是联系到上一篇关于数组的文章,就很容易联想到 `@` 代表所有数组,`#` 代表数组个数。 | ||
|
||
``` bash | ||
hello () { | ||
echo '$#' ${#} | ||
echo '$*' ${*} | ||
echo '$@' ${@} ${@[1]} $1 | ||
} | ||
|
||
# 调用函数 | ||
# => Output: | ||
# $# 4 | ||
# $* a b c d | ||
# $@ a b c d a a | ||
hello a b c d | ||
``` | ||
|
||
## 命令行即函数 | ||
|
||
函数的调用方法是不和命令行调用方法一模一样?实际上,可把命令行视为函数。 | ||
|
||
如果 `$0`、`$1`、`$@` 出现在全局,则表明他们是命令行的参数。 | ||
|
||
## 作业 | ||
|
||
1. [nodejs 官方镜像的 docker-entrypoint](https://github.com/nodejs/docker-node/blob/main/16/alpine3.16/docker-entrypoint.sh) 是何意思? | ||
1. `$0` `$1` `$@` 各代表什么意思 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters