LinuxQuestions.org
Review your favorite Linux distribution.
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-28-2011, 09:14 AM   #1
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
Advanced uses of variables in Korn Shell


Hi all,

Somehow I'm not really managing this thing, which would / could be a nice way to use variables.

What I am doing is setting up compound variable in ksh. What I have is a "config" file, which has fields delimited by a semicolon

Code:
cat ${CONFIGFILE} | grep -v "^#" | grep -v "^$" | while read line
do
      WORD1=$(echo ${line} | cut -d':' -f 1)
      if [[ "${WORD1}" = "SPECIAL" ]]
      then
           echo "got a special"
           # treat specials differently
           SPECIALS[${#SPECIALS[*]}]=$(echo ${line} | cut -d ':' -f 2)
      else
           RECORD[${#RECORD[*]}]=(
               typeset ID=${WORD1}
               typeset NAME=$(echo ${line} | cut -d: -f2)
               typeset MAINMENU=$(echo ${line} | cut -d: -f3)
               typeset SUBMENU=$(echo ${line} | cut -d: -f4)
               typeset OSUSER=$(echo ${line} | cut -d: -f5)
               typeset HOST=$(echo ${line} | cut -d: -f6)
               typeset WASUSER=$(echo ${line} | cut -d: -f7)
               typeset WASPWD=$(echo ${line} | cut -d: -f8)
               typeset PROFILEPATH=$(echo ${line} | cut -d: -f9)
           )
      fi
done
What I next want is list all RECORD[*].MAINMENU items uniquely, but somehow I am not doing it right putting regexp correctly:

Code:
select choice in ${RECORD[*].MAINMENU@\b\w.*\b?!.*\b\k\b}
{
    echo ${choice}
}
What / where am I messing up?
 
Old 07-28-2011, 12:32 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please explain what you think the regex is representing?

Also, as an alternative for your current loop structure:
Code:
while IFS=":" read -r WORD1 REST
do
    if [[ $WORD1 == SPECIAL ]]
    then
        echo "got a special"
        # treat specials differently
        SPECIALS[${#SPECIALS[*]}]="${REST}"
    else
        set - ${REST//:/ }

        RECORD[${#RECORD[*]}]=(
               typeset ID=${WORD1}
               typeset NAME=$1
               typeset MAINMENU=$2
               typeset SUBMENU=$3
               typeset OSUSER=$4
               typeset HOST=$5
               typeset WASUSER=$6
               typeset WASPWD=$7
               typeset PROFILEPATH=$8
           )
    fi
done< <(egrep -v "^(#|$)" ${CONFIGFILE})
 
1 members found this post helpful.
Old 07-29-2011, 12:28 AM   #3
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Original Poster
Rep: Reputation: 111Reputation: 111
Thanks! I like that "set - ${REST//:/ }" approach. I keep forgetting how powerful that is.

\b\w.*\b?!.*\b\k\b

are actually 2 regexps, but I got errors about the use of an unexpected '(' when I wrote it like this:
(\b\w.*\b)(?!.*\b\k\b)
The first is to match a word (\w) (with any character followed by whatever ;-) with a boundary (\b) so you get the 1st regexp.
The second is to mean that the word I just found should not be followed by another occurrence of itself (\k) even if there are other characters in between (.*); same boundary applies here.

In other words, I had expected to get a list of ${RECORD[#].MAINMENU} entries as if they were sorted through uniq: in a list of "apple pear egg omelet pear", I'd get "apple pear egg omelet"

Last edited by Ramurd; 07-29-2011 at 12:30 AM.
 
Old 07-29-2011, 01:21 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I was not able to find anything to tell me about \k, but assuming that is ok, the reason you would get -
unexpected '(' - that I can see is because ? means 0 or 1 of the preceding character, so:

(\b\w.*\b) - alphanumeric followed by any character and between word boundaries

(? - zero or one lot of left round bracket

!.*\b\k\b) - assuming your understanding of \k - not any character followed by the same word inside a word boundary (of course this also now means the right hand round bracket is part of the regex which is also wrong)

So presumably removing the ? may alleviate the problem??

Unfortunately I have to wait till I get home to test as no access to ksh here
 
Old 07-29-2011, 04:51 AM   #5
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Original Poster
Rep: Reputation: 111Reputation: 111
Gave it a shot like this:
Code:
elect choice in ${RECORD[*].MAINMENU@(\b\w.*\b!.*\b\k\b)}
But now the unexpected ( shows up (again), so assumingly @ (meaning regexp if I got even that right) should not have the regexp in braces.

As should be clear by now, I've not much experience with regexp in variable arrays
 
Old 07-29-2011, 10:00 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Unfortunately ksh is not my strength either and I am struggling to find a good tutorial (if you have one i would appreciate a link) that lists this type of thing

What I have found is that the issue is not exactly to do with your regex but where you have put it:
Code:
select choice in ${RECORD[*].MAINMENU@(\b\w.*\b!.*\b\k\b)}

# should be

select choice in ${RECORD[*].MAINMENU}@(\b\w.*\b!.*\b\k\b)
Notice the move of the curly braces.

The issue we now have is that the following construct is incorrect for displaying all in an array:
Code:
echo ${RECORD[*].MAINMENU}
This results in a weird arithmetic error.

So I guess my question to you is, do you know or have notes on the correct format to display all items for a single element?
 
Old 07-29-2011, 11:17 AM   #7
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Original Poster
Rep: Reputation: 111Reputation: 111
I have not (yet), I've been putting some stuff together from fragments all over the place; That was mostly the regexp, which I took and adapted, since it was for another language. But this is something that helps me move further. Will keep you posted on some findings and solutions. I still think this is potentially a very nice way to handle data.
 
Old 07-29-2011, 02:48 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Yes I did not know that ksh was able to have multidimensional numeric and associative arrays. Would be a nice feature if added to bash

I will keep looking too, so please advise when you get a solution to this one as I am interested
 
Old 08-01-2011, 01:13 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
There's a load of ksh docs here http://kornshell.com/ and further links therein
 
Old 08-01-2011, 01:32 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
It might be helpful if you told us what the problem was rather than
how you think it should be solved.
i.e. example input and output.

There's probably a much easier way to do it. I never use arrays in my shell scripts.
 
Old 08-01-2011, 04:03 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I am not sure of the OPs exact requirements, but personally I would still like the question answered on whether or not it is possible (and if so how) to display, for
example, all of the MAINMENU (associative sub index) items for all RECORDs or if only possible if you loop through?
 
Old 08-01-2011, 03:56 PM   #12
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Original Poster
Rep: Reputation: 111Reputation: 111
For every problem multiple solutions exist; I could think up a quick and dirty one that would read my config file multiple times. However, I'm never fond of too much file i/o. In fact, I hate file i/o, and will try to reduce it as much as possible. So, what I thought up was this array-approach, as it is closest to how I would do things while programming (in C(++)): a struct or a class in that case.

So the question is NOT about how do I create my menu, but: how do I handle regular expressions to search and sort data in my (compound) array? (if it is at all possible).
I have not found a solution yet on how I can manipulate this compound array.

As a side note: as is often the case with me: sometimes I find "new" (for me) things that are possible. I'm one of those who lies to explore there, and see what's possible so I have more tools to work with, which often lead to being able to solve things that are otherwise much harder / next to impossible to solve.
 
Old 08-02-2011, 01:26 AM   #13
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
interesting,
however if you are not interested in file i/o I wonder why you are calling 'cut' at least 10 times and
'grep' at least twice?
 
Old 08-02-2011, 02:38 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well if you look at my rework it has none of those.
 
Old 08-02-2011, 04:49 AM   #15
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Original Poster
Rep: Reputation: 111Reputation: 111
bigears: I was working on something else and the rest was quick&dirty, as I was looking into the concepts of compound variables and how to manipulate in that. I know grail's code is much cleaner in that aspect, and for the sake of getting a great example, I've fixed the code for that :-)

I've been looking a bit about, and I think I found one of the big problems:
Code:
echo ${RECORD[*].MAINMENU}
is not valid syntax, which really is a pity. One of the solutions is to create a new variable and loop through the array... but doesn't that really defeat the purpose :-)
Thinking up other ways to work around this issue... Currently checking out http://oreilly.com/catalog/korn2/chapter/ch04.html#t3 and see how that brings us further.
 
  


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
[SOLVED] Command Line Substutions and Variables using Korn for First Backup Script metallica1973 Programming 28 06-21-2011 09:51 PM
Disadvantages of using a Korn shell? Heffy06 Linux - Newbie 7 02-04-2011 12:02 AM
shell variables advanced wakatana Linux - Software 15 10-13-2009 12:13 PM
Korn Shell test if multiple variables are set jonlake Programming 4 04-09-2008 10:41 AM
Korn Shell equivalent bassplayer69 Linux - General 1 08-02-2007 01:15 PM

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

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