2 Simple shell scripts (if,while,case statements)

Source: Internet
Author: User
Tags case statement

Experimental requirements:

1. Write a script called sum.sh, prompting the user to enter an integer less than 100, and to calculate all integers from 1 to that number

2. Write a script called tar.sh, which is used to extract compressed files in. tar.gz or. tar.bz2 format, requiring a case statement


Experimental steps:

A

VI sum.sh


#!/bin/bash

# This is sum


While : #循环条件为真, equal to while true, loop is executed without exit 0 or break

Do

read-p "Please enter an integer of 1-100:" Nu #把屏幕输出的数字赋值给变量nu

expr $nu + 0 &>/dev/null #此条语句作用是判断这个数字是否为0或者不是整数 and does not display this number on the screen

if [$?-ne 0] | |  [$nu-ge] #如果上条语句是非0的结果则表示错误, or the number greater than or equal to 100 does not meet the experimental requirements

then echo "The $nu you entered does not meet the requirements, please re-enter:" #提示错误信息要求重新输入

Else #不是以上结果则执行下面的语句

s=0 i=1 #给变量初始化赋值

While [$i-le $nu] #因为当输入的数字小于100都要执行相加, so I stop the loop when I is greater than this number

Do

s=$ (expr $s + $i) #s从0开始, I start with 1, assign the result of each loop to s and then continue and I add, always add to the input integer

Let i++ #每循环一次i +1

Done

echo "1-$nu integer and is: $s" #将结果输出, Stop the Loop

Break

Fi

Done


Validation results:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/54/2E/wKioL1R7MfWAZ1CyAAB08G23yhM870.jpg "style=" float: none; "title=" 1.jpg "alt=" Wkiol1r7mfwaz1cyaab08g23yhm870.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/54/2F/wKiom1R7MW6S_F39AADdp8_Przk488.jpg "style=" float: none; "title=" 2.jpg "alt=" Wkiom1r7mw6s_f39aaddp8_przk488.jpg "/>


Two

VI tar.sh


#!/bin/bash

# This is test


read-p "Please enter the compressed file you want to unzip:" F #将输入的文件名称赋值给变量f

s=$ (File $f | awk ' {print $} ') #判断这个文件是哪种格式的压缩文件

Case $s in #若结果为gzip则用gzip解压

Gzip

echo "will unzip the file with the Gzip tool"

TAR-ZVXF $f

;;

bzip2) #若结果为bzip2则用bzip2解压

echo "will use the bZIP tool to extract the file"

TAR-JXVF $f

;;

*)   #其他结果则表示不是压缩文件, screen output error message

echo "is not a standard compressed file"

Esac


Validation results:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/54/2F/wKiom1R7MsfwhzwCAACua4h6qqQ132.jpg "title=" 3.jpg " alt= "Wkiom1r7msfwhzwcaacua4h6qqq132.jpg"/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/54/2E/wKioL1R7Mx-RZ0kdAAB1-lNr3n4072.jpg "style=" float: none; "title=" 4.jpg "alt=" Wkiol1r7mx-rz0kdaab1-lnr3n4072.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/54/2E/wKioL1R7MyDTmnA-AABPeP6JQWY959.jpg "title=" 6.jpg " Style= "Float:none;" alt= "wkiol1r7mydtmna-aabpep6jqwy959.jpg"/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/54/2F/wKiom1R7MpeQCCqaAAC3tXWzTrc537.jpg "style=" float: none; "title=" 5.jpg "alt=" Wkiom1r7mpeqccqaaac3txwztrc537.jpg "/>



This article is from the "Network growth path of the female kicked" blog, please make sure to keep this source http://shengjie.blog.51cto.com/8734352/1584881

2 Simple shell scripts (if,while,case statements)

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.