Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 意外标记“done';_Shell_Token - Fatal编程技术网

Shell 意外标记“done';

Shell 意外标记“done';,shell,token,Shell,Token,此shell脚本用于向系统添加用户。新用户的详细信息在一个文件中。shell拒绝此脚本,并显示以下消息: syntax error near unexpected token 'done'. 怎么了 #/bin/bash #用途:根据文本文件中的数据自动在linux系统中添加新用户 #为每个用户分配加密密码 #将用户添加到组或为这些用户创建新组 #必要时,在日志文件中报告错误和成功操作 #发布帮助选项(echo) #根验证 如果[[$(id-u)-eq 0]];然后 #参数验证 如果[-z“$

此shell脚本用于向系统添加用户。新用户的详细信息在一个文件中。shell拒绝此脚本,并显示以下消息:

syntax error near unexpected token 'done'.
怎么了

#/bin/bash
#用途:根据文本文件中的数据自动在linux系统中添加新用户
#为每个用户分配加密密码
#将用户添加到组或为这些用户创建新组
#必要时,在日志文件中报告错误和成功操作
#发布帮助选项(echo)
#根验证
如果[[$(id-u)-eq 0]];然后
#参数验证
如果[-z“$1”];然后
echo“未找到任何参数!”
echo“请包含用户详细信息文本文件作为第一个参数”
echo“请包含一个报告文本文件作为第二个参数”
echo“请将错误报告文本文件作为第三个参数”
echo“使用-h参数(即../script-h)获取帮助”
出口1
fi
#帮助验证和帮助文件
如果[“$1”=“-h”];然后
echo“这是帮助信息文件”
echo“此脚本旨在通过读取用户详细信息文件(如userlist.txt)中的信息将用户添加到linux系统”
echo“此用户详细信息文本文件的格式为“用户名密码组名全名”,使用制表符间距分隔”
echo“此脚本将第一个参数作为用户详细信息文件读取,第二个和第三个参数将分别作为成功报告文件和错误报告文件读取”
出口
fi
#将第一个参数作为数据的用户详细信息文件读取
cat userlist.txt |读取uname密码gname fullname时
#读取用户名的/etc/passwd
白鹭-w“^$uname”/etc/passwd
#如果找到用户名,则会报告错误
如果[$?==0];然后
echo“用户已存在:添加用户名为$uname;$gname;$fullname”>>success1.log的用户时出错
出口1
其他的
#读取Groupname的/etc/group
白鹭-w“^$gname”/etc/组
#如果找到Groupname,则什么也没有
如果[$?==0];然后
回声“”
其他的
#如果找不到Groupname,则创建新的组和报告
groupadd“$gname”
echo“未找到组:已创建新组$gname”>>successs1.log
fi
#检索日期
createddate=$(日期)
#Perl密码脚本从Userlist获取输入
pass=$(perl-e'打印密码($ARGV[0],“Password”)'“$Password”)
#使用userlist中的变量添加用户
用户添加“$uname”-g“$gname”-c“$fullname”-p“$pass”
#向successlist和errorlist报告文件报告信息
如果[$?==0];然后
groupid=$(id-g$uname)
userid=$(id-u$uname)
echo“用户已成功添加:$uname;$userid;$gname;$groupid;$createddate;$fullname”>>successs1.log
其他的
groupid=$(id-g$uname)
userid=$(id-u$uname)
echo“发生Useradd错误:$uname;$userid;$gname;$groupid;$createddate;$fullname”>>Errors1.log
echo“错误:必须是根用户才能执行脚本”
出口
fi
完成

第二次尝试 根据答案中的一些想法,我提出了第二个尝试:

#!/bin/bash
#Purpose: Automatically add new users in a linux system based upon the data found within a text file
#         Assign encryped passwords to each user
#         Add users to groups or create new groups for these users
#         Report errors and successful operations where necessary in log files
#         post help options (echo)

#Root validation
if [[ $(id -u) -eq 0 ]]; then
  #Argument validation
  if [[ -z "$1" ]]; then
    echo "Usage: $0 usernames report errors" 1>&2
    echo "Please include a user detail text file as first argument"
    echo "Please include a report text file as second argument"
    echo "Please include an error report text file as the third argument"
    echo "Use the -h argument (i.e. ./script -h) for help"
    exit 1
  fi
fi

#Help validation and Help file
if [[ "$1" = "-h" ]]; then
  echo "This is the help information file"
  echo "This script is designed to add users to a linux system by reading information from a user detail file (such as userlist.txt)"
  echo "The format of this user detail text file is "username password groupname fullname" seperated using TAB spacing"
  echo "This script will read the first argument as the user detail file, the second and third arguments will be read as a success report file and error report file respectively"
  exit
fi

#Reads first argument as user detail file for data
cat jan.txt | while read uname password gname fullname; do
#Reads /etc/passwd for Username
 egrep -w "^$uname:" /etc/passwd >/dev/null 2>&1
#If Username is found then error reports
if [ $? == 0 ] 
then
  echo "User Already Exists : Error adding user with username $uname;$gname;$fullname" >> Errors1.log

else
  #Reads /etc/group for Groupname
  egrep -w "^$gname" /etc/group
  #If Groupname is found then nothing
if [ $? == 0 ]; then
    echo ""
else
  #If Groupname not found then creates new group and reports
  groupadd "$gname"
    echo "Group Not Found: New Group $gname was created" >> Successes1.log
done < $1
#Retrieves Date
createddate=$(date)
#Perl password script takes input from Userlist
pass=$(perl -e 'print crypt($ARGV[0], "Password")' "$password")
#Adds Users with variables from userlist
useradd "$uname" -g "$gname"  -c "$fullname" -p "$pass"
#Reports information to successlist and errorlist report files
if [ $? == 0 ]
then
  groupid=$(id -g $uname)
  userid=$(id -u $uname)
  echo "User Successfully Added: $uname;$userid;$gname;$groupid;$createddate;$fullname" >> Successes1.log
else
  groupid=$(id -g $uname)
  userid=$(id -u $uname)
  echo "Useradd Error Occurred: $uname;$userid;$gname;$groupid;$createddate;$fullname" >> Errors1.log
  echo "Error: Must be root user to execute script"
  exit 1
fi
fi
done
#/bin/bash
#用途:根据文本文件中的数据自动在linux系统中添加新用户
#为每个用户分配加密密码
#将用户添加到组或为这些用户创建新组
#必要时,在日志文件中报告错误和成功操作
#发布帮助选项(echo)
#根验证
如果[[$(id-u)-eq 0]];然后
#参数验证
如果[-z“$1”];然后
echo“用法:$0用户名报告错误”1>&2
echo“请包含用户详细信息文本文件作为第一个参数”
echo“请包含一个报告文本文件作为第二个参数”
echo“请将错误报告文本文件作为第三个参数”
echo“使用-h参数(即../script-h)获取帮助”
出口1
fi
fi
#帮助验证和帮助文件
如果[“$1”=“-h”];然后
echo“这是帮助信息文件”
echo“此脚本旨在通过读取用户详细信息文件(如userlist.txt)中的信息将用户添加到linux系统”
echo“此用户详细信息文本文件的格式为“用户名密码组名全名”,使用制表符间距分隔”
echo“此脚本将第一个参数作为用户详细信息文件读取,第二个和第三个参数将分别作为成功报告文件和错误报告文件读取”
出口
fi
#将第一个参数作为数据的用户详细信息文件读取
cat jan.txt |读取uname密码gname fullname时;做
#读取用户名的/etc/passwd
egrep-w“^$uname:”/etc/passwd>/dev/null 2>&1
#如果找到用户名,则会报告错误
如果[$?==0]
然后
echo“用户已存在:添加用户名为$uname;$gname;$fullname的用户时出错”>>Errors1.log
其他的
#读取Groupname的/etc/group
白鹭-w“^$gname”/etc/组
#如果找到Groupname,则什么也没有
如果[$?==0];然后
回声“”
其他的
#如果找不到Groupname,则创建新的组和报告
groupadd“$gname”
echo“未找到组:已创建新组$gname”>>successs1.log
已完成<$1
#检索日期
createddate=$(日期)
#Perl密码脚本从Userlist获取输入
pass=$(perl-e'打印密码($ARGV[0],“Password”)'“$Password”)
#使用userlist中的变量添加用户
用户添加“$uname”-g“$gname”-c“$fullname”-p“$pass”
#向successlist和errorlist报告文件报告信息
如果[$?==0]
然后
groupid=$(id-g$uname)
userid=$(id-u$uname)
echo“用户已成功添加:$uname;$userid;$gname;$groupid;$createddate;$fullname”>>successs1.log
其他的
groupid=$(id-g$uname)
userid=$(id-u$uname)
echo“发生Useradd错误:$uname;$userid;$gname;$groupid;$createddate;$fullname”>>Errors1.log
echo“错误:必须是根用户才能执行脚本”
出口1
fi
fi
完成
这似乎也不能正常工作。现在怎么了? 似乎显示参数和运行,但未添加任何用户或组,因此未创建任何日志
cat userlist.txt | while read uname password gname fullname
do 
cat userlist.txt | while read uname password gname fullname; do
if [ $? == 0 ]; then
  echo "User Already Exists : Error adding user with username ...
  exit 1
else
cat userlist.txt | while read uname password gname fullname
while cmd1
      cmd2
      cmd3 ...
do
#!/bin/bash
#Purpose: Automatically add new users in a linux system based upon the data found within a text file
#         Assign encryped passwords to each user
#         Add users to groups or create new groups for these users
#         Report errors and successful operations where necessary in log files
#         post help options (echo)

#Root validation
if [[ $(id -u) -eq 0 ]]
then
    #Argument validation
    if [[ -z "$1" ]]
    then
        echo "No arguments found!"
        echo "Please include a user detail text file as first argument"
        echo "Please include a report text file as second argument"
        echo "Please include an error report text file as the third argument"
        echo "Use the -h argument (i.e. ./script -h) for help"
        exit 1
    fi
fi

#Help validation and Help file
if [[ "$1" = "-h" ]]
then
    echo "This is the help information file"
    echo "This script is designed to add users to a linux system by reading information from a user detail file (such as userlist.txt)"
    echo "The format of this user detail text file is "username password groupname fullname" seperated using TAB spacing"
    echo "This script will read the first argument as the user detail file, the second and third arguments will be read as a success report file and error report file respectively"
    exit
fi

#Reads first argument as user detail file for data
cat userlist.txt | while read uname password gname fullname
do
    #Reads /etc/passwd for Username
    egrep -w "^$uname" /etc/passwd
    #If Username is found then error reports
    if [ $? == 0 ]
    then
        echo "User Already Exists : Error adding user with username $uname;$gname;$fullname" >> Successes1.log
        exit 1
    else
        #Reads /etc/group for Groupname
        egrep -w "^$gname" /etc/group
        #If Groupname is found then nothing
        if [ $? == 0 ]
        then
            echo ""
        else
            #If Groupname not found then creates new group and reports
            groupadd "$gname"
            echo "Group Not Found: New Group $gname was created" >> Successes1.log
        fi
        #Retrieves Date
        createddate=$(date)
        #Perl password script takes input from Userlist
        pass=$(perl -e 'print crypt($ARGV[0], "Password")' $pass)
        #Adds Users with variables from userlist
        useradd "$uname" -g "$gname"  -c "$fullname" -p "$pass"
        #Reports information to successlist and errorlist report files
        if [ $? == 0 ]
        then
            groupid=$(id -g $uname)
            userid=$(id -u $uname)
            echo "User Successfully Added: $uname;$userid;$gname;$groupid;$createddate;$fullname" >> Successes1.log
        else
            groupid=$(id -g $uname)
            userid=$(id -u $uname)
            echo "Useradd Error Occurred: $uname;$userid;$gname;$groupid;$createddate;$fullname" >> Errors1.log
            echo "Error: Must be root user to execute script"
            exit
        fi
    fi
done
echo "Usage: $0 usernames report errors" 1>&2
while read uname password gname fullname
do
    ...
done < $1
egrep -w "^$uname" /etc/passwd
#If Username is found then error reports
if [ $? == 0 ]
if egrep -w "^$uname:" /etc/passwd >/dev/null 2>&1
then
    #If Username is found then error report
pass=$(perl -e 'print crypt($ARGV[0], "Password")' $pass)
pass=$(perl -e 'print crypt($ARGV[0], "Password")' "$password")
sh -x script arg1 arg2 arg3 2>script-x.log