前端之家收集整理的这篇文章主要介绍了
shell函数使用方法,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
!/bin/bash
@H_404_6@function func1 {
@H_404_6@echo "This is an example of a function"
}
count=1
@H_404_6@while [ $count -le 5 ]
@H_404_6@do
func1
count=$[$count + 1 ]
@H_404_6@echo "count:" $count
done
@H_404_6@echo "This is the end of the loop"
func1
@H_404_6@echo "Now this is the end of the script"
@H_404_6@function db1 {
value=3
@H_404_6@echo $[ $value * 2 ]
}
result=$(db1)
@H_404_6@echo "The new value is $result"
function addem {
@H_404_6@if [ $# -eq 0 ] || [ $# -gt 2 ]
@H_404_6@then
echo -1
@H_404_6@elif [ $# -eq 1 ]
@H_404_6@then
echo $[ $1 + $1 ]
@H_404_6@else
echo $[ $1 + $2 ]
@H_404_6@fi
}
echo -n "Adding 10 and 15:"
value=$(addem 10 15)
echo $value
function badfunc1 {
echo $[ $1 * $2 ]
}
@H_404_6@if [ $# -eq 2 ]
@H_404_6@then
value=$(badfunc1 $1 $2)
echo "The result is $value"
@H_404_6@else
echo "Usage: badtest1 a b"
@H_404_6@fi
function func1 {
local temp=$[ $value + 5 ]
echo "The local temp is $temp"
result=$[ $temp *2 ]
}
temp=4
value=6
func1
echo "The result is $result"
echo "The global temp is $temp"
@H_404_6@if [ $temp -gt $value ]
@H_404_6@then
echo "temp is larger"
@H_404_6@else
echo "temp is smaller"
@H_404_6@fi
function testit {
echo "The parameters are: $@"
thisarray=$1
echo "The received array is ${thisarray[*]}"
}
myarray=(1 2 3 4 5)
echo "Ther original array is:${myarray[*]}"
testit $myarray
function testit {
echo "number:$#"
echo "argume:$@"
local newarray
newarray=(`echo "$@"`)
echo "The new array value is: ${newarray[*]}"
}
myarray=(1 2 3 4 5)
echo "The original array is ${myarray[*]}"
testit ${myarray[*]}
function addarray {
local sum=0
local newarray
newarray=($(echo "$@"))
@H_404_6@for value @H_404_6@in ${newarray[*]}
@H_404_6@do
sum=$[ $sum + $value ]
@H_404_6@done
echo $sum
}
myarray=(1 2 3 4 5)
echo "The original array is:${myarray[*]}"
arg1=$(echo ${myarray[*]})
result=$(addarray ${myarray[*]})
echo "The result is $result"
echo "------------------------------"
result=$(addarray $arg1)
echo "The result is $result"
原文链接:https://www.f2er.com/bash/387710.html