Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bourne shell question

Status
Not open for further replies.

nithink

Programmer
Nov 7, 2002
92
US
Hi,
I'm having a C-shell script which I'm converting into a Bourne shell script. The problem is there's a 'source' command for which I dont know the equivalent in Bourne shell. Can anyone pls help me out in this ?

the following line is the one which I want to change to Bourne Shell,

source $DATAENV/xyzenv ----> xyzenv is a c-shell script

Thanks


 
new2everything:

There's several extensive threads on this problem. The equivalent syntax in sh/ksh is:

. yoursourcefile

or

. ./yoursourcefile

That's a period, a space, and your filename.

Regards,


Ed
 
Thanks Ed, Sorry if I had repeated the same question again.

As you said I tried using,

. $DATAENV/xyzenv

but it gives the error as

syntax error at line 92: `switch' unexpected

Is 'exec' an equivalent for 'source' ? i tried that too, but it gave the error as

$DATAENV/xyzenv: cannot execute

Thanks, Your help appreciated.
 
New:

It looks like you have a space after the "."

. $DATAENV/xyzenv

that's good. Are you sure you don't have a syntax error in xyzenv? If you just execute:

$DATAENV/xyzenv

does it execute with no syntax errors?

Ed

 
The dot (.) command in sh is equivalent to the source command in csh. It reads in the referenced script as if it were part of the current script.

By implication, that means that the file being sourced must be in the same language. An sh script that tries to source a csh script will get an error and vice versa.

Solution - convert xyzenv to sh syntax.
 
Thanks Ed & Sampsonr.

Ed, There's no syntax error in xyzenv. I tried to run it separately and it ran w/o any errors. And fyi, that xyzenv is C-Shell script. Any other suggestions Ed or Sampsonr ?



 
as sampsonr has mentioned the sourced file has to be written in the same/ language the one it's being sourced from.

In other words if you have:
1. file 'a' that's a Bourne shell script
2. file 'b' that's a Cshell shell script

In order to 'source' "b" from "a", "b" has to be a Bourne shell script.

If you don't care if you 'source' 'b' OR simply call it, you can simply call "b". vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks Ed, Sampsonr, Vgersh. Yeah as you said either way both the scripts should be in the same shell either bourne or c-shell. As I'm calling a C-shell from inside the Bourne its not recognizing the 'switch' statement inside the C-shell. But I tried another way, by using the 'exec' command inside the Bourne shell. It didnt give any error.
exec $DATAENV/xyzenv
I just want to know whether it has the same effect as the 'source' command.

Thanks very much.
 
The result of an exec is vastly different from sourcing a file except in very limited circumstances.

If A calls B, it means run B until it finishes and then continue with the next line in A. You probably knew that though.

If A execs B, it means execute B and terminate when B finishess. If there are any lines in A after the exec, they are ignored. As a general rule, don't use exec unless you are confident that that's what you really want to happen.

If A sources B, -- the content of B is read (not executed) and then executed as if it were part of A. Any variable declared in B are visible in A -- this is the most common use of sourcing, to set variables used by multiple scripts. That's why B must be in the same language as A.

Figure out which one of these behaviors is appropriate for your particular program and go from there. It's critical though that you understand the ramifications of whichever one you choose.

 
Thanks much Sampsonr, It was a very good explanation. As you said 'exec' doesnt return to the original calling file A.
I tried 'eval' which is working fine but like you said 'source' just reads(not execute) but 'eval' executes.

I dont know what to use now.
Thanks very much.
 
If the script you're converting used source, then your best bet is probably to continue with that and use the dot. The downside of course is that you now have to convert the sourced script to bourne shell as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top