2016-02-25 70 views
0

我试图输出哪些帐户已经成功地从文本文件创建,哪些没有。我还想输出成功创建的帐户数量。我目前得到以下错误:grep:3:没有这样的文件或目录。脚本和文本文件保存在同一个文件夹中。我在脚本中使用了以下命令。从bash脚本中的文本文件验证帐户的创建

file=users.txt 
verify =grep "verify" $file |cut -f2 -d:` 
cat /etc/passwd | grep $verify 

echo -e "\nYou have Currently" 
cat /etc/passwd | grep $verify |wc -l; 
echo "users added from your Text File" 

编辑:

#!/bin/bash 
ROOT_UID=0 #The root user has a UID of 0 
if [ "$UID" -ne "$ROOT_UID" ]; then 
    echo "**** You must be the root user to run this script!****" 
    exit 
fi 
clear 
echo 
echo "######################################################" 
echo "##### Batch script to automate creation of users #####" 
echo -e "######################################################\n" 

while true; 
do 
    file=notvalid 
    while [ $file == "notvalid" ] 
    do 
    #echo "repeat $repeat" 
    #echo -e "\n" 
    echo -n "Please enter import filename:" 
    read filename 
    echo -e "\r" 
    exists=0 
    if [ -e $filename ]; then 
     file=valid 

     while IFS=":" read firstname lastname userid password group 
     do 
      egrep -i "^$userid:" /etc/passwd &>/dev/null 
      if [ $? -eq 0 ]; then 
       exists=$((exists+1)) 
       #echo -e "${firstname} ${lastname} already exists on the system" 
       #grep ${userid} /etc/passwd 
       aname=$(getent passwd "$userid" | cut -d: -f3) 
       echo "Account Exists: $aname" 
       euserid=$(getent passwd "$userid" | cut -d: -f1) 
       echo "User ID: $userid" 
       homedir=$(getent passwd "$userid" | cut -d: -f6) 
       echo "Home Directory: $homedir" 
       usershell=$(getent passwd "$userid" | cut -d: -f7) 
       echo "User Shell: $usershell" 
       g=$(id -Gn "$userid") 
       echo "Groups: $g" 
       echo -e "\r" 
      else 
       egrep -i "^$group:" /etc/group &>/dev/null 
       if [ $? -eq 1 ]; then 
        /usr/sbin/addgroup ${group} &>/dev/null 
       fi 
       useradd -d /home/"${userid}" -m -s /bin/bash -c \ 
"${firstname}${lastname}" -g "${group}" "${userid}" 
       echo "Creating Account: ${firstname} ${lastname}" 
       nuserid=$(getent passwd "$userid" | cut -d: -f1) 
       echo "Creating User ID: ${nuserid}" 
       { echo ${password}; echo ${password}; } | sudo passwd ${userid} > /dev/null 2>&1 
       echo "Creating Password: ${password}" 
       echo "Creating Home Directory: /home/${userid}" 
       echo "Creating User Shell: /bin/bash" 
       echo -e "Assigning Group: ${group}\n" 
      fi 

     done < $filename 
    else 
     echo -e "##### CANNOT FIND OR LOCATE FILE #####" 
    fi 

    verify=`grep "verify" /home/pi/$filename | cut -f3 -d:` 
    echo "$verify" 
    count=0 
    for id in $verify 
     do grep -wo ^$id /etc/passwd && count=$((count+1)) 
    done 
    echo $count users added from your text file 
    echo these are not added: 
    for id in $verify 
     do grep -wq ^$id /etc/passwd || echo $id 
    done 
    while true 
    do 
    echo -n "Create additional accounts [y/n]: " 
    read opt 
    if [[ $opt == "n" || $opt == "y" ]];then 
     break 
    else 
     echo "Invalid Input" 
    fi 
    done 

    if [ $opt = "n" ]; then 
     clear 
     break 
    else 
     clear 
    fi 
done 
+1

您在第一个cat命令中错误地拼写了'varify'。 –

+0

变量名和命令之间不能有空格,并且执行系统命令需要很好地包装** verify = $(grep“verify”$ file | cut -f2 -d:)* * – tink

+0

请看看:http://www.shellcheck.net/ – Cyrus

回答

1

你是几乎没有。

您的方法的主要问题是您尝试使用grep一次搜索多个帐户。变量verify有多个用户标识,因此您需要逐个处理它。

file=users.txt 
verify=`grep "verify" $file | cut -f2 -d:` 
count=0 
for id in $verify 
    do grep -wo ^$id /etc/passwd && count=$((count+1)) 
done 
echo $count users added from your text file 
echo these are not added: 
for id in $verify 
    do grep -wq ^$id /etc/passwd || echo $id 
done 

for循环将各元件在verify可变进id和使用grep搜索(-w仅匹配整个词语,不是片段,^线和-o输出的开头匹配只有匹配的字不整条线)。 我们计算count变量中匹配的数量。如同您一样,运行for循环两次并将第二个循环到wc -l。 & &运算符表示如果前一个命令发现匹配(grep的返回码为0),它将增加count

如果grep没有找到匹配项(返回代码不是0),下一个循环将不会打印匹配的id(-q),并且会回显id。这是通过||来实现​​的运营商。

关于迭代列表的最后一点注意事项:如果成员可以包含空格(与userids不同),则应该使用${verify[@]}(这是一个bash-ism)而不是$verify

而忘了这个:cat /etc/passwd | grep pattern,改用grep pattern /etc/passwd

+0

我尝试了我的脚本中的代码,但它似乎分裂并打印字符串验证。它似乎没有处理'| cut -f2 -d:'作为命令 – learningpython

+0

它将字符串'verify'分割,但只打印在/ etc/passwd中找到的id。也许所有的id都是在users.txt里成功创建的,所以你没有注意到它们的区别。 for循环将搜索/ etc/passwd中的每个id,并只打印匹配的id。 – xian

+0

我一定在做错事。为了测试发生了什么,我在第一个循环中打印了id。它打印grep,“验证”,$文件等等。它似乎分裂字符串'验证',而不是实际的文件。剪切-d':'-f3 $文件作为一个孤立的命令切割文件罚款。 – learningpython