Operating System - HP-UX
1753394 Members
7201 Online
108792 Solutions
New Discussion юеВ

Re: simple script is failing

 
SOLVED
Go to solution
Grayh
Trusted Contributor

simple script is failing

Hi All,

I was running the below script and somehow it dosent work:-

cat myscript.sh
#!/usr/bin/ksh
echo "hello world"
alias 'ps=ps -aef'
alias -x 'bmw=ls -rt'

Globe1>./myscript.sh
hello world
Globe1> bmw
ksh: bmw: not found

Plz advice

6 REPLIES 6
MarkSyder
Honored Contributor

Re: simple script is failing

The aliases only exist inside your script. Once the script ends so do the aliases. Better off putting them in your .profile, .login etc. as appropriate.

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
James R. Ferguson
Acclaimed Contributor
Solution

Re: simple script is failing

Hi:

When you ran as:

# ./myscript.sh

...you executed in your own environment. A child process cannot propagate variables to its pareent.

Instead, do:

# . ./myscript.sh

...and then you can use the 'bmw' alias.

Regards!

...JRF...
Grayh
Trusted Contributor

Re: simple script is failing

I do not have write permissions on the .profile and .login...

I type alias after running the script

and my alias dosent work...

Plz help me with a small example

thank you
Paul McCleary
Honored Contributor

Re: simple script is failing

Hi,

I see you are trying to use:

alias -x

I guess with the aim that the alias is propagated to another shell.

This won't work as you've seen. See this part of ksh man page:

Exported aliases remain in effect for subshells but must be
reinitialized for separate invocations of the shell (see Invoking ksh
below).

You would probably be better just sourcing in a file to set them for each new shell:

. /file

HTH, Paul
Grayh
Trusted Contributor

Re: simple script is failing

Thanks JRF... It worked

I have learnt something now
Matti_Kurkela
Honored Contributor

Re: simple script is failing

If you can write to your home directory, you can always fix the permissions of any files in it, even if you don't have the ownership of the file.

Just copy the original to a new name (you will own the new copy)...

cp .profile .profile.new

... delete the original...

rm .profile

... and rename the copy you made back to original name.

mv .profile.new .profile

If the permissions of the file are -r--r--r-- but you are the owner of the file, there is no need to do any copying. You can simply give yourself the write permission:

chmod u+w .profile

MK
MK