-2

I can not give much explanation because my English is bad. The example of what I want to do is below;

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

Putty;

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

How can i make?

4
  • 1
    And what have you tried in shell script? Commented Mar 2, 2017 at 7:59
  • Nothing. I do not know how to do it. Commented Mar 2, 2017 at 8:01
  • Why do you use , as the delimiter? I would simply use space Commented Mar 2, 2017 at 8:10
  • It will be in SPACE, it will not matter. I just gave an example. Commented Mar 2, 2017 at 8:12

2 Answers 2

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

Runtime:

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 will check if the $host is listening on $port

IFS=, splits input, In our case with , Think of it as input separator.

-p To display message.

-a puts input into array

Sign up to request clarification or add additional context in comments.

2 Comments

Changing the script to expect whitespace-separated tokens instead of comma-separated tokens would simplify the logic and integrate better with other tools.
Sure, Just change the IFS. The example provided by OP has ,
-2

use $* to read arguments:

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

or

 for i in $*; do
   echo $i 
 done

4 Comments

I do not want for Bash. I need it for Shell script
This works in generic sh but it seems to be the answer to a different (though tangentially related) question. I think it's trying to say "don't read input interactively in the first place; make your script accept the port numbers as command-line arguments instead, and both you and we will be happier".
Downvote for the lack of quoting and the silly Awk workaround for no obvious benefit. The proper way is to use "$@" (yes, with the double quotes) instead of $* and to quote "$i" inside the loop.
This script works for me. it is not compulsory to include double quotes

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.