简体   繁体   English

Linux 命令在 bash 中不起作用

[英]Linux commands not working in bash

I have a bash script called AddUsers.sh.我有一个名为 AddUsers.sh 的 bash 脚本。 The script should take a line like this:该脚本应采用如下一行:

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

and return variables like this:并返回这样的变量:

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

When I run the script and give it the required text file, it is tellilng me that 'groupadd' 'chgrp' 'chmod' and 'chage' are all errors.当我运行脚本并提供所需的文本文件时,它告诉我 'groupadd' 'chgrp' 'chmod' 和 'chage' 都是错误的。 Is anyone able to tell me why / give me any feedback?有没有人能告诉我为什么/给我任何反馈?

Thanks in advance for any help.在此先感谢您的帮助。

#!/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

you could use commands in scripts with full path address.您可以在具有完整路径地址的脚本中使用命令。 and you can extract their full path address by "which" command.您可以通过“which”命令提取它们的完整路径地址。 for example instead of using "chmod" you could use "/bin/chmod" type this to find full path: $which chmod例如,代替使用“chmod”,您可以使用“/bin/chmod”键入以下内容来查找完整路径:$which chmod

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM