-2

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

This is an example.

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.

I need shell script, not bash.

Can
  • 7
  • at least, what's the current problem – lese Mar 02 '17 at 10:02
  • No problem.I just want it to be plural. For the entered value, I want to repeat the given commands for those values. – Can Mar 02 '17 at 10:04
  • Cross-posted: http://stackoverflow.com/questions/42549714/shell-script-how-can-i-make-multiple-read – muru Mar 02 '17 at 10:32

1 Answers1

1

Your script shows that you are using #!/bin/sh in shebang line. That might be an issue, since some default shells dont support arrays.. An example of that would be dash on Ubuntu. In case where you absolutely must use default shell that cannot use arrays, see Stephane's answer in the link. Alternatively, one could take advantage of word splitting like so:

read -p "Enter ports(space separated):" ports
for port in $ports
do
       [ -d "/root/port$port" ] || mkdir -p "/root/port$port" && echo "Created /root/port$port"

done

On the other hand, if we could use a shell with more capabilities ( and in many cases an OS has more than one shell), it would be better to use an array for storing ports. General approach for that I would take when arrays are available ( bash, ksh, mksh,zsh shells ) is like this:

printf "Enter ports(space separated):"
read ports
arr=( $ports  )

Here we're taking advantage of having string , for example, 80 22 23 being split at spaces (known commonly as word splitting) and provide those individual values to array arr. This is far from ideal, but works.

If you can use bash, it's possible to take advantage of read -a to read all items as array, and then iterate over the values. For example:

bash-4.3$ read -p "Enter ports(space separated):" -a portsEnter ports(space separated):80 22 25
bash-4.3$ echo ${ports[0]}
80
bash-4.3$ echo ${ports[1]}
22
bash-4.3$ echo ${ports[2]}
25

Same approach could be used on ksh with read -A.

Then you could do something like this:

for port in ${ports[@]};
do
   [ -d "/root/port$port" ] || mkdir -p "/root/port$port" && echo "Created /root/port$port"
done

I am not familiar with csh so won't provide solution for that, but the general idea should be similar

  • I do not use Bash. I need shell script. – Can Mar 02 '17 at 10:05
  • 1
    @MehmetCanErtanLastOfDead bash is a shell and is the default shell on many systems. If you need a specific shell language, you need to mention that in your question. On many systems, sh is actually bash in POSIX mode anyway. – terdon Mar 02 '17 at 10:53
  • 2
    @serg the OP seems to be on Ubuntu, so their sh is dash. You might want to add a solution without the arrays. Something like for port in $ports will actually work for space separated strings. – terdon Mar 02 '17 at 10:56
  • Answer edited, added example for basic bourne-like shell – Sergiy Kolodyazhnyy Mar 02 '17 at 16:46