[英]Stuck with bash script
我是這里的新手,並且使用 bash 腳本。
我的練習很少,但我已經陷入了第一個練習。
所以我要解釋一下:使用腳本,我必須檢查一個手寫的 IP 地址,例如 51.32.12.51,然后使用我創建的兩個文件(accept.txt 和 deny.txt)檢查該地址是否被接受或否認。 到目前為止,我已經這樣做了,但我完全被困在這里。
#!/bin/bash
IPS_acc="`cat accept.txt`"
IPS_den="`cat deny.txt`"
read -p "Quina IP vols comprovar? " IP
for ipfitxer in $IPS_acc $IPS_den
do
if [ "$IP" = "$ipfitxer" ]; then
echo "La $IP està acceptada explícitament"
elif [ "$IP" != "$ipfitxer" ]; then
echo "La IP $IP està denegada explícitament"
fi
done
如果有什么不明白的,我可以翻譯。
提前致謝。
這是使用grep(1)
和if-statement
一種方法
#!/usr/bin/env bash
ips_acc=accept.txt
ips_den=deny.txt
read -p "Quina IP vols comprovar? " IP
if grep -wq "$IP" "$ips_acc"; then
echo "input $IP is accepted"
elif grep -wq "$IP" "$ips_den"; then
echo "input $IP is denied"
else
echo "$P is not known!" >&2
fi
這可能與您嘗試編寫的代碼很接近,我只是通過谷歌翻譯了您代碼中的文字。
#!/usr/bin/env bash
ips_acc=accept.txt
ips_den=deny.txt
read -rp "Quina IP vols comprovar? " IP
if grep -wq "$IP" "$ips_acc" "$ips_den"; then
echo "La $IP està acceptada explícitament"
exit 0
else
echo "La IP $IP està denegada explícitament" >&2
exit 1
fi
它包含在if-statement
因此操作將取決於 grep 的退出狀態。
-w
強制 PATTERN 只匹配整個單詞。
-q
抑制所有正常輸出或靜默。
根據 OP 的原始代碼使用for loop
但需要 bash4+ 功能,因為mapfile
#!/usr/bin/env bash
ips_acc=accept.txt ##: Save the text files in variables
ips_den=deny.txt
mapfile -t accept < "$ips_acc" ##: save the value text files in an array.
mapfile -t deny < "$ips_den" ##: Using mapfile aka readarray.
main() { ##: Create a function named main
read -rp "Quina IP vols comprovar? " IP ##: ask and save user input
for ipfitxer in "${accept[@]}" "${deny[@]}"; do ##: loop through both accept and deny values.
if [[ $ipfitxer == "$IP" ]]; then ##: if input and values match
echo "La $IP està acceptada explícitament" ##: Print/send message that they did match
exit 0 ##: Exit the script with a zero (true) status
fi
done
echo "La IP $IP està denegada explícitament" >&2 ##: Send message to stderr if there was no match.
exit 1
}
main ##: run/execute main function.
這個需要exglob。
#!/usr/bin/env bash
shopt -s extglob ##: Enable shell globbing extglob.
ips_acc=accept.txt ##: save the value text files in an array.
ips_den=deny.txt
mapfile -t accept < "$ips_acc" ##: save the value text files in an array.
mapfile -t deny < "$ips_den" ##: Using mapfile aka readarray.
both=("${accept[@]}" "${deny[@]}"); ##: merge both arrays.
pattern=$(IFS='|'; printf '%s' "@(${both[*]})") ##: Format/prepare the arrays.
read -rp "Quina IP vols comprovar? " IP ##: Read/store user input
if [[ ${IP:?You did not gave an answer!} == $pattern ]]; then ##: Test if there was a match
echo "La $IP està acceptada explícitament" ##: Print/send message that they do.
exit 0 ##: Exit gracefully with zero exit status
else
echo "La IP $IP està denegada explícitament" >&2 ##: If no match send message to stderr
exit 1 ##: Exit with 1 which is an error
fi
免責聲明,我不會說你的母語。
${IP:?You did not gave an answer!}
是PE 參數擴展的一種形式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.