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
Extracting the values separated by comma padmisri Shell Programming and Scripting 6 07-31-2009 03:13 AM
Its PERL + Comma separated seventh field ganapati Shell Programming and Scripting 17 06-24-2009 04:20 AM
validate a pattern of numbers that are comma separated 12345 UNIX for Dummies Questions & Answers 5 05-08-2009 03: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 03:46 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 6 Days Ago
Reddy482 Reddy482 is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 15
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 6 Days Ago
jsmithstl jsmithstl is offline
Registered User
  
 

Join Date: Oct 2009
Location: St. Louis, MO
Posts: 34
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 6 Days Ago
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 584
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; 5 Days Ago at 03:01 AM..
  #4 (permalink)  
Old 5 Days Ago
Reddy482 Reddy482 is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 15
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 4 Days Ago
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 584
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 On




All times are GMT -4. The time now is 02:40 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