1

I am unable to access variable declared in a KornShell (ksh) script from another c shell script for example,

vi script.ksh
set MyUser=root
set MyPassword=shroot
exit

vi script.csh
. /script1.ksh
echo $MyUser
echo $MyPassword
exit

The problem involved here is its unable to access the variable MyUser and MyPassword from the script1 .ksh in script2.csh.

Can you please suggest in this case?

1
  • 2
    you can't 'source' a script in one shell and have it execute cmds with the syntax of another shell. Your example is a big mix-up between the syntax's of the 2 shells. source envFile is really the best you can do in csh. and some issue in ksh, . envFile.ksh, you can't use csh set var = value (note the spaces) in ksh. Good luck. Commented Jul 16, 2013 at 2:52

1 Answer 1

1

The ksh script is not doing what you think it is doing when used by ksh; it sets $1 first to MyUser=root and then MyPassword=shroot.

However, when you source it from your C shell script, because the syntax is compatible with C shell (checked with tcsh), you would set the two variables. If you sourced the script from a Bourne-shell derivative, the exit at the end of the ksh script would cause the shell to exit; it does not seem to cause the C shell to exit.

So, what you've got is a weird hybrid. Normally, you stick to one language and use it exclusively. Where you need to use another shell, you can do so, but you simply run the script written for the other shell. You can communicate to the other script with environment variables. You don't normally try writing sourceable code that can be used by both C shell and Bourne-shell derivatives; there is too much syntax that is different.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.