Operating System - HP-UX
1833401 Members
3611 Online
110052 Solutions
New Discussion

Re: How to interpret this script?

 
SOLVED
Go to solution
zhaogui
Super Advisor

How to interpret this script?

Can anyone help me interpret the following "ENV=..." statement?
Thanks,

#cat .profile
...
ENV='${START[(_$-=1)+(_=0)-(_$-!=_${-%%*i*})]}'
START=$HOME/.kshrc
export ENV START
#cat .kshrc
set -o vi
set -o monitor
alias ll='ls -la'
setprmt='PS1=$HOST"["$USER"]:"${PWD##*/}" "'
setprmt
alias cd=newcd
function newcd
{
\cd "$@" >&- || return 1
setprmt
}


29 REPLIES 29
Steven Sim Kok Leong
Honored Contributor

Re: How to interpret this script?

Hi,

Did you mistype the double-quotes as single-quotes?

ENV='${START[(_$-=1)+(_=0)-(_$-!=_${-%%*i*})]}'

will simply assign the string in the single quotes ''

${START[(_$-=1)+(_=0)-(_$-!=_${-%%*i*})]}

to the ENV variable

Thus echo $ENV will give you the literal string:

${START[(_$-=1)+(_=0)-(_$-!=_${-%%*i*})]}

Hope this helps. Regards.

Steven Sim Kok Leong
Edgar Matzinger
Advisor

Re: How to interpret this script?

This seems just to be an elaborate way of making sure that ${ENV} is only excuted by interactive ksh's. And I wonder if it works at all, because it seems that ENV is not executed if the shell is non-interactive (script).

The variable $- gives you the options given to ksh.
zhaogui
Super Advisor

Re: How to interpret this script?

Sorry for delayed reply.
Yes, this script cannot work. But anyidea on how to make it work?

Thanks,
harry d brown jr
Honored Contributor

Re: How to interpret this script?

ARE YOU KIDDING ME?

This member has assigned points to 21 of 242 responses to his/her questions.

LESS THAN 10%???

http://forums.itrc.hp.com/cm/UserProfile/1,,CA698738!1,00.html


live free or die
harry
Live Free or Die
Paula J Frazer-Campbell
Honored Contributor

Re: How to interpret this script?

zhaogui
Super Advisor

Re: How to interpret this script?

I admited that sometimes I forgot to give points, For this I will compensate by gradually giving out points. But most of time I haven't got any satisfactory answer or the answer is not relevent or applicable to my environment. In that case, how can I give points? Should I give 1 or 2 points?
harry d brown jr
Honored Contributor

Re: How to interpret this script?

If the post didn't answer the question or was way off mark assign "N/A". For instance, this post here should be assigned "N/A". And there is nothing wrong with giving someone 1 or 2 points just for trying.

Now with the new feature on our "profiles" you have the ability to see all of the threads that you have started.


live free or die
harry
Live Free or Die
zhaogui
Super Advisor

Re: How to interpret this script?

If I assign "N/A" to this post, for example, does that mean that this post is closed and other people cannot comment anymore? Sometimes I thought if I just leave it there and someday other people might provide better answer. That is why I often just leave my post there without giving any points unless I already got a satifactory answer.

steven Burgess_2
Honored Contributor

Re: How to interpret this script?

Hi

Thats why you have a value between 1 and 10 - depending on how helpful the answer is for you - If you assign less than 8 , it is assumed that the answer has not been provided -
entering N/A does not close the post

Steve
take your time and think things through
zhaogui
Super Advisor

Re: How to interpret this script?

Is "N/A" equivalent to 0 points? I didn't find "N/A" and I am allowed to assign only 0-10 points.
Frank Slootweg
Honored Contributor

Re: How to interpret this script?

> Yes, this script cannot work. But anyidea on how to make it work?

*What* does not work? Only the "ENV=..." statement? What results do you get, i.e. (error) message(s), etc.?

As far as I can tell, your "ENV=...", "START=..." and "export ENV START" statements are correct, assuming you are using a *login* *Korn* shell, i.e. if this is (X) 'windows' related, i.e. CDE, VUE, etc., then it may just be a question of .profile not being executed.

Minor point: The ENV and START statements are 'the wrong way around', i.e. START is used in ENV, but is defined *after* ENV. While, as far as I know, this should matter because this stuff is exported and then executed, it is better to put them in logical order.

For what it is worth, this rather complex "ENV=" statement is apparently documented in the "Shell's User Guide" Chapter 23 and Korn's book on ksh.

[My reference: A4678466]




zhaogui
Super Advisor

Re: How to interpret this script?

I didn't get any error message, but when I type env, I got the following,
...
TZ=SST-8
ENV=${START[(_$-=1)+(_=0)-(_$-!=_${-%%*i*})]}
START=/home/opc_op/.kshrc
...

Actually I don't understand what this ENV command will do, I just copy this from .profile of user opc_op which was automatically created by OpenView ITO.
Frank Slootweg
Honored Contributor
Solution

Re: How to interpret this script?

As I wrote, see the "Shells: User's Guide".

Specifically:

"23 Advanced Concepts and Commands" -> "The ENV Variable".

This is referenced in "POSIX and Korn Shell" -> "16 Starting and Stopping the Shell" -> "Setting Up .profile and .kshrc".

The "Shells: User's Guide" should be supplied with your system. An an HTML version is available at <> and a PDF version at <>.
zhaogui
Super Advisor

Re: How to interpret this script?

You have pointed to the right place, but I still don't know if this script works or not since there is no error message. see this,
$ echo $ENV[0]
${START[(_$-=1)+(_=0)-(_$-!=_${-%%*i*})]}[0]
$ echo $START[0]
/home/opc_op/.kshrc[0]

Does it work?

Additionally, how do I know if interactive shell or noninteractive shell is required in .profile of user opc_op which was created by OpenView ITO? Usually after login as opc_op, we would run opc to start ITO.

I think you may want to see .kshrc, here it is,
$cat .kshrc
set -o vi
set -o monitor
alias ll='ls -la'
alias setprmt='PS1=$HOST"["$USER"]:"${PWD##*/}" "'
setprmt
alias cd=newcd
function newcd
{
\cd "$@" >&- || return 1
setprmt
}
$alias
autoload=typeset -fu
cat=/usr/bin/cat
cd=newcd
false=let 0
functions=typeset -f
hash=alias -t -
history=fc -l
integer=typeset -i
ll=ls -la
ls=/usr/bin/ls
nohup=nohup
r=fc -e -
setprmt=PS1=$HOST"["$USER"]:"${PWD##*/}" "
stop=kill -STOP
suspend=kill -STOP $$
true=:
type=whence -v
$cd
newcd[2]: @: parameter not set


zhaogui
Super Advisor

Re: How to interpret this script?

One more thing, may I know if
this line in function newcd is correct?
\cd "$@" >&- || return 1
Why need "\"? What is &-? Are these specifically for ksh?
Frank Slootweg
Honored Contributor

Re: How to interpret this script?

> Does it work?

Because you get a "newcd[2]: @: parameter not set" error message when you do a "cd" and that "newcd" alias is defined in .kshrc, it looks like .kshrc is executed on startup of a login shell and hence the START= and ENV= statements seem to be working.

To be absolutely sure, why don't you put a few simple "echo Start of .kshrc" and "echo End of .kshrc" in .kshrc, so you will *see* when it is (not) executed?

I can not comment on the OpenView ITO aspects.

Also I let the newcd alias to others who may know its source/use.

H.Merijn Brand (procura
Honored Contributor

Re: How to interpret this script?

I think it's time to reward those people for their precious time. N/A (0) for me please
Enjoy, Have FUN! H.Merijn
zhaogui
Super Advisor

Re: How to interpret this script?

Frank, thank you for your help. You have answered the most part of my question. But I still have one doubt left,
How do I know whether I am running in interactive or non-interactive shell and how to switch from one to another? Because according to SHELL GUIDE, .kshrc will only be run under interacetive shell.
$ set -o
and look for:
interactive on
When can I see it becomes off or how to set it to off?
Is it true that .kshrc can only be run through .profile, It is normally something like ENV=~/.kshrc?

As for newcd, I think I need to post it as another question.
Frank Slootweg
Honored Contributor

Re: How to interpret this script?

A shell is interactive when it is used from a terminal, i.e. when you can/are type/typing commands.

Specifically, from the ksh(1) manual page (description of "-i" option):

> If the -i option is present or if the shell input
> and output are attached to a terminal (as reported
> by tty(3C)), the shell is interactive.

For future situations: You could have found this yourself by sending the (ksh(1)) manual page to a file and then using vi(1) or view(1) and look for the desired word(s), i.e.

$ man ksh | col -b >/tmp/ksh.man
$ view /tmp/ksh.man
[In view(1):]
/interactive
n
n
[etc.]
ZZ
$

Note: The "| col -b" strips the highlighting from the output, so what remains is plain ASCII text, which can be view(1)-ed, searched, etc..
zhaogui
Super Advisor

Re: How to interpret this script?

Thank you Frank, can you also tell me what is ">&-", the close standard output, is it different from normal 1> or 2> ?
Frank Slootweg
Honored Contributor

Re: How to interpret this script?

> Thank you Frank, can you also tell me what
> is ">&-", the close standard output, is it
> different from normal 1> or 2> ?

As the (ksh or other) manual page says, ">&-" closes standard output, i.e. for example this command gives no output:

echo message >&-

"2>&-" closes standard error, i.e. for example this command gives no error message:

ll file_which_does_not_exist 2>&-

For what it is worth, I see little use for ">&-" and "2>&-". Normally ">/dev/null" and "2>/dev/null" is much clearer. About the only use I can think of is:

exec >&-

which means close standard output for *all* subsequent commands (likewise for "exec 2>&-").

I don't know why ">&-" is used in 'your' case, i.e. the newcd alias. I think it should be "2>&-", because cd does not generate output, but can generate an error. Perhaps it has to do with the stuff ("\"?) mentioned in your "Why my newcd got problem?" thread (<>).
zhaogui
Super Advisor

Re: How to interpret this script?

A few scripts for references
zhaogui
Super Advisor

Re: How to interpret this script?

sorry.here it is.
zhaogui
Super Advisor

Re: How to interpret this script?

one more