-
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
Showing
3 changed files
with
70 additions
and
1 deletion.
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,37 @@ | ||
--- | ||
title: 1.04 函数使用重定向标准输入 | ||
type: docs | ||
weight: 1 | ||
--- | ||
|
||
# 获取用户名 | ||
exp1: | ||
```bash | ||
#!/bin/bash | ||
|
||
ARGCOUNT=1 | ||
E_ARGEXIT=65 | ||
FILE=/etc/passwd | ||
|
||
if [ $# -ne $ARGCOUNT ] | ||
then | ||
echo "Usage: `basename $0` USERNAME" | ||
exit $E_ARGEXIT | ||
fi | ||
|
||
find_user () | ||
{ | ||
while read line | ||
do | ||
echo "$line" | awk -F ":" '{print $5}' | grep $1 | ||
done | ||
}<$FILE | ||
|
||
find_user $1 | ||
exit 0 | ||
``` | ||
|
||
<div style="display: flex;justify-content: space-between;align-items: center;"> | ||
<p><a href="https://books.linuxwt.com/linuxwtabs/ChapterOne/Fuza_Function2">上一页➡️</a></p> | ||
<p><a href="https://books.linuxwt.com/linuxwtabs/ChapterOne/Fuza_Function4">下一页➡️</a></p> | ||
</div> |
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,32 @@ | ||
--- | ||
title: 1.05 函数使用局部变量 | ||
type: docs | ||
weight: 1 | ||
--- | ||
|
||
# 局部变量与全局变量 | ||
exp: | ||
```bash | ||
#!/bin/bash | ||
|
||
func () | ||
{ | ||
local a=5 | ||
b=10 | ||
} | ||
|
||
func | ||
|
||
echo "\"a\" is equal $a" | ||
echo "\"b\" is equal $b" | ||
``` | ||
执行脚本输出 | ||
"a" is equal | ||
"b" is equal 10 | ||
|
||
**ps:函数内没有使用local声明的变量均是全局变量** | ||
|
||
<div style="display: flex;justify-content: space-between;align-items: center;"> | ||
<p><a href="https://books.linuxwt.com/linuxwtabs/ChapterOne/Fuza_Function3">上一页➡️</a></p> | ||
<p><a href="https://books.linuxwt.com/linuxwtabs/ChapterOne/Fuza_Function5">下一页➡️</a></p> | ||
</div> |