Linux Learning Summary (66) a script that prints a bunch of numbers

Source: Internet
Author: User

Write a shell script that prompts for a paused number and then prints from 1 to that number. Then ask if you want to continue. Continue to enter a number and then print, or exit the script.
For example: If the input is 5, print 1 2 3 4 5, then continue to enter 10, and then print 6 7 8 9 101 times and so on.

Topic Analysis:
1. Prompt for user input, with read-p
2. To make a judgment on the user's input, whether it is a pure number. If not, give a hint and exit directly.
3 after the first printing results, if the user continues to enter the number, at this time not only to determine whether the user input is a pure number, but also to determine whether the number is larger than the first input.
4 to continue entering a number, you need to add a while loop to the second operation.
The overall framework is

judge() {  判断输入的数字是否为纯数字}read -p  “输入一个数字” number1judge $number1for 循环依次打印出结果while :;doread -p "是否继续输入数字? (yes or no)" a   case $a inyes)read  -p "请输入您想要暂停的数字(要大于$number1):" number2judege $number2if 判断number2 是否大于number1,大于循环打印结果,不然退出最后把number2 赋值给number1no)*)done

The complete code is

#!/bin/bashjudge_number()    //判断数字是否为纯数字{if [ -z "$1" ];then    echo "你必须输入一个数。"    exitfin=`echo $1|sed ‘s/[0-9]//g‘`if [ -n "$n" ];then    echo "你输入的数字有误"    exitfi}read -p  "请输入你想要暂停的数字:" number1judge_number $number1for i in `seq 1 $number1`;do    echo $idonewhile :;doread -p "是否继续输入数字?(输入yes或者no)" acase $a in    yes|YES)    read -p "请继续输入您想要暂停的数字(要大于$number1):" number2    judge_number $number2    if [ $number2 -le $number1 ];then        echo "请输入一个比$number1大的数字。"        exit    fi    number3=$[$mumber1+1]    for h in `seq $number3 $number2`;do        echo $h    done    number1=$number2    ;;    no|NO)    exit    ;;    *)    echo "请输入yes或者no"    exit    ;;esacdone

Linux Learning Summary (66) a script that prints a bunch of numbers

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.