Unix an Linux scripts - Open source guru JiUnix an Linux scripts - Open source guru Ji

Creating Function in Linux scripting, Do you know how to create function. Here is few example which will be directly explained you.  Details of every parameter will be discussed in below functions. Lets learn some real time basic script to understand the procedure to create functions in Linux.

1. Create a function in Linux Script which will tell us the average of total number.

Note:

=>  $# : This will be refer to the total number of command line which arguments passed into. So you can use $# to check/validate the number of arguments/parameters passed and you can also handle the unexpected situations.


find_avg(){
len=$#
sum=0
for x in "$@"
do
sum=$((sum + x))
done
avg=$((sum/len))
return $avg
}
find_avg 30 50 50 60
printf "%f" "$?"
printf "\n"

One thought on “Creating Function in Linux Script | Linux Script Function”

Leave a Reply

Your email address will not be published. Required fields are marked *

Netflix’s Overall Architecture. Gold is money every thing else is credit