LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-22-2015, 06:18 AM   #1
luvshines
Member
 
Registered: Apr 2009
Posts: 74

Rep: Reputation: 16
Unhappy ksh unset on first element of associative array unsets the whole array


I being boggled down since last 26 hrs with a weird issue in ksh with associate array and unset

I have a function
Code:
function setMyParms
{
  nameref refOptions=$1     # This is an associate array from parent script
  typeset -a cmd
    ...
  for key in "${!refOptions[@]}"; do
    cmd=('text')
    cmd+=('text')
    cmd+=('text')
  ...
    execCMD cmd
  done
}

function execCmd
{
  nameref refCmd=$1
  typeset cmd
      print "Before unsetting refCmd[0], array length is ${#refCmd[@]}"
    for arg in "${refCmd[@]}"
    do   
      print "${arg}"
    done 

    unset 'refCmd[0]'

    print "After unsetting refCmd[0], array length is ${#refCmd[@]}"

    for arg in "${refCmd[@]}"
    do   
      print "${arg}"
    done 
}
This code is behaving very weird.
The first invocation of execCmd from setParms 'for' loop, the 'unset refCmd[0]' is removing the entire array.

Code:
execCMD:> print 'Before unsetting refCmd[0], array length is 3'
execCMD:> print 2Fusr%2Fbin%2Fnet%20conf%20delparm
execCMD:> print global
execCMD:> print client%20use%20spnego
execCMD:> unset 'refCmd[0]'
execCMD:> print 'After unsetting refCmd[0], array length is 0'
It runs fine for the rest of the for loop though.

I am not able to debug why the unset removes all the elements, any only for first invocation

Last edited by luvshines; 05-22-2015 at 11:50 AM.
 
Old 05-22-2015, 07:21 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Please create a complete, minimal example-script that shows the problem. And please give the expected and the actual results.
 
Old 05-23-2015, 08:17 AM   #3
luvshines
Member
 
Registered: Apr 2009
Posts: 74

Original Poster
Rep: Reputation: 16
OK !!
So more debugging into this revealed the following.

The project I am working on is having 2 versions of ksh

Code:
#/usr/local/bin/ksh --version
  version         sh (AT&T Research) 93t+ 2010-06-21
And
Code:
# /usr/bin/ksh --version
  version         sh (AT&T Research) 93u+ 2012-08-01
And I am observing the bug with the 2010 version. The newer one has this fixed
Unfortunately, the project is using the older version only.

I tried to look into the changelogs of ksh releases from here http://www2.research.att.com/~astopen/download/ but couldn't pin point to the exact fix which fixed this in later version.
 
Old 05-24-2015, 11:51 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
You sure that you couldn't use some actual programming language? (awk, perl, php, python -- just to name a few)
 
Old 05-29-2015, 07:27 AM   #5
luvshines
Member
 
Registered: Apr 2009
Posts: 74

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by NevemTeve View Post
You sure that you couldn't use some actual programming language? (awk, perl, php, python -- just to name a few)
I don't have that privilege. Have to work within the existing project environment and the chosen language only.
And not to forget, with the buggy ksh version only

Marking this as SOLVED, since there is not much we can do here.
Have informed the management about the bug and it's upto them now.

Last edited by luvshines; 05-29-2015 at 07:28 AM. Reason: typos
 
  


Reply

Tags
array, ksh



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
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 09:48 PM
Does calling an element of an array calls the whole array unkn(0)wn Programming 3 07-06-2012 07:50 PM
problem to initialize ksh array when first element includes hyphen gdan2000 Programming 5 07-20-2011 04:06 AM
exchange value and key per element in associative array rblampain Programming 3 04-02-2006 09:07 AM

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

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