Bash Scripting 3 conditional judgments and arithmetic operations (notes)

Source: Internet
Author: User

Little exercise: Write a script

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

If there is one, it shows how many such users will otherwise show no such user


#!/bin/bash
#
grep "bash$"/etc/passwd &>/devnull
Code=$?

If [$CODE-eq 0]; Then
usernu= ' grep ' bash$ '/etc/passwd | Wc-l '
echo "The number of Bash shell user is $USERNU" English not well don't know right hehe
Else
echo "The system is not a user of bash shell." English is not very good don't know right hehe

Fi


Script Execution Results

[Email protected] ~]#./first.sh
The number of Bash shell user is 8


Exercise two:

    1. To see if the system has a user's default shell is bash

    2. There is no indication that one of them is displayed on the system.

scripting, similar to the previous script

#!/bin/bash
#
grep "bash$"/etc/passwd &>/devnull
Code=$?

If [$CODE-eq 0]; Then
usernu= ' grep ' bash$ '/etc/passwd | head-n1 | cut-d:-f1 '
echo "$USERNU is one of bash shell users."
Else
echo "The system is not a user of bash shell."
Fi

Script Execution Results:

[Email protected] ~]#
[Email protected] ~]#./first.sh
Root is one of the bash shell users.
[Email protected] ~]#


How to perform arithmetic operations in the shell (note: The default shell treats variables as characters and does not perform arithmetic operations)

Cases:

[Email protected] ~]# a=3
[Email protected] ~]# b=6
[Email protected] ~]# c= $A + $B
[Email protected] ~]# echo $C
3+6
[Email protected] ~]#

If you want it to do arithmetic operations:


The first way: use let plus the variable to be operated on

Cases:

[Email protected] ~]# a=3
[Email protected] ~]# b=6

[[email protected] ~]# let c= $A + $B
[Email protected] ~]# echo $C
9
[Email protected] ~]#


Second way: Use $[arithmetic expression]

Cases:

[Email protected] ~]# a=25
[Email protected] ~]# b=88
[Email protected] ~]# c=$[$A + $B]
[Email protected] ~]# echo $C
113
[Email protected] ~]#


Third Way: Using $ ((arithmetic expression))

Cases:

[Email protected] ~]# a=222
[Email protected] ~]# b=381
[[email protected] ~]# c=$ (($A + $B))
[Email protected] ~]# echo $C
603
[Email protected] ~]#


Fourth way: Use expr arithmetic expressions Note that there are spaces between the operands in the expression, and that you want to use a command reference

Cases:

[Email protected] ~]# a=3244
[Email protected] ~]# b=38234
[[email protected] ~]# c= ' expr $A + $B ' note to use a space to separate the HA
[Email protected] ~]# echo $C
41478
[Email protected] ~]#

Bash Scripting 3 conditional judgments and arithmetic operations (notes)

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.