2016-04-23 48 views
1

如果我想檢查單個用戶,我可以這樣做。如何將單個變量分配給列表中的多個項目?

echo "enter username" 
read -p "" usr 

if [[ $(curl -sS https://example.com) = "online" ]]; then 
    echo "$usr is online" 
else 
    echo "$usr is offline" 
fi 

但是每次我寧願從列表中讀取,而不是輸入用戶名。

cat userlist.txt 
user1 
user2 
user3 
user4 

但我怎麼做,還是分配變量$usr到列表中的每個項目?

回答

2

您從文件中讀取每一行,並將其與循環分配給$user

#!/bin/bash 

while read usr; do 
    if [[ $(curl -sS https://example.com) = "online" ]]; then 
     echo "$usr is online" 
    else 
     echo "$usr is offline" 
    fi 
done < userlist.txt 
相關問題