我正在使用下面的脚本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"

While giving the 100 inside the script then it works as follows ..

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

Failing while trying user input :

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

推荐答案

如 comments 中所述,使用whilefor也是一种很好的做法,请参阅此处的bash manual.

我刚刚修改了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"

对脚本进行轻微修改:

#!/bin/bash
read -rp $'Please Enter your hostFile name: ' target_File
# Don't use variables in the printf format string. Use printf '..%s..' "$(date)".
printf "Starting the Ping Check %s ....\n" "$(date)"
# You Can Just use function name hence its your choice, i just removed.
# pingHost i changed to "icmp_echo" as i used that 
# function pingHost  () {
icmp_echo  () {
    target_host=${1}
    ping -c2 "${target_host}"  >/dev/null 2>&1 &&
    printf "%-32s %-10s\n" "host ${target_host}" "ping SUCCESS" ||
    printf "%-32s %-10s\n" "host ${target_host}" "ping FAILED"
}
# Use a while loop and the read command. Where The -r option to read prevents backslash interpretation.By default,read modifies each line read, 
# by removing all leading and trailing whitespace characters (spaces and tabs, if present in IFS).If that is not desired, the IFS variable may 
# be cleared
while IFS= read -r line
do
    icmp_echo  "$line" &
# Double quote to prevent globbing and word splitting.
done < "$target_File"
wait
# Don't use variables in the printf format string. Use printf '..%s..' "$(date)".
printf "Completed @=> %s" "$(date)"
printf "\n"

Linux相关问答推荐

在CefExecuteProcess;之前创建新线程会导致CEF应用程序崩溃并显示SIGTRAP

是否有例外情况需要在.gitconfig中使用?

删除第二列中数字为零的行

Linux 的 __fastfail 替代方案?

Linux TTY 操作顺序

x64 NASM 汇编程序在程序开始时显示分段错误

sed + 从没有额外空格的文本中删除单词

将(覆盖)文件移动到不同位置的同名文件夹中

输出特定字符的所有列号

RabbitMQ 安装后没有自动启动

Linux time 命令输出中 real、user 和 sys 的含义

如何在 Linux 命令行上解析 CSV 文件?

如何使用不同的出口 IP 一次运行多个 Tor 进程?

更改核心转储的位置

如何让 PHP、Symlinks 和 __FILE__ 很好地协同工作?

php.ini 更改,但在 Ubuntu 上无效

根据日期范围过滤日志(log)文件条目

Mac OS X 中的 ldconfig 等效项?

如何自动化 HTML 到 PDF 的转换?

Hierarchical分层 ldd(1)