Basic shell programming

Source: Internet
Author: User

I used shell to write it. At that time, I had a little understanding of it. Now I completely forgot it. But I still can understand it, so I won't write it. They are all simple questions assigned by teachers.

 

1. Select addition, subtraction, multiplication, division, and enter two numbers and results to check whether the user input results are correct.

 #! /Bin/bash <br/> # This is the first homework <br/> echo "input 1 to start and other numbers to exit: "<br/> Read sig <br/> while [$ sig-EQ 1] <br/> DO <br/> echo-n" Please choose (+ ,-, *,/) "<br/> read operation <br/> echo-n" Please input the 2 numbers to calculate: "<br/> Read a B <br/> declare C <br/> echo-n" Please input the result: "<br/> Read result <br/> Mul = * <br/> If [" $ operation "=" $ Mul "] <br/> T Export c = 'expr $ A/* $ B '<br/> Elif [$ operation = +] <br/> then c = 'expr $ A + $ B' <br /> Elif [$ operation =-] <br/> then c = 'expr $ A-$ B '<br/> Elif [$ operation =/] <br/> then C = 'expr $ A/$ B '<br/> else <br/> echo "error" <br/> Exit 1 <br/> fi <br/> If [$ result-EQ $ C] <br/> then Echo "you are right! "<Br/> else echo" You are wrong, the right answer is $ C! "<Br/> fi <br/> echo" input 1 to start again and any other numbers to exit "<br/> Read sig <br/> done <br/> echo "goodbye!" 

 

2. Sort the three input numbers in ascending order.

#! /Bin/bash <br/> # This is the second homework <br/> function order <br/> {<br/> If [$ C-GT $ A] <br /> then <br/> T = $ C <br/> C = $ A <br/> A = $ T <br/> fi <br/> If [$ B -GT $ A] <br/> then <br/> T = $ A <br/> A = $ B <br/> B = $ T <br/> fi <br/> If [$ C-GT $ B] <br/> then <br/> T = $ C <br/> C = $ B <br/> B = $ T <br/> fi <br/>}</P> <p> echo-n "Please input 3 numbers: "<br/> Read a B c <br/> order a B c <br/> echo" $ A $ B $ C "<br/>

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.