5.4shell Programming 3

Source: Internet
Author: User

5.4

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, otherwise, no such user is shown

Nano dd.sh

#!/bin/bash

#

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

RETVAL = $?


If [$RETVAL-eq 0];then

USERS = ' grep ' \<bash$ '/etc/passwd | Wc-l '

echo "The shells of $USERS USERS is bash."

Else

echo "No such user."

Fi


Exercise: Write a script

Determine if a user's default shell is bash on the current system

If so, displays one of the user names, otherwise, it shows no such user

Nano dd.sh

#!/bin/bash

#

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

RETVAL = $?


If [$RETVAL-eq 0];then

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

echo "$USERS is one of such USERS"

Else

echo "No such user."

Fi



Exercise: Write a script

Given a file, such as/etc/inittab

Determine if there is a blank line in this file

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

#!/bin/bash

FILE =/etc/inittab

If grep "^$" $FILE &>/dev/null;then

echo "Total Blank lines: ' grep" ^$ "$FILE | Wc-l '. "

Else

echo "No blank line."

Fi


Exercise: Write a script

Given a user, determine if its UID and GID are the same

If it does, the user is displayed as "good guy";

#! /bin/bash

USERNAME =user1

USERID = ' Id-u $USERNAME '

GROUPID = ' Id-g $USERNAME '

if[$USERID-eq $GROUPID];then

echo "Good guy"

Else

echo "Bad guy"

Fi

Further requirements: Do not use the ID command to obtain its ID number


Exercise: Writing a script

Given a user, get their password warning period:

And then determine whether the last time the user changed the password is today less than the warning period

Hint: The method of arithmetic operation $[$A-$B]: The result of the value of variable A minus the value of variable B

If it is less than, "Warning" is displayed: otherwise, "OK" is displayed

Exercise: Write a script

The total entry of the history command in the Interpretation command history is greater than 1000;

"Some command would gone."; Otherwise, "OK" is displayed


How to perform arithmetic operations in the shell:

A = 3

B = 6

1 Let arithmetic op-expression

Let C = $A + $B

2 $[arithmetic operation expression]

c = $[$A + $B]

3 $ ((arithmetic expression))

c = $ (($A + $B))

4 The Expr command operation expression, with spaces between the operands and operators in the expression, and

Using a command reference

c = ' expr $A + $B '


5.4shell Programming 3

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.