<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<div>I love how I found this workaround. C is resembled a lot more this way.
<div><br>
</div>
<div>Passing variables by value is achieved by sourcing from a sub-shell. Passing variables by reference is achieved by sourcing from the parent Shell. Command substitutions always pass variables by value, though.</div>
<div><br>
</div>
</div>
<div>I'm thinking of introducing a built-in 'function' command based on this idea. Any thoughts on that?<br>
</div>
<div><br>
</div>
<div>By the way, regarding the exit status issue I reported on Csh, it was a problem on my side. My mistake. Sorry about that.</div>
<div><br>
</div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><span style="color: var(--textPrimary);">Here's a better workaround I found to simulate functions:</span><br>
</div>
<div>
<div>if !( "$?0" ) then</div>
<div> echo 'You must run this script by explicitly calling its file.'</div>
<div> exit -1</div>
<div>endif</div>
<div><span style="">alias function 'set argv = ( \!* ) ; source "$0"'</span><br>
</div>
<div>if ( "$?main" ) goto "$1"</div>
<div><br>
</div>
<div>set main</div>
<div>set ret = "`function myfunc`"</div>
<div>echo "$ret"</div>
<div>exit</div>
<div><br>
</div>
<div>myfunc:</div>
<div>( function myfunc2 )</div>
<div>echo "A function."</div>
<div>exit</div>
<div><br>
</div>
<div>myfunc2:</div>
<div>echo "Another function."</div>
<div>exit</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
<br>
</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
This is much better than aliases in scripts. Works for Tcsh and Csh.</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
<br>
</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
One problem I found on Csh is the exit status output is never stored to the parent Shell.</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
<br>
</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
For the function workaround described above, the workaround to store the exit status is to echo:</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
if !( "$?0" ) then</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
echo 'You must run this script by explicitly calling its file.'</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
exit -1</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
endif</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
alias function 'set argv = ( \!* ) ; source "$0"'</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
if ( "$?main" ) goto "$1"</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
<br>
</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
set main</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
set ret = "`function myfunc "\""$1"\"" && echo 1 || echo 0`"</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
if ( $ret[$#ret] )<span style="background-color:transparent; color:inherit"> then</span></div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
<span style="background-color:transparent; color:inherit"> while ( $#ret > 1 )</span></div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
<span style="background-color:transparent; color:inherit"> echo "$ret[1]"</span></div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
<span style="background-color:transparent; color:inherit"> shift ret</span></div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
<span style="background-color:transparent; color:inherit"> end</span></div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
<span style="background-color:transparent; color:inherit">endif</span></div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
exit</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
<br>
</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
myfunc:</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
if ( -f "$2" ) then</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
<span style="background-color:transparent; color:inherit">echo 'OK.'</span></div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
<span style="background-color:transparent; color:inherit"> echo 'File found.'</span></div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
exit</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
endif</div>
<div style="font-family:inherit; font-size:inherit; color:inherit; background-color:transparent">
exit -1</div>
<div></div>
<br>
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="x_divRplyFwdMsg" dir="ltr"><a href="https://en.m.wikipedia.org/wiki/Special:MobileDiff/1102822365" data-auth="NotApplicable">https://en.wikipedia.org/wiki/Special:MobileDiff/1102822365</a><br>
</div>
<div>
<div dir="auto" style="color:rgb(33,33,33); background-color:rgb(255,255,255)">Why do they think multi-line aliases aren't possible?</div>
<div dir="auto" id="x_x_ms-outlook-mobile-signature">
<div dir="auto">alias function 'if -e \!$ then\</div>
<div dir="auto">echo OK\</div>
<div dir="auto">else\</div>
<div dir="auto">echo Not OK\</div>
<div dir="auto">endif'</div>
<div dir="auto">function dir</div>
<div dir="auto" style="color:rgb(33,33,33); background-color:rgb(255,255,255)"><br>
</div>
<div dir="auto" style="color:rgb(33,33,33); background-color:rgb(255,255,255)">I can also make it into a variable:</div>
<div dir="auto" style="color:rgb(33,33,33); background-color:rgb(255,255,255)">set function = 'if -e \!$ then\</div>
<div dir="auto" style="color:rgb(33,33,33); background-color:rgb(255,255,255)">echo OK\</div>
<div dir="auto" style="color:rgb(33,33,33); background-color:rgb(255,255,255)">else\</div>
<div dir="auto" style="color:rgb(33,33,33); background-color:rgb(255,255,255)">echo Not OK\</div>
<div dir="auto" style="color:rgb(33,33,33); background-color:rgb(255,255,255)">endif\</div>
<div dir="auto" style="color:rgb(33,33,33); background-color:rgb(255,255,255)">:'</div>
<div dir="auto" style="color:rgb(33,33,33); background-color:rgb(255,255,255)">eval $function:q dir</div>
</div>
</div>
</div>
</body>
</html>