<div dir="ltr">It is linked to /usr/bin/ksh. I have tested it in ksh on a CentOS and came to the same conclusion of ${0#/} being equal to ${0}. I am uncertain what is the point of the conditional other than to be confusing. To give some context, it is the starting point for an internal deployment tool within my company. The originator of the script has been _less_ than helpful, not answering questions via email. I have been asked by my boss to understand how to do this from the ground up and disseminate that information to everyone. </div>
<div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Mar 11, 2014 at 2:12 PM, Lightner, Jeff <span dir="ltr"><<a href="mailto:JLightner@dsservices.com" target="_blank">JLightner@dsservices.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div lang="EN-US" link="blue" vlink="purple">
<p></p>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">The ${0} refers to the current program name.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">It is not assigning X to anything – it is assigning the value of X. If the condition is true the it is setting X to the present working directory followed
by the program name. If not true it is setting it to just the program name.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">So if your program were called billybob.sh and it were in /home/bobbiesue then $0 = billybob.sh (as does ${0}) and ${PWD}/${0} = /home/bobbiesue/billybob.sh.
If the condition is true then X gets set to billybob.sh and if not true X gets set to /home/bobbiesue/billybob.sh.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">After that it is trying to cd to the directory that ccontains $0 which would be “.” (current directory) if true or /home/bobbiesue if false.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">I don’t know if the #/ is trying to do in the conditional though. Out of context with the rest of the script it doesn’t mean anything to me. Testing it
as is I found ${0#/} does equal ${0}.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">ksh is available for Linux so you could try to add it and run this in ksh. You could then insert echo statements just before the conditional:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">echo ${0#/}<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">echo ${0}<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Note that ${0} might NOT be the actual program if this is calling a function within the program.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Does the interpreter line at top of script specify ksh or bash or sh? If sh what is that linked to?<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> <a href="mailto:ale-bounces@ale.org" target="_blank">ale-bounces@ale.org</a> [mailto:<a href="mailto:ale-bounces@ale.org" target="_blank">ale-bounces@ale.org</a>]
<b>On Behalf Of </b>Jonathan Meek<br>
<b>Sent:</b> Tuesday, March 11, 2014 1:44 PM<br>
<b>To:</b> Atlanta Linux Enthusiasts<br>
<b>Subject:</b> [ale] Stupid Question on ksh script<u></u><u></u></span></p><div><div class="h5">
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">Hello everyone, <u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I am reviewing a script at work in order to understand that someone has done before me. They decided to ksh to write their scripting. There's an if statement I don't quite get and would be grateful for some light on the issue. Here's the
code snippet: <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">if [ ${0#/} = ${0} ] ; then</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New""> X=${PWD}/${0}</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New""> else</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New""> X=${0}</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New""> fi</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New""> cd $(dirname ${X})</span><u></u><u></u></p>
</div>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial","sans-serif"">Based on my own searching for meaning and understanding and somewhat limited look into the man pages for ksh(couldn't remember how to search within vi), here's what I think is going on:</span><u></u><u></u></p>
</div>
<blockquote style="margin-left:30.0pt;margin-right:0in">
<div>
<p class="MsoNormal"><span style="font-family:"Arial","sans-serif"">The if statement is checking what shell is currently running and as long as it is the expected shell, it will assign X to ${PWD}/${0}. If not, it will assign X to ${0}. Then it will change
directories based on the output of dirname, which could be the current directory or the directory that variable X is at. </span><u></u><u></u></p>
</div>
</blockquote>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial","sans-serif"">Does this sound right or I am missing something? </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial","sans-serif"">Regards, </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial","sans-serif"">Jonathan</span><u></u><u></u></p>
</div>
</div>
</div></div></div>
<p></p>
<p> </p>
<p> </p>
<p></p>
<p><font face="Arial"><font color="fuchsia"><font style="FONT-FAMILY:Arial;FONT-SIZE:10pt"></font></font></font> </p>
<p> </p>
<p><font face="Arial"><font color="fuchsia"><font style="FONT-FAMILY:Arial;FONT-SIZE:10pt">Athena<font size="1">®</font>, Created for the Cause</font><font size="1">™
</font></font></font></p>
<p><font face="Arial">Making a Difference in the Fight Against Breast Cancer</font></p>
<p><font face="Arial"></font> </p>
<p><font face="Arial"></font> </p>
<p><font face="Arial"><font><strong>How and Why I Should Support Bottled Water!<br>
</strong>Do not relinquish your right to choose bottled water as a healthy alternative to beverages that contain sugar, calories, etc. Your support of bottled water will make a difference! Your signatures count! Go to
<a href="http://www.bottledwatermatters.org/luv-bottledwater-iframe/dswaters" target="_blank">http://www.bottledwatermatters.org/luv-bottledwater-iframe/dswaters</a> and sign a petition to support your right to always choose bottled water. Help fight federal and state issues,
such as bottle deposits (or taxes) and organizations that want to ban the sale of bottled water. Support community curbside recycling programs. Support bottled water as a healthy way to maintain proper hydration. Our goal is 50,000 signatures. Share this petition
with your friends and family today!</font></font></p>
<p><font face="Arial"></font> </p>
<p><span style="FONT-FAMILY:Arial;FONT-SIZE:10pt">---------------------------------<br>
CONFIDENTIALITY NOTICE: This e-mail may contain privileged or confidential information and is for the sole use of the intended recipient(s). If you are not the intended recipient, any disclosure, copying, distribution, or use of the contents of this information
is prohibited and may be unlawful. If you have received this electronic transmission in error, please reply immediately to the sender that you have received the message in error, and delete it. Thank you.<br>
----------------------------------</span><span style="FONT-FAMILY:'Courier New';FONT-SIZE:9pt"><u></u><u></u></span></p>
<p> </p>
<p></p>
<p></p>
</div>
<br>_______________________________________________<br>
Ale mailing list<br>
<a href="mailto:Ale@ale.org">Ale@ale.org</a><br>
<a href="http://mail.ale.org/mailman/listinfo/ale" target="_blank">http://mail.ale.org/mailman/listinfo/ale</a><br>
See JOBS, ANNOUNCE and SCHOOLS lists at<br>
<a href="http://mail.ale.org/mailman/listinfo" target="_blank">http://mail.ale.org/mailman/listinfo</a><br>
<br></blockquote></div><br></div>