Can someone please help!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can someone please help!
# 1  
Old 07-18-2008
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.

Code:
#!/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/

# 2  
Old 07-18-2008
I think you need a $ here:

Code:
while [ $host_name != "."]

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

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

# 3  
Old 07-18-2008
Also you should initialise c, d and e before your while loop.
# 4  
Old 07-18-2008
Hi!
Thanks for your comments. I implemented your suggestions, but it's giving me the following error.

Code:
[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..?
Code:
1)while [ "$host_name" != "."]
2)while [ $host_name != "."]

# 5  
Old 07-18-2008
I didn't spot that error, you need a space before the "]".
# 6  
Old 07-18-2008
Whoops.. my bad...

Thanks a lot!
# 7  
Old 07-18-2008
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.

Code:
#!/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

Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question