Day03 Script Exercises

Source: Internet
Author: User

1, with while loop output number 1 to 10 (descending output)

Output number 35 to 50 with while loop (ascending output)

#!/bin/bash

j=10

I=35

While [$j-ge 1]

Do

Echo$j

Let j--

Done

While [50-ge $i]

Do

Echo$i

Let i++

Done



2, Batch add 15 system user requirements as follows

User name has the same password as the user name

User name prefix is user, username format is as follows

User01 user02 ... user09 user10 ... user15

Force the newly added user to reset the password of their login system when they first log in


#!/bin/basha

I=useradd

U=user0

J=1

O=user

k=10

While [9-ge $j]

Do

$i $u $j

echo "$u $j" | passwd--stdin $u $j &>/dev/null

chage-d 0 $u $j

Let J + +

Done

While [15-ge $k]

Do

$i $o $k

echo "$o $k" | passwd--stdin $o $k &>/dev/null

chage-d 0 $o $k

Let k++

Done



3. Write Script userdel.sh (delete the 15 system users just added in bulk)

#!/bin/basha

U=user0

J=1

O=user

k=10

While [9-ge $j]

Do

Userdel-r $u $j

Let J + +

Done

While [15-ge $k]

Do

Userdel-r $o $k

Let k++

Done


4, useradd.sh (interactive batch add system users)

The functions are as follows:

1, you can set the number of users added, the user's password, the user's home directory, the user's validity period

User Prefix


#!/bin/bash

Read-p "Please enter the number of users you want to add:" YH

If [-Z $yh];then

echo "Quantity cannot be empty"

Exit

Fi

I=1

While [$yh-ge $i]

Do

Read-p "Please enter the location where the user's home directory is stored, such as:/home/xxx:" Mulu

Read-p "Please enter your user name:" Name

useradd-d $mulu $name

passwd $name

Read-p "Please enter your user's expiration date such as: xxxx-xx-xx:" Yxq

CHAGE-E $YXQ $name

Let i++

Done

Or


#!/bin/bash

Read-p "Please enter the number of users:" "Usernum"

Read-p "Please enter user name:" username

Read-p "Please enter your password:" Userpas

If [-Z $usernum] | | [-Z $username] | | [-Z $useraps];then

echo "Input incomplete, auto exit"

Else

I=1

While [$i-le $usernum]

Do

Useradd $username $i

echo $userpas | passwd--stdin $username &>/dev/null

Let i++

Done

Fi


5, with (c-for) batch Add 15 system user requirements as follows

User name has the same password as the user name

User name prefix is user, username format is as follows

User01 user02 ... user09 user10 ... user15

Force the newly added user to reset the password of their login system when they first log in

#!/bin/bash


For ((i=1;i<=15;i++))

Do

if[$i-lt];then

Useradd user0$i

echo "user-$i" | passwd--stdin user0$i

chage-d 0 user0$i

Else

Useradd user$i

echo "User$i" | passwd--stdin user$i

chage-d 0 user$i

Fi

Done



6. Write script with c-for (delete 15 users added in bulk)

#!/bin/bash

For ((i=1;i<=9;i+=))

Do

Userdel-r user0$i

Done

For ((i=10;i<=15;i++))

Do

Userdel-r user$i

Done



7, with until loop structure output value 1-5

#!/bin/bash

I=1

Until [$i-GT 5]

Do

Echo $i

Let i++

Done



8, four.sh (in a script with four kinds of loop structure output number 8 to 12)

#!/bin/bash


#for循环

For i in ' seq 8 12 '

Do

Echo $i

Done


#while循环

I=8

While [$i-le 12]

Do

Echo $i

Let i++

Done


#C-for Cycle

For ((i=8;i<=12;i++))

Do

Echo $i

Done


#until循环

I=8

Until [$i-GT 12]

Do

Echo $i

Let i++

Done



9, check_gatewy.sh

(A warning message is sent to the terminal when the gateway address used by the current server is unavailable)

Using Gateway Error

echo "Using gateway Error" >/dev/tty1

echo "Using gateway Error" >/DEV/PTS/1

Write


#!/bin/bash


Wg= ' Route-n | Tail-1 | awk ' {print $} '


Ping-c 3 $WG &>/dev/null


If [$?-eq 0];then

echo "Gateway normal"

Else

echo "Using gateway Error" >/dev/tty1

echo "Using gateway Error" >/DEV/PTS/1

Fi


1.100 Route Netcom

10, auto_gw.sh, automatically switch the gateway of the current host

192.168.1.0/24------iptables---

1.2511.254

Route Telecom


#!/bin/bash


gw= ' Route-n | Tail-1 | awk ' {print $} '


While:

Do

Ping-c 3 $GW &>/dev/null


If [$?-ne 0];then

Route add default GW 192.168.1.254 &>/dev/null

Route del default GW 192.168.1.100 &>/dev/null

Else

Route add default GW 192.168.1.100 &>/dev/null

Route del default GW 192.168.1.254 &>/dev/null

Fi

Done



11, if the system is Monday to Friday on the output working days

If the system is Saturday, the date of the appointment is output

If the system is Sunday, the output of the rest day

#!/bin/bash

Read-p "Please lose your choice of week Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday "Xq

Case $XQ in

Monday

echo "Today is working day";;

Tuesday

echo "Today is working day";;

Wednesday

echo "Today is working day";;

Thursday

echo "Today is working day";;

Friday

echo "Today is working day";;

Saturday

echo "Today is Date Day";

Sunday

echo "Today is day off";

*)

echo "$ can only choose one of the Mondays here | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday

Esac


12, very_num_dj.sh (the product of the output of any number multiplication)

#!/bin/bash

I=1

While [$#-GT 0]

Do

i= ' expr $i \* $ '

Shift

Done

Echo $i



13. very_num_shang.sh (quotient of dividing any number of outputs)


#!/bin/bash

I= ' expr $ \* $ '

While [$#-GT 0]

Do

i= ' expr $i/$ '

Shift

Done

Echo $i


14, very_num_shang.sh (the difference between the output of any number subtraction)


#!/bin/bash

i= ' expr 2 \* $ '

While [$#-GT 0]

Do

i= ' expr $i-$ '

Shift

Done

Echo $i



15, very_num_shang.sh (the sum of any number of outputs)

#!/bin/bash

I=0

While [$#-GT 0]

Do

i= ' expr $i + $ '

Shift

Done

Echo $i


Or


#!bin/bash

One=$1

Shift

While [$#-GT 0]

Do

Let one+=$1

Shift

Done

Echo $one


This article is from the "Wsyht blog" blog, make sure to keep this source http://wsyht2015.blog.51cto.com/9014030/1790412

Day03 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.