Linux command exercise: For Loop statement practice

Source: Internet
Author: User

Tag: Extract the user under the shell and display the relevant user name

Practice One:

Write a script

1, add 10 users user1 to User10, password with the user name, but requires only the user does not exist in the case can be added;

Extension: accepts a parameter:

Add: Adding User user1. User10

Del: Delete User user1. User10

Other, exit


#!/bin/bash

#program:

#练习添加10个用户

#history Donggen 2016-10-21-22:10

Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash

Export PATH

if [= = = "Add"]; Then

For I in {1..10}; Do

If ID user$i &>/dev/null; then

echo "The user$i is exist"

Else

Useradd user$i

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

echo "User$i is add successful"

Fi

Done

elif [= = = "Del"]; Then

For I in {1..10}; Do

If ID user$i &>/dev/null; Then

Userdel-r user$i

echo "User$i is del successful"

Else

echo "NO such user$i"

Fi

Done

Else

echo "The $ not legol command"

Fi

Exercise two:

Write a script

Calculates the total of all positive integers within 100 that can be divisible by 3, modulo, remainder:%

#!/bin/bash

#program:

#计算100以内所有能被三整除的正整数的和

#history Donggen 2016-10-21-23:10

Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash

Export PATH

Declare-i sum=0

For I in {1..100}; Do

Let remainder= $I%3

If [$REMAINDER-eq 0]; Then

Let sum= $SUM + $I

Fi

Done

echo "$SUM"


Exercise Three:

Write a script

Shows all default shell-bash users and default shell-/sbin/nologin users on the current system, respectively.

and statistics of the total number of Shell users, showing the results are as follows:

Bash,3users,they Are:root,redhat,gentoo

Nologin,2users,they are:bin,ftp

#!/bin/bash

#program:

#显示当前shell下面的用户

#history Donggen 2016-10-21-23:40

Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash

Export PATH

bashnum= ' grep '/bash\> '/etc/passwd | Wc-l '

nologinnum= ' grep '/nologin\> '/etc/passwd | Wc-l '

For I in ' seq 1 $BASHNUM '; Do

echo "' grep '/bash\> '/etc/passwd | Head-n $I | Tail-1 | cut-d:-f1 ' ">>/test/bash.txt

Done

For I in ' seq 1 $NOLOGINNUM '; Do

echo "' grep '/nologin\> '/etc/passwd | Head-n $I | Tail-1 | cut-d:-f1 ' ">>/test/nologin.txt

Done

echo "BASH," $BASHNUM "Users,they is:" ' Cat/test/bash.txt '

echo "Nologin," $NOLOGINNUM "Users,they is:" ' Cat/test/nologin.txt '


This article is from the "Learn Linux history" blog, please be sure to keep this source http://woyaoxuelinux.blog.51cto.com/5663865/1864438

Linux command exercise: For Loop statement practice

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.