shell脚本空格规范


练习,写一个脚本:

传递一个用户名参数给脚本,判断此用户的用户名跟其基本组的组名是否一致,并将结果显示出来


#!/bin/bash

#

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

        echo "No such user !"

        exit 10

fi


if [`id -n -u $1` == `id -n -g $1` ];then


#if [ $1 == `id -n -g $1` ];then

        echo "User group same!"

else

        echo "User group  difference!"

fi


注意 开头为 “if”的行,[  与  `之间要有空格,如果没有,运行时会有以下提示

[root@localhost ~]# ./ugsame.sh  test002

./ugsame.sh:行3: [test002: 未找到命令

User group  diffrence!


加了空格后,如

if [ `id -n -u $1` == `id -n -g $1` ];then

就可以正常运行,运行结果如下


[root@localhost ~]# ./ugsame.sh  test002

User group same!