Difference between revisions of "CSC231 Bash Tutorial 8"

From dftwiki3
Jump to: navigation, search
(Challenge 2)
(Bash Functions Returning Values)
Line 156: Line 156:
 
Bash has a strange (weird ?) way of implementing functions returning values.  Let's observe the following example, that will print the following output:
 
Bash has a strange (weird ?) way of implementing functions returning values.  Let's observe the following example, that will print the following output:
 
   
 
   
  1
+
  1  2
  2
+
  2  4
-------------
+
  3  6
  2
+
  4  8
  4
+
  5  10
-------------
 
  3
 
  6
 
-------------
 
  4
 
  8
 
-------------
 
  5
 
  10
 
-------------
 
 
   
 
   
 
Here's the code for this script:
 
Here's the code for this script:
Line 185: Line 175:
  
 
for i in 1 2 3 4 5 ; do
 
for i in 1 2 3 4 5 ; do
     echo $i
+
     echo -n $i " "
 
     doubleIt $i
 
     doubleIt $i
 
     echo $?
 
     echo $?
    echo "-------------"
 
 
done
 
done
  
 
</source>
 
</source>
 +
<br />
 +
* The way the code above works, is that you call the function doubleIt first, passing it $i, then on the next line, you use '''$?''' to access the returned value of the function.  '''$?''' is the standard way bash accesses the ''status'' of the previous command that was executed, or the previous function that was called.  We will always use '''$?''' to access the value returned by the function called on the previous line.  That's the way bash works.
 +
<br />
  
 
=Solutions=
 
=Solutions=

Revision as of 14:19, 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.
  • You do not put the parameters inside the parenthesis, when declaring the function.
  • Here is an example, with both style functions:


#! /bin/bash
# func1.sh
# D. Thiebaut

printBar() {
    echo "------------------------"
}

function printName {
    echo "Hello $1"
}

printAge() {
    echo "Your age: $1"
}


printBar
printName "Kathleen McCartney"
printAge  61
printBar



Challenge 1

QuestionMark1.jpg


  • 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
------------------------





Challenge 2

QuestionMark2.jpg


  • Below is an incomplete bash script that implements the teller machine script we saw earlier. It prompts the user for an integer, and takes the number as a dollar amount that is broken into a number of $20-bills, $10-bills, $5-bills and $1-bills. You need to replace the XXXXXX symbols by the appropriate expression(s)...


#! /bin/bash
# funcTeller.sh
# D. Thiebaut
# Gets a number from the user and breaks it down
# into a number of $20, $10, $5, and $1

if [ "$#" -ne 1 ] ; then
    echo "Syntax  $0 nnnn"
    echo "where nnnn is a positive dollar amount"
    exit 0
fi


amount=$1

function printBills {
    if [ XXXXX -ne "0" ]; then
       echo "$1 $2-bill(s)"
    fi
}
 
function breakAmount {   
    no20s=$( expr XXXXX / 20 )
    amount=$( expr  $amount % 20 )
    no10s=$( expr  $amount / 10 )
    amount=$( expr  $amount % 10 )
    no5s=$( expr  $amount / 5 )
    no1s=$( expr  $amount % 5 )

    printBills $no20s XXXXX
    printBills XXXXX "10"
    printBills XXXXX "5"
    printBills $no1s XXXXX
}

breakAmount $amount


  • Here is an example of how it works:


cs231a@aurora ~/handout $ ./funcTeller.sh
Syntax  ./funcTeller.sh nnnn
where nnnn is a positive dollar amount
 
cs231a@aurora ~/handout $ ./funcTeller.sh 1234
61 20-bill(s)
1 10-bill(s)
4 1-bill(s)

cs231a@aurora ~/handout $


Bash Functions Returning Values


Bash has a strange (weird ?) way of implementing functions returning values. Let's observe the following example, that will print the following output:

1  2
2  4
3  6
4  8
5  10

Here's the code for this script:

#! /bin/bash
# func3.sh
# D. Thiebaut
# display 5 ints and their double.

function doubleIt {
    return $( expr $1 \* 2 )
}

for i in 1 2 3 4 5 ; do
    echo -n $i " "
    doubleIt $i
    echo $?
done


  • The way the code above works, is that you call the function doubleIt first, passing it $i, then on the next line, you use $? to access the returned value of the function. $? is the standard way bash accesses the status of the previous command that was executed, or the previous function that was called. We will always use $? to access the value returned by the function called on the previous line. That's the way bash works.


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


Challenge 2


#! /bin/bash
# funcTeller.sh
# D. Thiebaut
# Gets a number from the user and breaks it down
# into a number of $20, $10, $5, and $1

if [ "$#" -ne 1 ] ; then
    echo "Syntax  $0 nnnn"
    echo "where nnnn is a positive dollar amount"
    exit 0
fi


amount=$1

function printBills {
    if [ "$1" -ne "0" ]; then
       echo "$1 $2-bill(s)"
    fi
}
 
function breakAmount {   
    no20s=$( expr $1 / 20 )
    amount=$( expr  $amount % 20 )
    no10s=$( expr  $amount / 10 )
    amount=$( expr  $amount % 10 )
    no5s=$( expr  $amount / 5 )
    no1s=$( expr  $amount % 5 )

    printBills $no20s "20"
    printBills $no10s "10"
    printBills $no5s "5"
    printBills $no1s "1"
}

breakAmount $amount