The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Storing commands in $variables. Paulw0t Shell Programming and Scripting 7 08-08-2008 05:42 PM
Splitting the data and storing it into 2 variables jisha Shell Programming and Scripting 10 02-17-2008 10:40 PM
Getting variables into a array. vivsiv Shell Programming and Scripting 2 02-06-2008 03:45 PM
Storing pointer array in C arunkumar_mca UNIX for Dummies Questions & Answers 1 08-23-2007 08:49 AM
awk - storing data in variables 2nilotpal Shell Programming and Scripting 7 04-16-2004 05:32 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-17-2008
nua7 nua7 is offline
Registered User
  
 

Join Date: Mar 2008
Location: /bin/sh
Posts: 353
storing variables in array.Please help

Hi All,
I need some help with arrays. I need to take input from the user for hostname, username and password until he enters .(dot) or any other character and store the values in the variable array.

I would further connect to the hostname using username and passwd and copy files from server to these machines.

This is what I have come up with so far. Just can't figure out a way of storing values in the array variable. can someone help.

Code:
#!/usr/bin/sh
#Take input from the user
while ["host_name != "."]
do
echo "Please enter the Hostname"
read host_name 
echo "Please enter the username"
read username
echo "Please enter the password"
read password
done
 
#If you have a better way of accepting data from user , would be great!

#storing these variables in array logic here..
 
#ftp logic as below..
 
typeset SRCDIR="/root/temp1"
typeset DESTDIR="/root/"
typeset REMHOST="machB"
typeset -i STAT=0
cd "${SRCDIR}"
STAT=${?}
if [[ ${STAT} -ne 0 ]]
then
echo "Can't cd to ${SRCDIR}" >&2
exit ${STAT}
fi
ftpput.pl -h ${REMHOST} -l ${USER} -p ${PASSWD} -d "${DESTDIR}" -B *
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
mv * "${BACKUP_DIR}/"
STAT=${?}
echo "mv failed; status ${STAT}." >&2
else
echo "FTP failed; status ${STAT}." >&2
fi
exit ${STAT}
Can someone please help me!

Thanks!
nua7
  #2 (permalink)  
Old 07-17-2008
mirusnet's Avatar
mirusnet mirusnet is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 146
You also can use
Code:
getopt
or $1 $2 $3 ...
  #3 (permalink)  
Old 07-17-2008
nua7 nua7 is offline
Registered User
  
 

Join Date: Mar 2008
Location: /bin/sh
Posts: 353
Hey!
Thanks for the reply, but the user needs to feed input for many hostnames, so need to have a loop and store it in array, so that I can use it further.

Thanks!
nua7
  #4 (permalink)  
Old 07-17-2008
mirusnet's Avatar
mirusnet mirusnet is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 146
For example ask user to enter the number of hosts, then read all of them in cycle.
  #5 (permalink)  
Old 07-17-2008
nua7 nua7 is offline
Registered User
  
 

Join Date: Mar 2008
Location: /bin/sh
Posts: 353
I would have loved to do that, but the client thinks, that's too many questions we are asking...

So I did put a while loop at start. Any idea how to put in those variables in array.
  #6 (permalink)  
Old 07-17-2008
mirusnet's Avatar
mirusnet mirusnet is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 146
Code:
$@ $#
  #7 (permalink)  
Old 07-17-2008
nua7 nua7 is offline
Registered User
  
 

Join Date: Mar 2008
Location: /bin/sh
Posts: 353
This is somehow not working.Some tweaking req here

Thanks mirusnet

This somehow is not working..Some tweaking req here.

Code:
#!/usr/bin/sh
i=j=k=0
#Take input from the user
while ["host_name != "."]
do
echo "Please enter the Hostname"
read host_name[i]
echo $hostname[i]
let i=i+1
echo "Please enter the username"
read username[j]
echo $username[j]
let j=j+1
echo "Please enter the password"
read password[k]
echo $password[k]
let k=k+1
done
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 03:51 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0