LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-19-2011, 08:15 AM   #1
gdan2000
LQ Newbie
 
Registered: Mar 2009
Posts: 8

Rep: Reputation: 0
problem to initialize ksh array when first element includes hyphen


Hi

I'm trying to create an array with variable including hyphen

but ksh refuses the first element


set -A allArgs
set +A allArgs ${allArgs[@]} -all
set +A allArgs ${allArgs[@]} -date
set +A allArgs ${allArgs[@]} test

./test.ksh[11]: -all: bad option(s)

It happens only when first element is like this:
set +A allArgs ${allArgs[@]} all
set +A allArgs ${allArgs[@]} -date
set +A allArgs ${allArgs[@]} test

print "Array is: ${allArgs[@]}"

Array is: all -date test
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 07-19-2011, 08:24 AM   #2
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,923
Blog Entries: 44

Rep: Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 07-19-2011, 08:44 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Not a real solution but a workaround:
Code:
#!/bin/ksh
set -A allArgs
set +A allArgs ${allArgs[@]} dummy
set +A allArgs ${allArgs[@]} -date
set +A allArgs ${allArgs[@]} test
allArgs[0]=-all
echo ${allArgs[@]}
that is you can change the first element later.
 
1 members found this post helpful.
Old 07-19-2011, 08:51 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
double hyphen is pretty standard for this sort of thing...

Code:
$ set -A  x -- -1 2 3 -all
$ echo ${x[*]}       
-1 2 3 -all

Last edited by bigearsbilly; 07-19-2011 at 08:52 AM.
 
3 members found this post helpful.
Old 07-19-2011, 11:59 PM   #5
gdan2000
LQ Newbie
 
Registered: Mar 2009
Posts: 8

Original Poster
Rep: Reputation: 0
10x

this is exactly what I need
 
Old 07-20-2011, 04:06 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
just a quick note,

the double hyphen -- is standard on most sh derived shells.
It stops any further processing of switches.
So it's useful in lots of places, like

echo -- "$thing"

is a safer way to echo for example where there may be hyphens, though
echo is a bit brain dead, but you get the idea.

Last edited by bigearsbilly; 07-20-2011 at 04:11 AM.
 
1 members found this post helpful.
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
get index of an element in array in bash mangatmodi Programming 4 11-20-2009 07:45 AM
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 01:47 AM
Find whether an element exists in an array. Sherlock Programming 8 06-21-2007 07:07 AM
index of an element in the array ? thelonius Programming 1 09-24-2005 12:41 PM
C++ Six Element Array petercool Programming 2 08-20-2003 11:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration