-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path5-Functions.sh
More file actions
58 lines (44 loc) · 1.14 KB
/
5-Functions.sh
File metadata and controls
58 lines (44 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
#Bash Tutorial 5: Functions
#Description: How to use Functions in bash, including parsing variables, and using return values
#Written by QuidsUp
#Use with Youtube Video: https://youtu.be/kiEbj_DHB4k
function foo1 {
echo "Foo1 - A Basic Function"
}
#Input variables into a function
function foo2() {
echo "Foo2 Function $1 $2"
echo "All variables parsed to Foo2 $*"
echo
}
foo1
foo2 "Hi" "There" "Everyone" 2
#Return Variables
function Return_Variable() {
return $1
#Bash can only return integer values 0..255
}
Return_Variable 25
Var1=$?
Var2=$? #Won't work because we've just used the return variable
echo "Var1 =$Var1" #25
echo "Var2 =$Var2" #0
function Return_Variable2() {
local Ret=$1 #Local variable which only exists in this function
let "Ret = Ret - 5"
return $Ret
}
Return_Variable2 20
Var3=$?
echo "Can we see the Local Variable? $Ret"
echo "Var3 =$Var3" #15
#Return a value using a Global variable
function Global_Return {
GlobalRet=0
let "GlobalRet = $1 + 1"
}
Global_Return 260
echo "GlobalReturn Variable =$GlobalRet" #261
Global_Return 270
echo "GlobalReturn Variable =$GlobalRet" #271