Linux Programming _shell script exercises

Source: Internet
Author: User

1, write the shell script, calculate the 1~100 and.
#! /bin/bashsum=0 for in 'seq1';  do        sum=$[$i +$sum]doneecho $sum

2, write the shell script, enter a number n and calculate the 1~n.

Requirement: If the number entered is less than 1, re-enter until the correct number is entered.

#! /bin/Bashn=0 while[$n-lt"1"]; DoRead-P"Please input a number:"N Donesum=0 forIinch`seq 1$n '; Do        sum=$[$i +$sum] DoneEcho$sum

3, write shell script, batch build user user_00, user_01...user_99.

Requirement: All users belong to the Users group.

#! /bin/bashgroupadd users for in 'seq -w0";  Do -Gusers user_0$i4done         

4, write Shell script, batch add user jsj01-jsj09, jsj10-jsj99.
#! /bin/bashfor ((i=1;i<; i++));  Do if (i<);  Then JSJ="jsj0$i"; Else JSJ="jsj$i"; fi Useradd $JSJ  Done

5, write the shell script, require the following function: When executing a program, this program will let the user choose Boy or girl, if the user enters B or B, it is shown: he is a boy; if the user enters G or G, it is displayed: he is a girl If it is a character other than b/b/g/g, it is displayed: I don ' t know.
#! /bin/bashecho -n your choice:read choicecase in[g,g]Echo  "She is a girl. " "  "He's a boy. " " ;; *)echo"I don t know. " " ;; Esac

6, write the shell script, implement the subtraction operation between the two variables.
#! /bin/Bashread-P"imput NUM1:"Aread-P"input num2:"Bread-P"input Operator:"o Case$oinch+) Let"res=a+b"Echo$res;;-) Let"Res=a-b"Echo$res;;/)awk'begin{printf%.2f\n ",'$a'/'$b'}';;*) Let"res=a*b"Echo$res;;Esac

Linux Programming _shell script exercises

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.