Shell learning: Several common shell exercises

Source: Internet
Author: User

1. Write the shell script and calculate the 1-100;

2.   writing The script requires a number to be entered and then calculated from 1 to enter the number and, if required, if the number entered is less than

3.   writing Script, put /root/ all directories (only one level) are copied to /tmp/ directory;

4.   writing Script, Batch build user and all users belong to users

5.   writing Scripts, intercepting files test.log

6.   writing Script, judging the input of the ip The rules are, , of which 1<n1<255, 0<n2<255, 0<n3<255, 0<n4 <255)

Here are the answers to the exercises:

1. #! /bin/bash

Sum=0

For i in ' seq 1 100 '; Do

sum=$[$i + $sum]

Done

Echo $sum

2. #! /bin/bash

N=0

While [$n-lt "1"]; Do

Read-p "Please input a number, it must greater than" 1 ":" N

Done

Sum=0

For i in ' seq 1 $n '; Do

sum=$[$i + $sum]

Done

Echo $sum

3. #! /bin/bash

For f in ' ls/root/'; Do

If [-D $f]; Then

Cp-r $f/tmp/

Fi

Done

4. #! /bin/bash

Groupadd Users

For i in ' seq 0 9 '; Do

Useradd-g Users User_0$i

Done

For j in ' seq 10 100 '; Do

Useradd-g Users User_$j

Done

5. #! /bin/bash

Awk-f ': ' $0~/abc/{print '} ' Test.log >/tmp/n.txt

Sort-n n.txt |uniq-c |sort-n >/tmp/n2.txt

awk ' $1>10 {print $/tmp/n2.txt} '

6. #! /bin/bash

Checkip () {

If echo $ |egrep-q ' ^[0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\. [0-9] {1,3}$ '; Then

A= ' echo $ | Awk-f. ' {print '} '

B= ' echo $ | Awk-f. ' {print $} '

C= ' echo $ | Awk-f. ' {print $} '

D= ' echo $ | Awk-f. ' {print $4} '

For n $a $b $c $d; Do

If [$n-ge 255] | | [$n-le 0]; Then

echo "The number of the IP should less than 255 and greate than 0"

Return 2

Fi

Done

Else

echo "The IP you are something wrong, the format is like 192.168.100.1"

Return 1

Fi

}

Rs=1

While [$rs-GT 0]; Do

Read-p "Please input the IP:" IP

Checkip $ip

Rs= ' echo $? '

Done

echo "The IP is right!"

Shell learning: Several common shell exercises

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.