6.1 Shell Programming 4

Source: Internet
Author: User

6.1

Given a user, determine if its UID and GID are the same "^ $USERNAME \>" (first word at the beginning of the line)

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

#!/bin/bash

#

USERNAME =user1

If!grep "^ $USERNAME \>"/etc/passwd &>/dev/null;then user does not exist

echo "No such User: $USERNAME."

Exit 1

Fi


USERID = ' Id-u $USERNAME '

GROUPID = ' Id-g $USERNAME '

if[$USERID-eq $GROUPID];then

echo "Good guy"

Else

echo "Bad guy"

Fi

Exit: Exit Script

Exit # (#代表数字)


Common conditions in bash test three kinds of

-gt

-le

-ne

-ge

-lt

-eq not 0 value is False

--------------------only the above can be used [], other expressions cannot appear []

INT1 = 63

INT2 = 77

[$INT 1-eq $INT 2]

[[$INT 1-eq $INT 2]]

TEST $INT 1-eq $INT 2


If [' grep ' ^ $USENAME \> "/etc/passwd | cut-d:-f3 '-eq 0];then to

If [' grep ' ^ $USENAME \> "/etc/passwd '-eq 0];then wrong (front is a row)

Test method

[Expression] Bash commands

[[expression]] bash keyword

Test expression


File test:

-E File: Test whether a document exists

-F File: Test files are normal files

-D FILE: Tests whether the specified path is a directory

-R File: Tests whether the current user has Read permission to the specified file

-W

-X FILE


[-e/etc/initttab]

[-x/etc/rc.d/rc.sysinit]


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 [!-e $FILE];then

echo "No $FILE."

Exit 8

Fi


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

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

Else

echo "No blank line."

Fi


Rounding, discarding the content after the decimal point

The expr command is generally used for integer values

#expr argument operator argument


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

#!/bin/bash

#

w= ' grep ' Student '/etc/shadow | cut-d:-f6 '

s= ' Date +%s '

t= ' expr $S/86400 '

L= ' grep ' ^student '/etc/shadow | cut-d:-f5 '

n= ' grep ' ^student '/etc/shadow | cut-d:-f3 '

sy=$[$L-$[$T-$N]


If [$SY-lt $W];then

Echo ' worning '

Else

Echo ' OK '

Fi


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

History the number of bars in the current display buffer (will be greater than 1000)

$HISTSIZE only show 1000

$HISTFILESIZE only show 1000

History | Tail-l | Cut-d '-f1 because there is a space in front, the number of bars cannot be taken, when taking the second field

History | Tail-l | Cut-d '-f2

#!/bin/bash

#

COUNT = History | Tail-l | Cut-d '-f2

If $COUNT-gt; then

echo "Some command would gone."

Else

echo "OK"

Fi


Multi-Branch If statement

If judging condition 1;then

Statement1

...

Elif Judging Condition 2;then

Statement2

...

Elif Judging Condition 3;then

Statement3

...

Else Judging condition 4;then

Statement4

...

Fi

#!/bin/bash

#

FILE =/etc/inittab


If [-e $FILE];then

echo "OK"

Else

echo "No such file"

Fi


Test script for syntax errors

Bash-n filetest.sh


Bash-x Script: Stepping


Given a file, if it is an ordinary file, it displays the

If it is a directory, it also displays the

Otherwise, this is an unrecognized file

filetest2.sh

#!/bin/bash

#

FILE =/etc/rc.d/rc.sysinit


if [!-e $FILE];then

echo "NO such file"

Exit 6

Fi


If [-F $FILE];then

echo "Common file"

elif [-D $FILE];then

echo "Directory"

Else

echo "Unkown."

Fi


Defining script Exit Status codes

If the script does not explicitly define an exit status code, then the last command executed

Exit code is the exit status code of the script.


Types of Bash variables:

Local variable (local variable) current shell process

Environment variables current shell process and its child processes


Position variable: $ ...

Shift (kick off a position value) default kick One

Shift [n] kicks off n

Special variable $?

$#: Number of parameters

$*: Parameter list

[email protected]: parameter list


./filetest.sh/etc/fstab/etc/inittab

$1:/etc/fstab

$2:/etc/inittab


Exercise: Write a script

can accept a parameter (file path)

The decision: If this parameter is an existing file, it will show OK, otherwise no such file

#!/bin/bash

#

Echo $#

Echo $*

echo [email protected]

if[$#-lt 1];then

echo "Usage:./filetest3.sh ARG1 [ARG2 ...]"

If [-E $];then

echo "OK"

Else

echo "No such file"

Fi


Exercise: Write a script

Passing two parameters (integers) to a script

Shows the sum of the two parameters, the product of

#!/bin/bash

#

If [$#-lt 2];then

echo "Usage:cac1.sh ARG1 ARG2"

Exit

Fi

echo "The sum is: $[$1 + $ [$]."

echo "The PROD is: $[$1 * $]."
























6.1 Shell Programming 4

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.