Shell Programming Exercises

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.