1、写一个脚本:创建一个用户little,如果用户已经存在,就提示用户已经存在的信息,否则将创建用户。

答:

!/bin/bash

#创建用户

#

id $1 &> /dev/null && echo "$1 is exist" || useradd $1


[root@little ~]# bash 1.sh little

little is exist


2、写一个脚本,列出系统中默认shell的bash用户。

答:

#!/bin/bash

#找出bash用户

#

if grep -q "bash$" /etc/passwd; then

  grep "bash$" /etc/passwd | cut -d ":" -f1

fi


[root@little ~]# bash 2.sh

root

little

mysql

ling



3、创建一个用户,如果用户已经存在,就提示用户已经存在的信息,否则将创建用户。

答:

#!/bin/bash

#判断用户并创建

#

if id $1 &> /dev/null; then

  echo "$1 is exist"

else

  useradd $1

  echo $1 | passwd --stdin $1

  echo "$1 is create seccessfully"

fi


[root@little ~]# bash 3.sh ling

ling is exist


4、写一个脚本,给脚本传递用户名参数,判断参数数量是否合格,并且判断用户是否存在,如果存在,就显示相应信息,否则就创建之并为其设置密码。

答:

#!/bin/bash

#创建用户和密码

#

if [ $# -ne 1 ]; then

  echo "You can only input one username"

  exit

fi


if id $1 &> /dev/null; then

  echo "$1 is exist"

else

  useradd $1

  echo $1 | passwd --stdin $1

  echo "$1 is create successfully"

fi


[root@little ~]# bash 4.sh ling

ling is exist



5、read脚本。

答:

#/bin/bash

#read函数

#

read -p "please inpout one option: "


[root@little ~]# bash 5.sh 

please inpout one option: ^C


6、写一个脚本,能够添加或删除用户账户,可以使用-a选项完成添加,使用-d选项完成删除任务。

答:

#!/bin/bash

#添加删除用户

#

if [ $# -ne 2 ]; then

  echo "Usage: $(basename $0) -a username | -d username"

  exit

fi


if [ $1 == '-a' ]; then

   if id $2 &> /dev/null; then

     echo "$2 is exist"

   else

     useradd $2

     echo $2 | passwd --stdin $2

     echo "$2 is add successfully"

   fi

fi


if [ $1 == '-d' ]; then

  if id $2 &> /dev/null; then

    userdel -r $2

    echo "$2 is delete"

  else

    echo "$2 is not exist"

  fi

fi


[root@little ~]# bash 6.sh -a ling

ling is exist


7:判断给出的文件大小是否大于100kb,如果大于100kb,就显示这是个大文件,否则就显示这是一个小文件。

答:

#!/bin/bash

#判断文件大小

#

if ! id $1 &> /dev/null; then

  echo "$1 is not exist"

  exit

fi


filesize=$(wc -c < $1)

if [ $filesize -gt 102400 ]; then

  echo "$1 is a big file"

else

  echo "$1 is a small file"

fi


8:判断给出的一个字符串是否为整数。

答:

#!/bin/bash

#判断是否为整数

#

if echo $1 | grep "^\<[[:digit:]]\+\>$" &> /dev/null; then

  echo "$1 is a integer"

else

  echo "$1 is not integer"

fi


[root@little ~]# ./8.sh 666

666 is a integer


9、写一个脚本,shift作用。

答:

#!/bin/bash

#shift作用

#

while [ $# -ne 0 ]; do

  echo -e "第一个参数为:$1\n参数总数为:$#"

  shift

done


[root@little ~]# ./9.sh 2 3 56 

第一个参数为:2

参数总数为:3

第一个参数为:3

参数总数为:2

第一个参数为:56

参数总数为:1


10、写一个脚本,求写入的参数之和。

答:

#!/bin/bash

#求参数之和

#

if [ $# -eq 0 ]; then

  echo "Usage: $(basename $0) 参数"

  exit

fi


sum=0

until [ $# -eq 0 ]; do

  sum=$[$sum+$1]

  shift

done


echo "sum is $sum"


[root@little ~]# bash 10.sh 1 2 3 4 

sum is 10


11、示例6的扩展,用shift实现:写一个脚本,能够添加或删除用户账户,可以使用-a选项完成添加,使用-d选项完成删除任务。

答:

#!/bin/bash

#shift实现创建和删除

#

if [ $# -ne 2 ]; then

  echo "Usage: $(basename $0) -a username | -d username"

  exit

fi


if [ $1 == '-a' ]; then

  shift

  if id $1 &> /dev/null; then

    echo "$1 is exist"

  else

    useradd $1

    echo $1 | passwd --stdin $1 &> /dev/null

    echo "create $1 successfully"

  fi

fi


if [ $1 == '-d' ]; then

  shift

  if id $1 &> /dev/null; then

    userdel -r $1

    echo "delete $1 already"

  else

    echo "$1 is not exist"

  fi

fi


[root@little ~]# ./11.sh -a ling 

ling is exist


12、简易计算器(不能计算乘法,注意:乘法要用"\*")

答:

#!/bin/bash

#简易计算器

#

if [ $# -ne 3 ]; then

  echo "please input three digits"

  exit

fi


echo $[$1$2$3]


[root@little ~]# bash 12.sh 3 \* 5

15


13、编写一个脚本,要求:从/etc/passwd中UID和GID相同的用户中随机选择一个用户,判断其用户的类型:UID为0,即超级用户;UID在1-999之间,即系统用户;UID为1000+,即登录用户。

答:

#!/bin/bash

#判断UID和gid相同的用户

#

lines=$(grep "\<\([[:digit:]]\+\)\>.*\1" /etc/passwd | wc -l)

sequence=$[$RANDOM%$lines+1]

username=$(grep "\<\([[:digit:]]\+\)\>.*\1" /etc/passwd | sort -n -t ":" -k 3 | head -n $sequence | tail -1 | cut -d ":" -f1)

userid=$(id -u $username)


if [ $userid -eq 0 ]; then

  echo "$username is 超级用户"

elif [ $userid -ge 1000 ]; then

  echo "$username is 登录用户"

else

  echo "$username is 系统用户"

fi


[root@little ~]# ./13.sh 

vcsa is 系统用户

[root@little ~]# ./13.sh 

root is 超级用户

[root@little ~]# ./13.sh 

rpcuser is 系统用户


14、写一个脚本,能够添加或删除一个或多个用户账户,可以使用-a选项完成添加,使用-d选项完成删除任务。

答:

#!/bin/bash

#用户添加删除

#

if [ $# -ne 2 ]; then

  echo "Usage: $(basename $0) -a username | -d username"

  exit

fi


if [ $1 == '-a' ]; then

  shift

  for I in $*; do

    if id $I &> /dev/null; then

      echo "$I is exist already"

    else

      useradd $I

      echo $I | passwd --stdin $I &> /dev/null

      echo "create $I seccessfully"

    fi

  done

fi


if [ $1 == '-d' ]; then

  shift

  for I in $*; do

    if id $I &> /dev/null; then

      userdel -r $I

      echo "delete $I already"

    else

      echo "$I is not exist"

    fi

  done

fi


[root@little ~]# bash 14.sh

Usage: 14.sh -a username | -d username

[root@little ~]# bash 14.sh -a ling

ling is exist already


15、计算100以内所有整数的和。

答:

#!/bin/bash

#100以内的整数和

#

read -t 10 -p "please input a integer: " integer


if [ -z $integer ]; then

  echo "pleae input a integger"

  exit

fi


if ! echo $integer | grep -q "^\<[[:digit:]]\+\>$" &> /dev/null; then

  echo "please input a integer"

  exit

fi


sum=0

for I in $(seq $integer); do

  sum=$[$sum+$I]

done


echo "$integer 以内的整数和为:$sum"


[root@little ~]# ./15.sh

please input a integer: 10

10 以内的整数和为:55


16、写一个脚本,打印倒置的等腰三角形。

答:

#!/bin/bash

#打印倒置三角形

#

if [ $# -eq 0 ]; then

  echo "please input row number"

  exit

fi


for I in $(seq $1); do

  for J in $(seq $[I-1]); do

    echo -n " "

  done

  for K in $(seq $[2*($1-$I)+1]); do

    echo -n "*"

  done

  echo

done


[root@little ~]# bash 16.sh

please input row number

[root@little ~]# bash 16.sh 4

*******

 *****

  ***

   *


17、打印九九乘法表。

答:

#!/bin/bash

#九九乘法表

#

for I in $(seq 9); do

  for J in $(seq $I); do

    echo -ne "$J*$I=$[$J*$I]\t"

  done

  echo

done


[root@little ~]# ./17.sh 

1*1=1

1*2=2 2*2=4

1*3=3 2*3=6 3*3=9

1*4=4 2*4=8 3*4=12 4*4=16

1*5=5 2*5=10 3*5=15 4*5=20 5*5=25

1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36

1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49

1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64

1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81


18、写一个脚本,算出100以内的整数和(C语言风格)。

答:

#!/bin/bash

#100以内的整数和

#

sum=0

for ((I=1;I<=100;I++)); do

  let sum=$[$sum+$I]

done


echo "100 以内的整数和为:$sum"


[root@little ~]# bash 18.sh

100 以内的整数和为:5050


19、查找/var目录中所有的空文本文档,并删除。

答:

#!/bin/bash

#删除var下的空文档

#

for I in $(ls -d /var/*); do

  if [ -f $I ]; then

    if [ ! -s $I ]; then

      rm -f $I

      echo "delete $I successfully"

    fi

  fi

done


20、写一个脚本:提示用户输入信息,然后判断用户输入的信息是否合法。

答:

#!/bin/bash

#判断用户输入信息是否合法

#

read -p "please input your choice yes|no:" choice


case $choice in

  [Yy][Ee][Ss])

    echo "This is right."

    ;;

  [Nn][Oo])

    echo "This is fault."

    ;;

  *)

    echo "please input your choice yes|no"

    ;;

esac


[root@little ~]# bash 20.sh

please input your choice yes|no:yes

This is right.


21、写一个脚本,管理用户账户的脚本,case实现a添加和d删除(case分支实现)。

答:

#!/bin/bash

#用户账户管理

if [ $# -lt 2 ]; then

  echo "please input two facters"

  exit

fi


case $1 in

  -a)

    for I in $(echo $2 | tr "," " "); do

      if id $I &> /dev/null; then

        echo "$I is exist."

      else

        useradd $I

        echo $I | passwd --stdin $I

      fi

    done

  ;;

  -d)

    for I in $(echo $2 | tr "," " "); do

      if id $I &> /dev/null; then

        userdel -r $I

        echo "delete $I already."

      else

        echo "$I is not exist."

      fi

    done

  ;;

  *)

    echo "Usage: $(basename $0) -a username1,username2... | -d username1,username2..."

    exit

  ;;

esac


[root@little ~]# ./21.sh -a ling

ling is exist.


22、写一个脚本,利用while循环结构,计算100以内所有整数的和。

答:

#!/bin/bash

#100以内的整数和,while循环

#

declare -i I=1

sum=0

while [ $I -le 100 ]; do

  let sum=$[$sum+$I]

  let I++

done


echo "100以内的整数和(while循环)为:$sum"


[root@little ~]# bash 22.sh

100以内的整数和(while循环)为:5050


23、写一个脚本,利用until循环结构,计算100以内所有整数的和。

答:

#!/bin/bash

#100以内的整数和,until循环

#

declare -i I=1

sum=0

until [ $I -gt 100 ]; do

  let sum=$[$sum+$I]

  let I++

done


echo "100以内的整数和(until循环)为:$sum"


[root@little ~]# ./23.sh

100以内的整数和(until循环)为:5050


24、写一个脚本,判断是否为正确文本文件,以bash脚本格式打开一个文本文档。

答:

#!/bin/bash

#以bash文本输入

#

if [ $# -eq 0 ]; then

  echo "please input one parameter"

  exit

fi


if [ ! -e $1 ]; then

  echo "$1 is not exist."

  exit

fi


if [ ! -f $1 ]; then

  echo "please input a txt file."

  exit

fi


until false; do

  vi + $1

  if bash -n $1; then

    break

  else

    continue

  fi

done


[root@little ~]# ./24.sh 23.sh

[root@little ~]#

# 进入23.sh脚本


25、写一个脚本,用select创建菜单式列表。

答:

#!/bin/bash

#select菜单列表

#

select I in rice soup meat corn; do

  case $I in

    rice)

      echo "I like rice"

      break

    ;;

    soup)

      echo "I do not like soup"

      break

    ;;

    meat)

      echo "I like meat"

      break

    ;;

    cron)

      echo "corn is nice for us"

      break

    ;;

    *)

      echo "please choise 1(rice)|2(soup)|3(meat)|4(corn)"

      continue

    ;;

  esac

done


[root@little ~]# bash 26.sh

1) rice

2) soup

3) meat

4) corn

#? 1

I like rice


26、编写一个shell脚本将/usr/local命令下大于100k的文件转移到/tmp目录下(略错)。

答:

#!/bin/bash

#/usr/local下的文件复制

#

path=/usr/local

des=/tmp

for I in $path/*; do

  if [ $(wc -c < $I) -gt 102400 ]; then

    mv $I $des

    ls -l $I

  fi

done


27、写一个具有systemV风格的脚本:给脚本传递一些参数,如start、stop、restart、status。

答:

#!/bin/bash

#系统的start/stop/restart/status

#

lockfile="/var/local/subsys/$(basename $0)"


case I in

  start)

    if [ -f $lockfile ]; then

      echo  "service start up already"

    else

      touch $lockfile

      echo "service is starting"

    fi

  ;;

  stop)

    if [ -f $lockfile ]; then

       rm -f $lockfile

       echo "service was stop"

    else

       echo "service is stop"

    fi

  ;;

  restart)

    if [ -f $lockfile ]; then

      echo  "service start up already"

    else

      touch $lockfile

      echo "service is starting"

    fi


    if [ -f $lockfile ]; then

       rm -f $lockfile

       echo "service was stop"

    else

       echo "service is stop"

    fi

  ;;

  status)

  if [ -f $lockfile ]; then

    echo "service is running"

  else

    echo "service is stop"

  fi

  ;;

esac


[root@little ~]# ./27.sh start

touch: 无法创建"/var/local/subsys/27.sh": 没有那个文件或目录

service is starting


28、写一个脚本,创建用户局部变量。

答:#!/bin/bash

#局部变量创建多用户

#

adduser() {

  for I in {1..5}; do

    if id $1$I /&> /dev/null; then

      echo "$1$I is exist"

    else

      useradd $1$I

      echo $1$I | passwd --stdin $1$I

      echo "create $1$I successfully"

    fi

  done

}


adduser $1


[root@little ~]# bash 28.sh

更改用户 1 的密码 。

passwd: 所有的身份验证令牌已经成功更新。

create 1 successfully

更改用户 2 的密码 。

passwd: 所有的身份验证令牌已经成功更新。

create 2 successfully

更改用户 3 的密码 。

passwd: 所有的身份验证令牌已经成功更新。

create 3 successfully

更改用户 4 的密码 。

passwd: 所有的身份验证令牌已经成功更新。

create 4 successfully

更改用户 5 的密码 。

passwd: 所有的身份验证令牌已经成功更新。

create 5 successfully


29、写一个脚本,调用函数,本地变量。

答:

#!/bin/bash

#本地和局部变量

#

name=little


func() {

  local name=ling

  echo "func_name=$name"

}


func

echo "external_name: $name"


[root@little ~]# bash 29.sh

func_name=ling

external_name: little


30、写一个脚本,求阶乘。

答:

#!/bin/bash

#求阶乘

#

if [ $# -eq 0 ]; then

  echo "please input one integer"

  exit

fi


func() {

  if [ $1 -eq 0 ] || [ $1 -eq 1 ]; then

    echo "1"

  else

    echo "$[$1*$[$1-1]]"

  fi

}


func $1


[root@little ~]# ./30.sh 3

6


31、写一个脚本,列出斐波那契数列。

答:

#!/bin/bash

#斐波那契

#

if [ $# -eq 0 ]; then

  echo "please input one parameter"

  exit

fi


fabonacci() {

  if [ $1 -eq 0 ] || [ $1 -eq 1 ]; then

    echo "1"

  else

    echo "$[$(fabonacci $[$1-1])+$(fabonacci $[$1-2])]"

  fi

}



for I in $(seq 0 $1); do

  fabonacci $I

done


[root@little ~]# ./31.sh 4

1

1

2

3

5


32、写一个脚本,解决汉诺塔问题。

答:

#!/bin/bash

#汉诺塔问题

#

step=0

move() {

  let step++

  echo "$step: move disk $1 $2 ------> $3"

}


hanoi() {

  if [ $1 -eq 1 ]; then

    move $1 $2 $4

  else

    hanoi "$[$1-1]" $2 $4 $3

    move $1 $2 $4

    hanoi "$[$1-1]" $3 $2 $4

  fi

}


hanoi $1 A B C


[root@little ~]# ./32.sh 4

1: move disk 1 A ------> B

2: move disk 2 A ------> C

3: move disk 1 B ------> C

4: move disk 3 A ------> B

5: move disk 1 C ------> A

6: move disk 2 C ------> B

7: move disk 1 A ------> B

8: move disk 4 A ------> C

9: move disk 1 B ------> C

10: move disk 2 B ------> A

11: move disk 1 C ------> A

12: move disk 3 B ------> C

13: move disk 1 A ------> B

14: move disk 2 A ------> C

15: move disk 1 B ------> C


33、写一个脚本,利用任意循环结构,输出每一行的第二个和第四个字符(以逗号分隔)。

文件为ling.txt为:

1,2,3,4,5

q,w,e,r,t

a,s,d,f,g

q,w,e,r,t

a,s,d,f,g

答:

#!/bin/bash

#查找指定列

#

while read LINES; do

  echo -n "hello: "

  echo $LINES | cut -d "," -f2,4 | tr "," " "

done < ling.txt


[root@little ~]# bash 33.sh

hello: 2 4

hello: w r

hello: s f

hello: w r

hello: s f


34、写一个脚本实现下列功能

(1)、允许用户通过命令行传递参数,实现用户账户的管理

(2)、如果给出-a|--add选项,就创建该选项后面的用户账户

(3)、如果给出-d|--del选项,就删除该选项后面的用户账户 

(4)、如果用户给出-v|--verbose选项,就显示创建或删除信息;

(5)、.如果用户给出-h|--help选项,就显示帮助信息,并且以0作为退出状态码退出脚本的运行;

(6)、如果用户给出的其他选项,显示帮助信息,并以5作为退出状态码退出脚本的运行

答:

#!/bin/bash

#添加删除状态

#

usage() {

  echo "Usage: $(basename $0) -a|--add username1,username2... | -d|--del username1,username2..."

  echo "option:"

  echo -e "-a|--add\vCreate some users"

  echo -e "-d|--del\vDelete some users"

  echo -e "-h|--help\vDisplay help information"

}


adduser() {

  addlist=$(echo $2 | tr "," " ")

  for I in $addlist; do

    if id $I &> /dev/null; then

      echo "$I is exist already"

    else

      useradd $I

      echo $I | passwd --stdin $I > /dev/null

      echo "create $I successfully"

    fi

  done

}


deleteuser() {

  dellist=$(echo $2 | tr "," " ")

  for J in $dellist; do

    if id $J &> /dev/null; then

      userdel -r $J &> /dev/null

      echo "delete $J already"

    else

      echo "$J is not exist"

    fi

  done

}


#main function


if [ $# -eq 0 ]; then

  usage

  exit 5

fi


case $1 in

  -a|--add)

    adduser $2

  ;;

  -d|--del)

    deleteuser $2

  ;;

  -h|--help)

    usage

    exit 0

  ;;

  *)

    usage

    exit

  ;;

esac


[root@little ~]# ./34.sh --help

Usage: 34.sh -a|--add username1,username2... | -d|--del username1,username2...

option:

-a|--add

        Create some users

-d|--del

        Delete some users

-h|--help

         Display help information


35、写一个脚本,给脚本传递几个文件,选择压缩方式进行压缩。

答:

#!/bin/bash

#归档压缩

#

if [ $# -eq 0 ]; then

  echo "Usage: $(basename $0) 请输入归档压缩文件: "

  exit

fi


select I in gzip bzip2 xz; do

  case $I in

    gzip)

      while true; do

        tar -czf $1.tar.gz $1

        shift

      done

      exit

    ;;

    bzip2)

      while true; do

        tar -cjf $1.tar.bz2 $1

        shift

      done

      exit

    ;;

    xz)

      while true; do

        tar -cJf $1.tar.xz $1

        shift

      done

      exit

    ;;

    *)

      echo "please input compression way: gzip|bzip2|xz"

      exit

    ;;

  esac

done


36、写一个脚本,模拟Linux登录界面:

(1)、将用户名和密码(明文即可)存放于/tmp/userinfo文件中,格式为username:password;

(2)、运行脚本后提示用户输入用户名和密码;

(3)、用户输入完成后,判断用户输入的是否正确;

   a)、如果输入错误的用户名,直接提示用户重新输入正确的用户名;

   b)、如果用户名正确,密码错误,提示用户重新输入密码,三次连续输错,则让用户重新输入用户名。

答:

#!/bin/bash

#linux登录界面

#

while true; do

  read -p "please input your login name: " GET_NAME

  if ! grep -q "^\<$GET_NAME\>" /tmp/userinfo; then

    continue

  fi


  COUNT=0

  until false; do

    read -p "please input your login password: " GET_PASSWORD

    PASSWORD=$(grep "$GET_NAME" /tmp/userinfo | cut -d: -f2)


      if [ $GET_PASSWORD != $PASSWORD ]; then

         let COUNT++

         if [ $COUNT -eq 3 ]; then

            break

         fi

         continue

      else

         break 2

      fi

  done

done


echo "login succussfully"


[root@little ~]# cat /tmp/userinfo 

little:78

ling:23


[root@little ~]# bash 36.sh

please input your login name: ling

please input your login password: 23


37、写一个脚本:

 1) 显示一个菜单给用户:

    d|D) show disk usages.

    m|M) show memory usages.

    s|S) show swap usages.

    q|Q) quit.

 2) 当用户给定选项后显示相应的内容;

 3) 每次执行完成后不退出脚本,而是提示用户可以继续查看其它信息;直到用户键入q、Q、quit或QUIT退出;

 4) 如果用户给出的不是菜单中可用的选项,则告诉用户选项错误,需要重新选择;

注意:cat实现菜单,后一个EOF前不能有空格,不然无法识别编辑结束。

答:

#!/bin/bash

#磁盘和内存使用情况

#

menu() {

  cat << EOF

  d|D) show disk usages

  m|M) show memory usages

  s|S) show swap usages

  q|Q) quit.

EOF

}


while true; do

  menu

  read -p "please input your choice: " CHOICE

  case $CHOICE in

    d|D)

      echo "---------------------------------------"

      df -hT

      echo "---------------------------------------"

      continue

    ;;

    m|M)

      echo "---------------------------------------"

      free -m | head -2

      echo "---------------------------------------"

      continue

    ;;

    s|S)

      echo "--------------------------------------"

      free -m | head -1

      free -m | tail -1

      echo "---------------------------------------"

    ;;

    q|Q|quit|QUIT)

      break

    ;;

    *)

      echo "please input your choice"

      continue

    ;;

  esac

done


[root@little ~]# bash 37.sh

  d|D) show disk usages

  m|M) show memory usages

  s|S) show swap usages

  q|Q) quit.

please input your choice: m

---------------------------------------

             total       used       free     shared    buffers     cached

Mem:           475        461         13          2         25        135

---------------------------------------

  d|D) show disk usages

  m|M) show memory usages

  s|S) show swap usages

  q|Q) quit.

please input your choice: d

---------------------------------------

Filesystem     Type     Size  Used Avail Use% Mounted on

/dev/sda2      ext4      50G  5.6G   42G  12% /

tmpfs          tmpfs    238M  224K  238M   1% /dev/shm

/dev/sda1      ext4     190M   36M  145M  20% /boot

/dev/sr0       iso9660  3.7G  3.7G     0 100% /media/CentOS_6.7_Final

---------------------------------------

  d|D) show disk usages

  m|M) show memory usages

  s|S) show swap usages

  q|Q) quit.

please input your choice: q


38、写一个脚本:向每个默认shell为bash且uid与gid相等的用户问候母亲节快乐,格式如下:"Hi USERNAME, Happy mother's Day!"。

答:

#!/bin/bash

#UID和gid相同的用户

#

bashuser=$(grep "bash$" /etc/passwd | cut -d ":" -f1)


for I in $bashuser; do

  if [ $(id -u $I) -eq $(id -g $1) ]; then

    echo "Hello $I, happy mother's day"

  fi

done


[root@little ~]# ./38.sh

Hello root, happy mother's day