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
AWK set FILENAME via user input timj123 Shell Programming and Scripting 2 02-24-2008 03:05 PM
awk user input gefa Shell Programming and Scripting 17 10-30-2007 05:01 AM
Accepting user input in c shell skumar11 Shell Programming and Scripting 3 09-09-2007 06:32 PM
Accepting user input in Bourne shell and using sed Pits Shell Programming and Scripting 1 09-09-2007 09:39 AM
Getting user input stevefox Shell Programming and Scripting 3 02-16-2007 02:09 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-14-2007
user_prady user_prady is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 163
Smile sh shell user input and stote it to a array

Dear Friends,

I am doing a sh shell script , But I dont have any idea how to read value from user Keyboard and store them to an array .. Is it possible or not I am not also sure in sh shell script ?
EX:-
#! /bin/sh

read DATA
echo "DATA[1] -" $DATA[1]
echo "DATA[3] -" $DATA[2]
echo "DATA[3] -" $DATA[3]


if my input line is - 300 200 100. Then it should display

DATA[1] -300
DATA[2] -200
DATA[3] -100

If it can be store in array then is $# will give me total no of array or anything else ?
or The only way to store is
read DATA[1] DATA[2] DATA[3] in sh script ?

I need a drop of Knowledge from you friends..

Thanks in advance.
user_prady

Last edited by user_prady; 11-14-2007 at 02:18 AM..
  #2 (permalink)  
Old 11-14-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,952
something like this,

Code:
echo "300 200 100" | awk '{ split($0, arr, " "); for ( i in arr ) { print arr[i] } }'
  #3 (permalink)  
Old 11-14-2007
user_prady user_prady is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 163
Quote:
Originally Posted by matrixmadhan View Post
something like this,

Code:
echo "300 200 100" | awk '{ split($0, arr, " "); for ( i in arr ) { print arr[i] } }'
Oh oh..
But I think you did not get my problem..

Anyway Thanks for the reply

Like in csh shell script

#!/bin/csh -f

echo 'enter a line'
set userline = $<
echo $userline
set uline = ($userline)
echo $uline[1]
echo $uline[2]
echo $uline[3]
echo $uline[4]

If We enter 20 10 30 70
and I guess $# will give me total no index in that array..
Then it ll display
20
10
30
70
like this I want in sh shell script not in awk or nawk . I am not sure it is possible or not ?

Hope this time my explanation is better than before

Thanks again
user_pradyu
  #4 (permalink)  
Old 11-14-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by user_prady View Post
I am doing a sh shell script , But I dont have any idea how to read value from user Keyboard and store them to an array .. Is it possible or not I am not also sure in sh shell script ?
sh does not have support for arrays but ksh does.
  #5 (permalink)  
Old 11-14-2007
user_prady user_prady is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 163
Quote:
Originally Posted by porter View Post
sh does not have support for arrays but ksh does.
Thanks for your reply...
can I have a ksh code for the above problem ?
  #6 (permalink)  
Old 11-14-2007
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,913
OK, here is an example.

#!/bin/ksh

set -A parms $*
print "You supplied ${#parms[@]} parameters:"

echo 'Enter a line'
read x
echo 'You entered this line: ' $x
set -A rarray $x
print "You entered ${#rarray[@]} items"
print ${rarray[0]}
print ${rarray[1]}
print ${rarray[2]}
  #7 (permalink)  
Old 11-14-2007
user_prady user_prady is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 163
Quote:
Originally Posted by fpmurphy View Post
OK, here is an example.

#!/bin/ksh

set -A parms $*
print "You supplied ${#parms[@]} parameters:"

echo 'Enter a line'
read x
echo 'You entered this line: ' $x
set -A rarray $x
print "You entered ${#rarray[@]} items"
print ${rarray[0]}
print ${rarray[1]}
print ${rarray[2]}

Thaaaaaaaaaaak You ..............Love the people out here..
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 08:12 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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