2016-10-17 116 views
-1

我有一个名为AddUsers.sh的bash脚本。该脚本应该采取这样一行:Linux命令不能在bash中工作

[email protected];1927/09/04;sudo,visitor;/visitorData 

,并返回变量是这样的:

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

当我运行该脚本,并给它所需的文本文件,它是tellilng我说,“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 
+0

通过http://shellcheck.net –

+1

也跑你的脚本,而不是整个脚本,将其缩小到发生错误的位置会有所帮助。请参阅http://stackoverflow.com/help/mcve –

+0

,在希望脚本吐出其状态的位置添加'set + x',并在希望停止的位置放置'set -x'。通常情况下,如果你不想监视整个脚本,只需在文件的顶部放置'set + x',在##/ bin/bash后面# – jiveturkey

回答

0

您可以在具有完整路径地址的脚本中使用命令。你可以通过“which”命令提取他们的完整路径地址。 例如,而不是使用“文件模式”,你可以使用“/斌/文件模式” 类型此找到完整路径: $其搭配chmod张贴的