Difference between revisions of "CSC231 Bash Tutorial 8"
(→Bash Functions) |
(→Passing Arguments) |
||
Line 59: | Line 59: | ||
</source> | </source> | ||
<br /> | <br /> | ||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | |||
+ | ==Challenge 1== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | <br /> | ||
+ | * Add a new function to '''func2.sh''' called ''printInfo()''. The new function takes 2 parameters and calls ''printName'' and ''printAge'' to print both. Here is an example of how to call it (that will be the only function call in the main part of the script): | ||
+ | |||
+ | printInfo "Kathleen McCartney" 61 | ||
+ | |||
+ | :and the output will be the same as the previous version of '''func2.sh''': | ||
+ | |||
+ | ------------------------ | ||
+ | Hello Kathleen McCartney | ||
+ | Your age: 61 | ||
+ | ------------------------ | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | =Solutions= | ||
+ | <br /> | ||
+ | ==Challenge 1== | ||
+ | <br /> | ||
+ | ::<source lang="bash"> | ||
+ | #! /bin/bash | ||
+ | # funcChallenge1.sh | ||
+ | # D. Thiebaut | ||
+ | |||
+ | printBar() { | ||
+ | echo "------------------------" | ||
+ | } | ||
+ | |||
+ | function printName { | ||
+ | echo "Hello $1" | ||
+ | } | ||
+ | |||
+ | function printAge { | ||
+ | echo "Your age: $1" | ||
+ | } | ||
+ | |||
+ | function printInfo { | ||
+ | printBar | ||
+ | printName $1 | ||
+ | printAge $2 | ||
+ | printBar | ||
+ | } | ||
+ | |||
+ | printInfo "Kathleen McCartney" 61 | ||
+ | |||
+ | |||
+ | |||
+ | </source> |
Revision as of 13:38, 1 November 2017
--D. Thiebaut (talk) 13:13, 1 November 2017 (EDT)
Bash Functions
There are two ways of declaring functions in bash, illustrated in the code below:
#! /bin/bash # func1.sh # D. Thiebaut # prints some messages printSomething() { echo "Hello there!" } function printSomethingElse { echo "Hello again!" } printSomething printSomething printSomethingElse
- Create the script above, make it executable, and run it, to see how it works.
- Add a call to printSomething inside the printSomethingElse function, just to see if functions can actually call functions... Does bash accept nested calls?
Passing Arguments
- Inside a function, $1 will refer to the first parameter passed to the function, $2 will refer to the second argument, etc.
- Here is an example, with both style functions:
#! /bin/bash # func1.sh # D. Thiebaut printBar() { echo "------------------------" } function printName { echo "Hello $1" } function printAge { echo "Your age: $1" } printBar printName "Kathleen McCartney" printAge 61 printBar
Challenge 1 |
- Add a new function to func2.sh called printInfo(). The new function takes 2 parameters and calls printName and printAge to print both. Here is an example of how to call it (that will be the only function call in the main part of the script):
printInfo "Kathleen McCartney" 61
- and the output will be the same as the previous version of func2.sh:
------------------------ Hello Kathleen McCartney Your age: 61 ------------------------
Solutions
Challenge 1
#! /bin/bash # funcChallenge1.sh # D. Thiebaut printBar() { echo "------------------------" } function printName { echo "Hello $1" } function printAge { echo "Your age: $1" } function printInfo { printBar printName $1 printAge $2 printBar } printInfo "Kathleen McCartney" 61