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

时间:2017-03-02 07:58:32

标签: linux bash shell

我不能给出太多解释,因为我的英语不好。我想做的例子如下:

try.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.

我该怎么做?

2 个答案:

答案 0 :(得分:2)

#!/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将输入放入数组

答案 1 :(得分:-2)

使用$ *来读取参数:

 echo $* | awk '{ i=1; while(i<=NF) { print $i; i++; } }'

 for i in $*; do
   echo $i 
 done