码迷,mamicode.com
首页 > 其他好文 > 详细

bash腳本編程之三 条件判断及算数运算

时间:2017-09-18 18:18:37      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:bash腳本編程之三 条件判断及算数运算

练习: 写一个脚本,判断当前系统上是否有用户的默认shell为bash,如果有就显示有多少个这类的用户;否则,就显示没有这类的用户。

#!/bin/bash

#

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

retval=$?


if [ $retval -eq 0 ]

then

 users=`grep "\<bash" /etc/passwd | wc -l`

 echo "$users"

else

 echo "no such user."

fi


例:写一个脚本,判断当前系统上是否有用户的默认shell为bash,如果有就显示其中一个用户名,没有就显示没有这类的用户。

#!/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


例:给定一个用户,判断其UID与GID是否一样,如果一样就显示此用户为“good guy”,否则就显示是“bad guy”

#!/bin/bash

#

        username=user1

        userid=`id -u $username`

        groupid=`id -g $username`

if [ $userid -eq $grepid ]

then

        echo "good guy."

else

        echo "bad guy."

fi



shell中如何做算数运算:

A=3

B=6

1、let 算数运算表达式

    let c=$A+$B

2、$[算数运算表达式]

    C=$[$A+$B]

3、$((算数运算表达式))

    C=$(($A+$B))

4、expr 算数运算表达式,表达式中各操作数及运算符之间要有空格,而且要使用命令引用

    C=`expr $A + $B` 

本文出自 “10858195” 博客,请务必保留此出处http://10868195.blog.51cto.com/10858195/1966366

bash腳本編程之三 条件判断及算数运算

标签:bash腳本編程之三 条件判断及算数运算

原文地址:http://10868195.blog.51cto.com/10858195/1966366

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!