1.创建函数
function name {
commands
}
也可以写成下面这种形式
name() {
commands
}
2.函数返回值
函数退出状态是函数最后条命令返回的退出状态,可以用 $? 确定函数的退出状态
使用return:
#!/bin/bash
func1() {
read -p "Enter a value: " value
echo "the value times two"
return $[ $value * 2 ]
}
func1
echo "now value = $?"
使用函数的输出:
#!/bin/bash
func1() {
read -p "Enter a value: " value
echo $[ $value *2 ]
}
v1=func1
echo "v1 =
v
1
<
/
s
p
a
n
>
"
<
/
s
p
a
n
>
<
/
c
o
d
e
>
<
/
p
r
e
>
<
b
r
>
<
p
r
e
>
<
c
o
d
e
c
l
a
s
s
=
"
l
a
n
g
u
a
g
e
−
r
u
b
y
"
>
a
l
l
e
n
@
u
b
u
n
t
u
<
s
p
a
n
c
l
a
s
s
=
"
h
l
j
s
−
s
y
m
b
o
l
"
>
:
/
l
e
a
r
n
l
i
n
u
x
<
/
s
p
a
n
>
v1</span>"</span></code></pre><br><pre><code class="language-ruby">allen@ubuntu<span class="hljs-symbol">:~/learnlinux</span>
v1</span>"</span></code></pre><br><pre><codeclass="language−ruby">allen@ubuntu<spanclass="hljs−symbol">: /learnlinux</span> ./read.sh
Enter a value: 10
v1 = 20
3.在函数中使用变量
函数可以使用标准参数环境变量来表示命令行传递给函数的参数. 专用变量 $# 可以用来确定传递给函数的参数数目.
在脚本中指定函数时,必须在函数所在命令行提供参数 : func1 $value1 10
#!/bin/bash
# passing parameters to a function
function addem {
if [ KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲</span> -eq 0 ]…# -gt 2 ]
then
echo -1
elif [ $# -eq 1 ]
then
echo $[ $1 + $1 ]
else
echo $[ $1 + $2 ]
fi
}
echo -n "Adding 10 and 15: "
value=addem 10 15
echo
v
a
l
u
e
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
h
l
j
s
−
b
u
i
l
t
i
n
"
>
e
c
h
o
<
/
s
p
a
n
>
−
n
<
s
p
a
n
c
l
a
s
s
=
"
h
l
j
s
−
s
t
r
i
n
g
"
>
"
L
e
t
′
s
t
r
y
a
d
d
i
n
g
j
u
s
t
o
n
e
n
u
m
b
e
r
:
"
<
/
s
p
a
n
>
v
a
l
u
e
=
‘
a
d
d
e
m
10
‘
<
s
p
a
n
c
l
a
s
s
=
"
h
l
j
s
−
b
u
i
l
t
i
n
"
>
e
c
h
o
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
h
l
j
s
−
v
a
r
i
a
b
l
e
"
>
value</span> <span class="hljs-built_in">echo</span> -n <span class="hljs-string">"Let's try adding just one number: "</span> value=`addem 10` <span class="hljs-built_in">echo</span> <span class="hljs-variable">
value</span><spanclass="hljs−builtin">echo</span>−n<spanclass="hljs−string">"Let′stryaddingjustonenumber:"</span>value=‘addem10‘<spanclass="hljs−builtin">echo</span><spanclass="hljs−variable">value
echo -n "Now trying adding no number: "
value=addem
echo
v
a
l
u
e
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
h
l
j
s
−
b
u
i
l
t
i
n
"
>
e
c
h
o
<
/
s
p
a
n
>
−
n
<
s
p
a
n
c
l
a
s
s
=
"
h
l
j
s
−
s
t
r
i
n
g
"
>
"
F
i
n
a
l
l
y
,
t
r
y
a
d
d
i
n
g
t
h
r
e
e
n
u
m
b
e
r
s
:
"
<
/
s
p
a
n
>
v
a
l
u
e
=
‘
a
d
d
e
m
101520
‘
<
s
p
a
n
c
l
a
s
s
=
"
h
l
j
s
−
b
u
i
l
t
i
n
"
>
e
c
h
o
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
h
l
j
s
−
v
a
r
i
a
b
l
e
"
>
value</span> <span class="hljs-built_in">echo</span> -n <span class="hljs-string">"Finally, try adding three numbers: "</span> value=`addem 10 15 20` <span class="hljs-built_in">echo</span> <span class="hljs-variable">
value</span><spanclass="hljs−builtin">echo</span>−n<spanclass="hljs−string">"Finally,tryaddingthreenumbers:"</span>value=‘addem101520‘<spanclass="hljs−builtin">echo</span><spanclass="hljs−variable">value
allen@ubuntu:~/learnlinux/UNIX$ ./func.sh
Adding 10 and 15: 25
Let’s try adding just one number: 20
Now trying adding no number: -1
Finally, try adding three numbers: -1