linux - shell 脚本 - 如何进行多次读取?

标签 linux bash shell

我无法给出太多解释,因为我的英语很糟糕。我想要做的示例如下;

尝试.sh

#!/bin/sh
echo -n "Enter the port:"
read ports
if [ ! -d "$ports" ]; then

mkdir -p /root/port$ports
echo "The folder was created for $ports"
wget -q www.example.com/example.tar.bz2
tar -xjf example.tar.bz2
su root -c "screen -A -m -d -S 'example$ports' ./example -RunningAsRootIsEvilAndIKnowThat"
echo "$ports started."
else
exit 0
fi

腻子;

root@ubuntu:~# sh try.sh
Enter the port: 4445, 4546
Created folder 'port4445'
Created folder 'port4546'
4445 started.
4546 started.

我该如何制作?

最佳答案

#!/bin/bash
IFS=, read -p "Enter the Hosts: " -ra hosts </dev/tty
IFS=, read -p "Enter the Port: " -ra ports </dev/tty

for host in "${hosts[@]}"; do
    for port in "${ports[@]}"; do
        nc -vz $host $port
    done
done

运行时:

bash try.sh
Enter the Port: 80
Enter the Hosts: 127.0.0.1
localhost [127.0.0.1] 80 (http) open

nc -vz $host $port 将检查 $host 是否正在监听 $port

IFS=, 分割输入,在我们的例子中使用 , 将其视为输入分隔符。

-p 显示消息。

-a 将输入放入数组

关于linux - shell 脚本 - 如何进行多次读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42549714/

相关文章:

linux - 从 Windows 和 Eclipse 连接到 gitolite 服务器

linux - 为什么 shell 脚本中的 'read' 命令缺少初始字符?

linux - 如何grep多个匹配一行?

linux - linux 中的一个软件的名称,它可以使一个控制台窗口通过在它们之间切换来使用多个 shell

regex - 列出所有不以数字开头的文件

python - 如何将多值 CSV 转换为 Json

linux - ffmpeg 每次运行返回不同的结果

linux - shell 语法错误

regex - grep和sed结合时如何使用变量名?

bash - 逐行读取文件,然后作为不同的变量进行处理