我需要能夠搜索用戶檔案并確定用戶的輸入是否在該檔案中。這不起作用
if [ $uname == awk '{print $1}' user_file ]; then echo "success"; fi
基本上,如果名稱在我的 user_file 中,我正在尋找一種回傳 true 的方法。我發現使用 if/then 的任何示例都處理整數進行比較。你不能在bash中比較字串嗎?
user_file 包含
username1 password
username2 password2
我正在嘗試撰寫一個腳本來檢查該 user_info 檔案,如果用戶輸入了正確的用戶名和密碼,那么它將執行一些其他操作。
if [ username/password in user_info ]; then [ do some other action ]
(是的,這是一門課的作業。老師不是很好,所以我一直在谷歌上搜索并試圖自己弄清楚很多事情。所以我不想得到我的腳本問題的確切答案,我只需要弄清楚 if/then 回圈如何處理字串。)
uj5u.com熱心網友回復:
if awk -v uname="$uname" '$1=="uname"{f=1; exit} END{exit !f}' user_file; then echo "success"; fi
uj5u.com熱心網友回復:
我建議bash:
#!/bin/bash
read -p "Enter username: " input
while read -r user pass; do
if [[ "$user" == "$input" ]]; then
echo "success"
break # exit from while loop
fi
done < user_file
uj5u.com熱心網友回復:
uname在"${u1}"這里,并且pword是"${p1}"
我故意將它的唯一副本放在倒數第二行,以確保它必須掃描整個檔案 -
除非您正在談論9 GB或更大的檔案,否則我認為一旦全部加載而不是逐行并零碎地進行掃描,就可以節省更多時間進行掃描-1.35GB確認輸入是否在1.2 secs:::
% f='test_userpass2.txt' ;
( time ( pvE0 < "${f}" \
\
| mawk2 '
END { exit RS^index($ RS,__) # this version won't match
# because i hyphenated them
}' RS='^$' __="${u1}-${p1}" FS='^$' \
)
echo "\n\n exit code : ${?}\n" ) | ecp
sleep 3;
( time ( pvE0 < "${f}" \
\
| mawk2 '
END { exit RS^index($ RS,__)
}' RS='^$' __="${u1} ${p1}" FS='^$' ) ;
echo "\n\n exit code : ${?}\n" ) | ecp;
in0: 1.35GiB 0:00:00 [3.46GiB/s] [3.46GiB/s][========>] 100%
( pvE 0.1 in0 < "${f}" | mawk2 'END { exit RS^index($ RS,__) }' RS='^$' ; )
0.54s user 0.64s system 101% cpu 1.160 total
exit code : 1
in0: 1.35GiB 0:00:00 [3.43GiB/s] [3.43GiB/s][=========>] 100%
( pvE 0.1 in0 < "${f}" | mawk2 'END { exit RS^index($ RS,__) }' RS='^$' ; )
0.56s user 0.61s system 98% cpu 1.191 total
exit code : 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/468662.html
