storing variables in array.Please help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting storing variables in array.Please help
# 1  
Old 07-17-2008
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  
Old 07-17-2008
You also can use
Code:
getopt

or $1 $2 $3 ...
# 3  
Old 07-17-2008
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  
Old 07-17-2008
For example ask user to enter the number of hosts, then read all of them in cycle.
# 5  
Old 07-17-2008
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  
Old 07-17-2008
Code:
$@ $#

# 7  
Old 07-17-2008
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing the SQL results in array variables

Requirement 1) I need to execute 15 SQL queries in oracle through linux script. All these query results needs to be stored in array variables. Requirement 2) And these 15 queries needs to be executed in parallel. Requirement 3) Once all the queries executed then the shell script should... (3 Replies)
Discussion started by: Niranjancse
3 Replies

2. Programming

C; storing strings in an array

I am trying to get userinput from stdin and store the lines in an array. If i do this: using a char **list to store strings allocate memory to it #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { char *prog = argv; char **linelist; int... (5 Replies)
Discussion started by: tornow
5 Replies

3. Shell Programming and Scripting

Storing outputs into variables

I need to know how to store output from one command so that it can initiate another command. chktraf -s | cut -c1-4 output would look like 321 142 256 342 123 Then if the value of the output = 0, then initiate next command. if then for xx in 01 02 03 04 05 06 07 08 09 10 do ... (4 Replies)
Discussion started by: Shaun74
4 Replies

4. UNIX for Dummies Questions & Answers

Storing variables and using them..

Hello Apologies for not having the most accurate of thread titles.. I'm using IBM Rational Synergy CM software. I use the Synergy commands in tandem with Unix commands. I have a directory containing source code objects: bash-3.00$ ccm ls *.fmb *.rdf *.pll *.mmb cre_applications.fmb-1... (1 Reply)
Discussion started by: Glyn_Mo
1 Replies

5. Shell Programming and Scripting

Storing awk output into variables

Hi all, Currently, i have a log file seperated by 'tab' and each record starting with a new line. i managed to retrieve the column that i'm interested in. (source_ip_address: xxx.xxx.xxx.xxx). example of awk output: '{ print $43 }' assuming the field is at column 43. 10.10.10.10... (4 Replies)
Discussion started by: faelric
4 Replies

6. Shell Programming and Scripting

perl: storing regex in array variables trouble

hi this is an example of code: use strict; use warnings; open FILE, "/tmp/result_2"; my $regex="\\ Starting program ver. (.*)"; my $res="Program started, version <$1> - OK.\n"; while (<FILE>) { if ($_ =~ /($regex)/) { print "$res"; } } close FILE; This finds $regex and print... (3 Replies)
Discussion started by: xist
3 Replies

7. Shell Programming and Scripting

Storing commands in $variables.

Hi I'm trying to store commands in variables... like so.. # lastcmd=" $t1 | $t2 | $t3 | $t4 | sort | uniq" t1="sed -e 's/http:/<li><a href=\"http:/'" t2="sed -e 's/http:.*/&\">&<\/a>Web Link<br>/'" t3="sed -e 's/.*. mailto:/<li><a href=\"mailto:/'" t4="sed -e... (7 Replies)
Discussion started by: Paulw0t
7 Replies

8. Shell Programming and Scripting

Splitting the data and storing it into 2 variables

Hi, I have a file in the given format: |aaa |hhh |fat hat chat |make sure I need to get store in a variable,say 'error' the value of the string 'fat hat chat' In short, echo $error should give the result fat hat chat Is this possible using awk? Also, can this be split.For... (10 Replies)
Discussion started by: jisha
10 Replies

9. UNIX for Dummies Questions & Answers

Storing pointer array in C

All .. I am having a pointer array . And trying to store the addess into that pointer array . please see below the problem i faced code: int cnt1; char *t_array; char *f_array; for(cnt1=0; cnt1<1000; cnt1++) { t_array =... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

10. Shell Programming and Scripting

awk - storing data in variables

In AWK script how do I store data in variables for later use. I have a multiline input and I do not want to print the data read on the console Thnaks in advance. Nilotpal. (7 Replies)
Discussion started by: 2nilotpal
7 Replies
Login or Register to Ask a Question