Linux命令无法在bash中运行

时间:2016-10-17 02:36:18

标签: linux bash

我有一个名为AddUsers.sh的bash脚本。脚本应该采用这样的一行:

john.mccarthy@caltech.edu;1927/09/04;sudo,visitor;/visitorData

并返回如下变量:

name=john
surname=mccarthy
bdate=1927/09/04    
uname=mccjoh
pass=1927
groups(array)=sudo, visitor
folder=/visitorData

当我运行脚本并为其提供所需的文本文件时,它告诉我' groupadd' ' chgrp命令' '搭配chmod'和' chage'都是错误。有人能告诉我为什么/给我任何反馈意见吗?

提前感谢您的帮助。

#!/bin/bash

#check for file
while [ ! -f  $file ]
do

    #ask user for filename
    echo What is the filename?

    #reading input as file name
    read file

    if [ ! -f $file ]
        then
            echo "File not found!"
        else
            echo "File found!"
    fi
done

#process each line and make a user from the data
cat "$file" | while read line

do
    name=`echo $line | sed 's/^\(.*\)\..*\@.*$/\1/g'`
    surname=`echo $line | sed 's/^.*\.\(.*\)\@.*$/\1/g'`
    bdate=`echo $line | sed 's/^.*;\(.*\);.*;.*$/\1/g'`


    #set groups to tokenize
    groups=`echo $line`

    folder=`echo $line`

    temp2=`echo $name | sed 's/^\(...\).*$/\1/g'`
    temp1=`echo $surname | sed 's/^\(...\).*$/\1/g'`

    user="${temp1}${temp2}"

    #pass must be first 4 numbers of birthdate
    pass= ${bdate:0:4}




    #tokenise group + add to array
    declare -a groupArray

    IFS=" "
    groupArray=(`echo $groups | tr "," " "`)

    #create groups if not existing.
    for i in ${groupArray[@]}
        do
            if [ getent group $i ]
                then
                    echo "group exists"
                else
                    groupadd $i
            fi
        done

    #Create shared folders if not existing.
    if [ ! -d $folder ]; 
        then
            mkdir -p $folder
    fi

    #Create groups for shared folders
    gname=`echo "${folder:1}"`
    groupadd $gname

    #Set group as owner of directory and change permissions
    chgrp -R $gname $folder
    chmod -R 770 $folder

    #create user and add to groups
    if [ grep "^${user}:" /etc/passwd ]
        then
            echo "user exists already!"

        else
            #Create user
            useradd -m -d /home/$user -p $pass $user

            #Add user to groups
            for i in ${groupArray[@]}
                do
                    usermod -a -G $i $user
                done

            #Add user to shared group
            usermod -a -G $gname $user
    fi

    #force password change
    chage -d 0 $user

done

1 个答案:

答案 0 :(得分:0)

您可以在具有完整路径地址的脚本中使用命令。并且您可以通过“which”命令提取其完整路径地址。 例如,而不是使用“chmod”你可以使用“/ bin / chmod” 输入以查找完整路径: $ chmod