The UNIX and Linux Forums  


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



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

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-15-2009
Registered User
 

Join Date: Nov 2009
Posts: 16
Parsing Comma separated Arguments

Hi unix guru's

I want to execute a shell script like ksh printdetails.ksh Andy,Bob,Daisy,Johnson

like passing all the four names in the as the arguments and these arguments are varies between 1 to 10.
How to pass these names to the shell script variable.
and also i want to know the count of arguments passed to script.

Thanks in advance.
  #2 (permalink)  
Old 11-15-2009
Registered User
 

Join Date: Oct 2009
Location: St. Louis, MO
Posts: 78
Here's one way.


Code:
#!/bin/ksh

VARS=$1

set -A A_NAME $( echo "$VARS" | sed 's/,/ /g' )

NUM_VARS=${#A_NAME[@]}

while [ -n "${A_NAME[i]}" ]
do
   echo "name = ${A_NAME[i]}"
   (( i = i + 1 ))
done

echo "Total Names:  ${NUM_VARS}"



Code:
./names.ksh mike,bill,jim,john

name = mike
name = bill
name = jim
name = john

Total Names:  4

  #3 (permalink)  
Old 11-15-2009
Registered User
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 1,258
And here is another way:

Code:
#!/bin/ksh
oIFS="$IFS"; IFS=, ; set -- $1 ; IFS="$oIFS"
for i in "$@"; do
  echo name = $i
done
echo Total Names: $#


Code:
$>./test Cooper,"Billy Bob",Trevor,Smithers
name = Cooper
name = Billy Bob
name = Trevor
name = Smithers
Total Names: 4


Last edited by Scrutinizer; 11-15-2009 at 03:01 AM..
  #4 (permalink)  
Old 11-15-2009
Registered User
 

Join Date: Nov 2009
Posts: 16
Hi Scrutinizer

is there any way that we can do this job workout $1
because when i use $1 it was defaultly assuming my environment variable
  #5 (permalink)  
Old 11-16-2009
Registered User
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 1,258
Hi Reddy, I do not understand what you mean exactly, could you be specific and/or give an example?
Sponsored Links
Reply

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 Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extracting the values separated by comma padmisri Shell Programming and Scripting 6 07-31-2009 04:13 AM
Its PERL + Comma separated seventh field ganapati Shell Programming and Scripting 17 06-24-2009 05:20 AM
validate a pattern of numbers that are comma separated 12345 UNIX for Dummies Questions & Answers 5 05-08-2009 04:47 AM
comma separated string manipulation saharookiedba Shell Programming and Scripting 2 02-13-2009 03:24 AM
Unix Comma Separated to Excel Column ravzter UNIX Desktop for Dummies Questions & Answers 3 07-31-2008 04:46 AM



All times are GMT -4. The time now is 12:04 PM.


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