Project Home
Project Home
Trackers
Trackers
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Shell question: (16 Items)
   
Shell question  
I would like to acheive something like:

export `echo ABC=2 3 DEF=4`

What I want to do is ABC="2 3"

The problem is that export complains about "3" not having an identifier. I can understand that.  I tried all sort of 
wrapping with ' and " but to no avail.  Suggestions?
Re: Shell question  
On Thu, 2008-04-03 at 19:08 -0400, Mario Charest wrote:
> 
> I would like to acheive something like:
> 
> export `echo ABC=2 3 DEF=4`
> 
> What I want to do is ABC="2 3"
> 
> The problem is that export complains about "3" not having an
> identifier. I can understand that.  I tried all sort of wrapping with
> ' and " but to no avail.  Suggestions?

Well, the problem to resolve is unclear, so the suggestion will be
equally unclear :-)

It's also not clear what shell you are using, which doesn't help clarify
things :-)  On nto, there's ksh/sh or esh.

A) Setting for all subsequent commands (ksh/sh):

export ABC="2 3"; export DEF=4

B) Setting for only myprogram (ksh/sh):

ABC="2 3" DEF=4 ./myprogram

C) if you are NOT using ksh/sh:

env ABC="2 3" DEF=4 myprogram

D) If you want to be creative with ksh/sh:

myenv () {
cat <<EOF
ABC 2 3
DEF 4
EOF
}
myenv | ( while read var value; do eval "export $var=\"\$value\""; done;
myprogram )


Etc. :-)
RE: Shell question  
Hi Mario,

What, exactly, are you trying to achieve? Do you want to have both ABC and
DEF set? Or did you want ABC="2 3 DEF=4"? I'm a little confused why you need
the echo :) 

Cheers,
Keith

-----Original Message-----
From: Mario Charest [mailto:mcharest@zinformatic.com] 
Sent: April 3, 2008 19:08
To: general-community
Subject: Shell question


I would like to acheive something like:

export `echo ABC=2 3 DEF=4`

What I want to do is ABC="2 3"

The problem is that export complains about "3" not having an identifier. I
can understand that.  I tried all sort of wrapping with ' and " but to no
avail.  Suggestions?

_______________________________________________
General
http://community.qnx.com/sf/go/post6448
Re: RE: Shell question  
Stephane, Keith

Let me try again ;-) I'm using QNX6's ksh.

export `echo ABC=2 3 DEF=4`

After running this command what i want is for ABC to be equal to "2  3" and for DEF to be equal to for.  The reason why 
I'm using echo is just to simplify the problem. Here is the whole deal.  For quite some time we have been using the 
command below to turn a configuration file into list of shell variables:

export `grep -v "#" ConfigFile`

the grep -v is to remove any line made up of comments ( crude but good enough )

The ConfigFile looks like this

#blablab
HEIGHT=52
WIDTH=33
OPTIONS=-z

The problem happen when the config file looks like

#blablab
HEIGHT=52
WIDTH=33
OPTIONS=-z -y -u

The -y -u will confuse the export command, and that is what I'm trying to fix.


> Hi Mario,
> 
> What, exactly, are you trying to achieve? Do you want to have both ABC and
> DEF set? Or did you want ABC="2 3 DEF=4"? I'm a little confused why you need
> the echo :) 
> 
> Cheers,
> Keith
> 
> -----Original Message-----
> From: Mario Charest [mailto:mcharest@zinformatic.com] 
> Sent: April 3, 2008 19:08
> To: general-community
> Subject: Shell question
> 
> 
> I would like to acheive something like:
> 
> export `echo ABC=2 3 DEF=4`
> 
> What I want to do is ABC="2 3"
> 
> The problem is that export complains about "3" not having an identifier. I
> can understand that.  I tried all sort of wrapping with ' and " but to no
> avail.  Suggestions?
> 
> _______________________________________________
> General
> http://community.qnx.com/sf/go/post6448


Re: RE: Shell question  
On Tue, 2008-04-08 at 13:13 -0400, Mario Charest wrote:

> export `grep -v "#" ConfigFile`

while IFS== read var val; do case "$var" in [#]*) ;; *) eval "export
$var=\"$val\"";; esac; done <ConfigFile

Would be one way.
Re: RE: Shell question  
> On Tue, 2008-04-08 at 13:13 -0400, Mario Charest wrote:
> 
> > export `grep -v "#" ConfigFile`
> 
> while IFS== read var val; do case "$var" in [#]*) ;; *) eval "export
> $var=\"$val\"";; esac; done <ConfigFile
> 
> Would be one way.

Not sure I undertand the IFS== part 

Doesn't quite work we would have to change all the config file from

LENGTH=50

to 

LENGTH 50

Also any empty line kills the while loops.

Re: RE: Shell question  
On Tue, 2008-04-08 at 15:33 -0400, Mario Charest wrote:

> Not sure I undertand the IFS== part 

IFS => Input Field Seperator

A good reference book for all things sh/ksh is actually the bash manual
(all of sh/ksh/bash share a common ancestor being bourne shell, so any
bourne shell wizardry is going to be documented in the bash manual).

> Also any empty line kills the while loops.

You can re-work the one liner to handle this case.  Though, at the end
of the day, if your configfile ain't a bourne shell file, you'll need to
make compromises as to what you want to support.

Re: RE: Shell question  
Hi Stephane,

The IFS I know about but it's the == that confuses me.  

What we are actually thinking of doing is using the ch shell.
RE: RE: Shell question  

> The IFS I know about but it's the == that confuses me. 

IFS== assigns '=' as the field seperator.

Try:

echo 'A=B' | (IFS== read variable value; echo var:$variable val:$value) 

> What we are actually thinking of doing is using the ch shell.

C shell is IMO, a bad shell.  See
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ for the classic article
that explains why you might want to avoid C shell.

For a basic, good old fashion shell, it's best to stick with bourne shell or
one of its close cousins (ksh, pdksh, bash).  If you write *PURE* bourne
shell, it will run in bourne shell, korn shell, born again shell, public
domain korn shell.

If you need something more advanced, Python is a possibility (though on a
target system, it will depend, 'cause Python is bigger than good 'ol ksh)

But of course, that's just me, and this advice is worth every penny you paid
me :-)
Re: RE: RE: Shell question  
> 
> 
> > The IFS I know about but it's the == that confuses me. 
> 
> IFS== assigns '=' as the field seperator.
> 
> Try:
> 
> echo 'A=B' | (IFS== read variable value; echo var:$variable val:$value) 
> 
> > What we are actually thinking of doing is using the ch shell.
> 
> C shell is IMO, a bad shell.  See
> http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ for the classic article
> that explains why you might want to avoid C shell.
> 
Thanks, that's a good read.  In our case we are thinking about going CShell simply because most guys here don't know 
shell stuff and that includes me.  Every one ends up modifying them and they all become spaghetti like very fast...

Or when you are in the field and you they are in a hurry to modify them it gets messy real fast...



Re: RE: RE: Shell question  
On Wed, 2008-04-09 at 13:25 -0400, Mario Charest wrote:

> Thanks, that's a good read.  In our case we are thinking about going
> CShell simply because most guys here don't know shell stuff and that
> includes me. 

Well, C Shell isn't C, so C shell isn't going to be easier just because
you know C.

Re: RE: RE: Shell question  
> On Wed, 2008-04-09 at 13:25 -0400, Mario Charest wrote:
> 
> > Thanks, that's a good read.  In our case we are thinking about going
> > CShell simply because most guys here don't know shell stuff and that
> > includes me. 
> 
> Well, C Shell isn't C, so C shell isn't going to be easier just because
> you know C.
> 


ch ( and not csh ) from soft integration is actually ISO C90 ( so they claim ), plus many C99 stuff ( they say they are 
more C99 compliant then many compiler) as well as basic C++.  There is also support for socket API (never tried it).  If
 one wants it can interface to any library, but that can be a lot of work since every library function must be 
interfaced with stub.  That means, in theory,  from a shell, one should be able to use any QNX function.

Re: RE: RE: Shell question  
On Thu, 2008-04-10 at 11:05 -0400, Mario Charest wrote:

> ch ( and not csh )

ok.

>  from soft integration is actually ISO C90 ( so they claim ), plus
> many C99 stuff 

grep "test" myfile.txt;
if ( _status == 0 )

doesn't look like C to me :-)

And the following is taken right from the bourne shell bag-o-tricks (the
ENDFILE bit, the use of $ to get the value):

void sendApplet(char *x, char *y, char *expr) {
 fprintf stdout << ENDFILE
#include<stdio.h> 
int main() { 
double x = $x; 
double y = $y; 
printf("x = %f", x); 
printf("y = %f\n", y); 
printf("$expr = %f\n", $expr);
} 
ENDFILE
}

So, I wouldn't really view this as very C-ish.

Though that's not to say that CH isn't nice. Ch may well be the best
choice for your project(s).
RE: RE: Shell question  
I tried this:


$ cat t
while read line
do
        if [ "${line%=*}"X != "$line"X ]
        then
                eval export ${line%=*}=\"${line#*=}\"
        fi
done


When I run it on the following file:

$ cat cfg
#blablab
HEIGHT=52

WIDTH=33
OPTIONS=-z -y -u

I get:
HEIGHT=52
OPTIONS='-z -y -u'
WIDTH=33

Now, there is one serious issue with ksh. It's actually fairly generic.
Basically when you try and use pipes in pdksh (ours) then the last command
will always be run in a subshell. Posix actually says _any_ command in a
pipeline could be in a subshell. While that script works you should be aware
of the subshell issue: if it is in a subshell, of course, the export only
applies to that shell. 

I have found a way around it: enclose each command in the pipeline in { }.
This forces the command(s) to run in the current shell. 

Hope that helps,
Keith 


-----Original Message-----
From: Mario Charest [mailto:mcharest@zinformatic.com] 
Sent: April 8, 2008 21:38
To: general-community
Subject: Re: RE: Shell question

Hi Stephane,

The IFS I know about but it's the == that confuses me.  

What we are actually thinking of doing is using the ch shell.


_______________________________________________
General
http://community.qnx.com/sf/go/post6539
RE: RE: Shell question  
On Tue, 2008-04-08 at 22:45 -0400, Keith Russell wrote:
> I tried this:
> 
> 
> $ cat t 
> while read line 
> do 
>         if [ "${line%=*}"X != "$line"X ] 
>         then 
>                 eval export ${line%=*}=\"${line#*=}\" 
>         fi 
> done

Though things get to behave funny when your input file has a line like:

#A=B

:-)

i.e. it runs the:
export #A=B

which is the same as:
export

which prints your entire environment :-)



Post Deleted