2016-02-25 239 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