码迷,mamicode.com
首页 > 系统相关 > 详细

shell脚本空格规范

时间:2017-07-07 20:05:34      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:shell初学

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!



shell脚本空格规范

标签:shell初学

原文地址:http://520158.blog.51cto.com/510158/1945321

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