A detailed description of the arithmetic operators for Bash programming under Linux (iii)

Source: Internet
Author: User
Tags arithmetic arithmetic operators

A detailed description of the arithmetic operators for Bash programming under Linux (iii)

The shell arithmetic operation is represented as follows:

1. Let arithmetic operation expression

Let c= $A + $B

2. $[arithmetic operation expression]

c=$[$A + $B]

3, $ (arithmetic operation expression)

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

4. Expr must have a space between each operand and operator, and you want to use a command reference

c= ' expr $A + $B '

To illustrate:

1. Specify a user to determine whether the remaining lifetime of the user's password is less than the warning period;

Show "Password will expire-Warning" if it is less than the warning period; otherwise, "OK" is displayed

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

Vim shadowdate.sh Create a script file and add the following:

#!/bin/bash

Username=willow Specify a user and set the variable

Rece= ' grep ' \< $USERNAME \> "/etc/shadow | cut-d:-f3 ' recent password change time

long= ' grep ' \< $USERNAME \> "/etc/shadow | cut-d:-f5 ' Maximum password usage period

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

Curr= ' Date +%s ' from 1970/01/01 to today, total number of seconds

Let today= $CURR/86400 from 1970/01/01 to today, how many days


#let userdays= $TODAY-$RECE notes: How many days a password has been used one of the table method methods

#USERDAYS =$[$TODAY-$RECE] Notes: How many days the password has been used one of the two table method

userdays=$ (($TODAY-$RECE)) password has been used for how many days one of the three table method


left= ' expr $LONG-$USERDAYS ' remaining period of use


If [$LEFT-lt $EXPI]; Then

echo "Password'll expire-Warning"

Else

echo "Password is OK"

Fi

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/FD/wKiom1cPA2uDae_9AAJfGcvYkYg712.jpg "title=" Shadowdate.jpg "alt=" Wkiom1cpa2udae_9aajfgcvykyg712.jpg "/>

chmod a+x shadowdate.sh granted execution rights

./shadowdate.sh Execute Script

This article is from the "Xavier Willow" blog, please be sure to keep this source http://willow.blog.51cto.com/6574604/1763691

A detailed description of the arithmetic operators for Bash programming under Linux (iii)

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.