2016-04-23 20 views
1

私はこれを行うことができます単一のユーザーを確認したい場合。リスト内の複数の項目に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 
関連する問題