Shell exercises (10)

Source: Internet
Author: User

Exercise 1:User interaction Scripts

Requirements: write a script that, after execution, prints a line that says "Please input a number:", asks the user to enter a value, prints out the value, and then asks the user to enter the value again. Until the user enters "end" to stop.

Reference Answer:

#!/bin/bash# date:2018 March 5 while:d o read-p "Please input a number: (End for exit)" N num= ' echo $n |sed-r ' s/[0-9]//g ' | Wc-c ' if [$n = = "End"] then exit Elif [$num-ne 1] then echo "Please input a number. "Else echo" $n "Fidone

Exercise 2: script transfer

Requirements: a script is written using the method of subtraction, which realizes the function of the message. For example: SH a.sh 1 2, which calculates the result of addition, subtraction, multiplication, and division respectively.

1 The script needs to determine that the two numbers provided must be integers

2 when subtracting or dividing, you need to decide which number is large

3 numbers that need to be reduced with a large number when subtracting

4 division requires a large number divided by a small number, and the result needs to retain two decimal points.

Reference Answer:

#/bin/bash# date: 2018 March 5 If [ $# -ne 2 ];then    echo   "please usage: ./$0 num1 num2."     exit 1fiis_int () {    if echo  "$" |grep -q  [^0-9];then        echo  "$1 is not a  Number. "         exit 1    fi}max () {     if [ $1 -ge $2 ];then        echo  $1    else        echo $2     fi}min () {            if [ $1  -lt $2 ];then        echo $1     Else        echo $2&nbSp;   fi}sum () {    echo  "$1 + $2 = $[$1+$2]"}sub () {    large= ' max $1 $2 '     small= ' min $1 $2 '      echo  "$large  -  $small  = $[$large-$small]"}mul () {     echo  "$1 * $2 = $[$1*$2]"}div () {    large= ' max $1  $2 '     small= ' min $1 $2 '     d= ' echo  ' scale=2;   $large  /  $small "|BC     echo " $large  /  $small  = $ D "}is_int $1is_int $2sum $1 $2sub $1 $2mul $1 $2div $1 $2

Exercise 3: divisible by 3

Requirements: Write a script that calculates all positive integers within 100 that can be divisible by 3

Reference Answer:

#/bin/bash# date:2018 March 5 sum=0for i in ' seq 1 ' do if[$[$i%3]-eq 0];then sum=$[$sum + $i] Fidoneecho "su m= $sum "


Shell exercises (10)

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.