Topic One: Summation exercise:
#!/bin/bash
function Sumarray {
Local sum=0
Local array
Array= (
echo "[email protected]"
)
For value in ${array[
]}
Do
sum=$[$sum + $value]
Done
Echo $sum
}
Wang= (1 2 3 4 5)
echo "Initial data is: ${wang[]}"
Kai=
echo ${wang[*]}
result=
sumarray $kai
echo "result is: $result"
Exit 0
Analysis: In this topic I pay attention to the practice of the definition and use of functions, as well as the use of arrays, these are the key points of the shell programming difficulties.
I have made mistakes in this topic: arrays are not enlarged brackets, the array is defined without parentheses; these should be noted!
Topic Two: Doubling the array after splitting the output of the merge
#!/bin/bash
function array {
Local array1= (
echo "[email protected]"
)
Local array2= (
echo "[email protected]"
)
Local I
Local Elements=$[$#-1]
for ((i=0; i<= $elements; i++))
{
array2[$i]=$[${array1[$i]}
2]
}
echo "${array2[]}"
}
Wang= (1 2 3 4 5)
echo "Initial data is: ${wang[
]}"
Kai= ' echo ${wang[]}
<br/>result=
Array $kai
<br/>echo "结果是:${result[*]}"<br/>![](http://i2.51cto.com/images/blog/201801/30/127f2176aaf433ae52d6201db2a8fae2.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)<br/>解析: 这道题目考察点是围绕着数组的处理来的,不仅将数组拆分成单个变量,也要做到将数组合并为整体输出。<br/>第三题:求阶乘<br/>#!/bin/bash<br/>function jj {<br/>if [ $1 -eq 1 ];then<br/>echo 1<br/>else<br/>local temp=$[$1-1]<br/>local result=
JJ $temp
<br/>echo $[$result * $1]<br/>fi<br/>}<br/>read -p "请输入数字:" number<br/>result1=
JJ $number '
echo "The result is
Analysis: The subject is used to investigate the recursive function, the difficulty is not very difficult.
Shell Programming Exercises