Linux command: Practice if statement

Source: Internet
Author: User

1. Exercise: Write a script

determine if there is a user's default shell for bash on the current system;

If so, the number of such users is displayed, and one of the user names is displayed;

Otherwise, no such user is shown;

#!/bin/bash

# program

# Practice using the IF statement to determine the default shell

# History Level1 2016-10-19-14:00

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

Export PATH

grep "\<bash$"/etc/passwd &>/dev/null

Result= ' echo $? '

If [$RESULT-eq 0]; Then

bashin= ' grep ' \<bash$ '/etc/passwd | head-1 | cut-d:-f1 '

echo "The number of bash users is ' grep" \<bash$ "/etc/passwd | Wc-l ' "

echo "The one of is $BASHIN."

Else

echo "No such user"

Fi

Hint: "Reference" the execution result of a command to use a command reference; for example: resaults= ' wc-l/etc/passwd | cut-d:-f1 ';

using the execution status result of a command, it must not be referenced to execute the command directly, for example: if ID user1 the ID command in a sentence must not be quoted;

If you want to assign the result of a command to a variable, use a command reference, such as userid= ' Id-u user1 ';

If you want to save the execution status result of a command, and as a condition for the success or absence of a command execution, you need to execute this command before referencing its status results, such as

Id-u user1

Retval=$?

This sentence must not be written as retval= ' Id-uuser1 ';


2. Exercise: Write a script

Given a file, such as/etc/inittab

Determine if there is a blank line in this file;

If so, the number of blank lines is displayed, otherwise, no blank lines are displayed.

#!/bin/bash

# program

# Practice using the IF statement to determine the default shell

# History Level1 2016-10-19-14:20

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

Export PATH

grep "^$"/etc/inittab

Result= ' echo $? '

If [$RESULT-eq 0]; Then

echo "The lines is 'grep" ^$ "/etc/inittab | Wc-l ' "

Else

echo "The lines is not exist."

Fi

3. Exercise: Write a script

Given a user, determine if the UID is the same as the GID

If it does, the user is displayed as "good guy", otherwise the user is shown as "bad guy".

#!/bin/bash

# program

# Determine if the user's GID and UID are consistent

# History 2016-10-19-15:26

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

Export PATH

Name=user5

Useruid= ' Id-u $NAME ' (useruid= ' grep "\< $NAME \>"/etc/passwd | cut-d:-f3 ')

Groupgid= ' Id-g $NAME ' (useruid= ' grep "\< $NAME \>"/etc/passwd | cut-d:-f4 ')

If [$USERUID-eq $USERGID]; Then

echo "The $NAME is good guy."

Else

echo "The $NAME is bad guy."

Fi

4. Exercise: Write a script

Given a user, get their password warning period;

And then determine whether the user password period is less than the warning period;

Tip: The calculation method, the maximum period of use minus the number of days that have been used is the remaining period of use;

If it is less than, "Warning" is displayed; otherwise, "OK" is displayed.  

#!/bin/bash

# program

# Determine if the user's password age expires

# History Time 2016-10-19-16:18

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

Export PATH

Username=donggen

chaday= ' grep ' \< $USERNAME \> "/etc/shadow | cut-d:-f3 '

longday= ' grep ' \< $USERNAME \> "/etc/shadow | cut-d:-f5 '

warning= ' grep ' \< $USERNAME \> "/etc/shadow | cut-d:-f6 '

Let todaytimes= ' date +%s '/86400

Let usedtimes= $TODAYTIMES-$CHADAY

Let surplustimes= $LONGDAY-$USEDTIMES

If [$SURPLUSTIMES-lt $WARNING]; Then

echo "The Surplus times is warning!"

Else

echo "The Surplus times is ok!"

Fi


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

Linux command: Practice if statement

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.