Can someone please help!

Hi!
I am trying to write a little script here, which would take input from the user and store them in arrays. But the array part is not working for some reason.

Can anyone help please.

#!/usr/bin/sh
##Take input from the user
while [ host_name != "."]
do
  echo "Please enter the host_name"
  read host_name
  echo "host_name is $host_name"
  store_hostname[$c]=$host_name
  c=$(( c + 1 ))
  echo "Please enter the username"
  read username
  echo $username
  store_username[$d]=$username
  d=$(( d + 1 ))
  echo "Please enter the password"
  read password
  echo $password
  store_password[$e]=$password
  e=$(( e + 1 ))
done

#Create a destination directory
ssh $store_host_name[c] mkdir temp
#scp files to host
scp -r /root/temp $store_username[c]@$store_host_name[c]:/root/

I think you need a $ here:

while [ $host_name != "."]

Also you need to delimit the variable names with {} for arrays, and you still need the $ before the array index.:

ssh ${store_host_name[$c]} mkdir temp
scp -r /root/temp ${store_username[$c]}@${store_host_name[$c]}:/root/

Also you should initialise c, d and e before your while loop.

Hi!
Thanks for your comments. I implemented your suggestions, but it's giving me the following error.

[root@iqmango nua7]# sh testing.sh 
testing.sh: line 4: [: missing `]'
ssh: mkdir: Name or service not known
ssh: : Name or service not known
lost connection

I also tried following things on the while loop, but the same syntax error:
Any idea..?

1)while [ "$host_name" != "."]
2)while [ $host_name != "."]

I didn't spot that error, you need a space before the "]".

Whoops.. my bad...

Thanks a lot!

Hi!
since this is my first script using arrays and user input, I am facing too many problems.

In the script below, even if I enter "done" as a hostname , it still continues to go further and ask username and password.

#!/usr/bin/sh
##Take input from the user
c=d=e=0
while [ "$host_name" != done ] 
do
  echo "Please enter the host_name"
  read host_name
  store_hostname[$c]=$host_name
  c=$(( c + 1 ))
  echo "Please enter the username"
  read username
  echo $username
  store_username[$d]=$username
  d=$(( d + 1 ))
  echo "Please enter the password"
  read password
  echo $password
  store_password[$e]=$password
  e=$(( e + 1 ))
done
for i in c
do
#Create a destination directory
ssh -q ${store_username[$d]}@${store_host_name[$c]} mkdir temp
#scp files to host
scp -r /root/temp ${store_username[$c]}@${store_host_name[$c]}:/root/
done
  ...
  read host_name
  if [ "$host_name" = done ] ; then break; fi
  ...

Also for i in c is not valid syntax. Also, why use three indices (c, d and e) when just one will do?

I have to take user input till user enters "done" , so I used the while loop.I have added the if line code after the read statement.

Also , I need to have a different count of all three arrays , so kept different variables.

I know I kept on bugging you for small queries , but this script simply won;t work!

I just need to pick up hostnames, usernames and passwords from the user and put some files/folder onto the hosts.

Can anyone give me a script that would work. (I know this is too much of asking...)

Here's the modofied version:

#!/usr/bin/sh
##Take input from the user
c=d=e=0
while [ "$host_name" != done ] 
do
  echo "Please enter the host_name"
  read host_name
  if [ "$host_name" = done ] ; then break; fi
  store_hostname[$c]=$host_name
  c=$(( c + 1 ))
  echo "Please enter the username"
  read username
  echo $username
  store_username[$d]=$username
  d=$(( d + 1 ))
  echo "Please enter the password"
  read password
  echo $password
  store_password[$e]=$password
  e=$(( e + 1 ))
done

len= size of array
for i in $len
do
#Create a destination directory
ssh -q ${store_username[$d]}@${store_host_name[$c]} mkdir temp
#scp files to host
scp -r /root/temp ${store_username[$c]}@${store_host_name[$c]}:/root/
done
len= size of array
for i in $len
do
#Create a destination directory
ssh -q ${store_username[$d]}@${store_host_name[$c]} mkdir temp
#scp files to host
scp -r /root/temp ${store_username[$c]}@${store_host_name[$c]}:/root/
done

--change to ->

len= ${#store_username[@]}
for(( i =0; i<$len; i++))
do

#Create a destination directory
ssh -q ${store_username[$i]}@${store_host_name[$i]} mkdir temp
#scp files to host
scp -r /root/temp ${store_username[$i]}@${store_host_name[$i]}:/root/

done