我正在使用下面的腳本來 ping 多個 Linux 主機,如果它將hostfile通過硬編碼放入腳本本身,它作業得非常好,但我希望它基于用戶輸入。
當我使用read該檔案作為用戶的輸入但那會失敗時,請建議我在這里做錯了什么。
#!/bin/bash
read -rsp $'Please Enter your hostFile name: ' target_File
echo $target_File
printf "$(date) Starting the Ping Check...."
function pingHost () {
target_host=${1}
ping -c2 ${target_host} >/dev/null 2>&1 &&
printf "%-20s %-10s\n" "host ${target_host}" "ping SUCCESS" ||
printf "%-20s %-10s\n" "host ${target_host}" "ping FAILED"
}
#
# Variable "data" seeks the filename containing host list.
data="$(< target_File)"
for line in $data
do
pingHost ${line} &
done
wait
printf "Completed @=> $(date)"
printf "\n"
在給出hostfile腳本內部的同時,它的作業方式如下..
data="$(< hostfile)" <-- this hard-coded file containing hosts
$ ./ping_parallel.bash
Sun May 22 10:55:24 IST 2022 Starting the Ping Check....
host savfav0194 ping SUCCESS
host savfav0268 ping SUCCESS
host savfav0263 ping SUCCESS
host savfav0196 ping SUCCESS
host savfav0260 ping SUCCESS
host savfav0259 ping SUCCESS
host savfav2088 ping SUCCESS
host savfav2135 ping SUCCESS
host savfav2136 ping SUCCESS
host savfav3088 ping SUCCESS
host savfav0257 ping SUCCESS
host savfav0262 ping SUCCESS
host savfav0261 ping SUCCESS
host savfav0270 ping SUCCESS
host savfav0255 ping SUCCESS
host savfav0265 ping SUCCESS
host savfav0266 ping FAILED
Completed @=> Sun May 22 10:55:26 IST 2022
嘗試用戶輸入時失敗:
Please Enter your hostFile name: target_File
Sun May 22 10:58:54 IST 2022 Starting the Ping Check...../ping_parallel.bash: line 22: target_File: No such file or directory
Completed @=> Sun May 22 10:58:54 IST 2022
uj5u.com熱心網友回復:
如評論中所述,也是使用whileover的好習慣for,請參閱此處的bash 手冊。
我剛剛修改了它while,希望它應該適合你!
#!/bin/bash
read -p $'Please Enter your hostFile name: ' target_File
printf "$(date) Starting the Ping Check...."
function pingHost () {
target_host=${1}
ping -c2 ${target_host} >/dev/null 2>&1 &&
printf "%-20s %-10s\n" "host ${target_host}" "ping SUCCESS" ||
printf "%-20s %-10s\n" "host ${target_host}" "ping FAILED"
}
while read -r line;
do
pingHost $line &
done < $target_File
wait
printf "Completed @=> $(date)"
printf "\n"
uj5u.com熱心網友回復:
你說這不起作用(更改data=$(< target_File)為data=$(< $target_File))
$ target_File=hosts.txt
$ data=$(< target_File)
bash: target_File: No such file or directory
$ echo $data
$ data=$(< $target_File)
$ echo $data
google.com amazon.com
# seems to be fine
$ for x in $data; do echo pingit $x; done
pingit google.com
pingit amazon.com
uj5u.com熱心網友回復:
繼續評論和請求答案,您的腳本有一些小問題需要解決。首先,始終將您的腳本粘貼到ShellCheck并在此處發布之前修復所有問題(這確實非常好)。
發現的其他問題(一些材料,一些形式)是:
- POSIX 函式定義很簡單
name() { body }。bashism這個詞function允許將函式定義為function name { body },是的 bash 甚至可以接受組合。對于可移植腳本,始終name() { body }只使用。 - 不要在
printfformat-string中嵌入可擴展的文本或變數。這就是轉換說明符的用途。不要將固定文本與您通過format -string中的format -specifiers發送的字串中的變數結合起來。固定文本進入格式字串本身,例如
不是
echo $target_File
printf "$(date) Starting the Ping Check...."
#...
printf "%-20s %-10s\n" "host ${target_host}" "ping SUCCESS"
反而
printf "%s\n%s Starting ping check...\n" "hostsfile" "$(date)"
#...
printf "host %-20s ping SUCCESS\n" "$1"
- 驗證,驗證,驗證 - 尤其是用戶輸入和檔案名
- 不要使用
list=$(<file)andfor i in "$list"; do ... done,而是while read -r line; do ... done < file
將所有部分放在一起,您可以執行類似于以下的操作:
#!/bin/bash --norc
## function
pinghost() {
## validate argument provided
[ -z "$1" ] && {
printf "error: no host provided.\n" >&2
return;
}
if ping -c2 "$1" >/dev/null 2>&1 ; then ## check ping success / failure
printf "host %-20s ping SUCCESS\n" "$1" ## use fixed-text / specifiers properly
else
printf "host %-20s ping FAILED\n" "$1"
fi
}
## destination hosts, validate filename given and file exists and has non-zero size
if [ -n "$1" ] && [ -s "$1" ]; then
hostsfile="$1"
else
printf "error: hostsfile argument required to non-zero size file.\n" >&2
exit 1
fi
## never embed expandable output in the printf format-string, use proper spedifiers
# printf "$(date) Starting the Ping Check...."
printf "%s\n%s Starting ping check...\n" "$hostsfile" "$(date)"
## loop over each line in hosts file
while read -r host; do
pinghost "$host" ## no sense in backgrounding just to wait
done < "$hostsfile"
printf "Completed @=> %s\n" "$(date)" ## the '\n' belongs in the format-string
如果您還有其他問題,請告訴我。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/479392.html
