[ale] Stupid Question on ksh script

Jim Kinney jim.kinney at gmail.com
Tue Mar 11 14:58:13 EDT 2014


The ${0#/} means to use the value of ${0} after stripping out any leading
/'s

The # means to remove leading <what follows next>


On Tue, Mar 11, 2014 at 2:24 PM, Jonathan Meek <jonathan.l.meek at gmail.com>wrote:

> 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.
>
>
> On Tue, Mar 11, 2014 at 2:12 PM, Lightner, Jeff <JLightner at dsservices.com>wrote:
>
>>   The ${0} refers to the current program name.
>>
>>
>>
>> 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.
>>
>>
>>
>> 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.
>>
>>
>>
>> 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.
>>
>>
>>
>> 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}.
>>
>>
>>
>> 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:
>>
>> echo ${0#/}
>>
>> echo ${0}
>>
>>
>>
>> Note that ${0} might NOT be the actual program if this is calling a
>> function within the program.
>>
>>
>>
>> Does the interpreter line at top of script specify ksh or bash or sh?  If
>> sh what is that linked to?
>>
>>
>>
>>
>>
>> *From:* ale-bounces at ale.org [mailto:ale-bounces at ale.org] *On Behalf Of *Jonathan
>> Meek
>> *Sent:* Tuesday, March 11, 2014 1:44 PM
>> *To:* Atlanta Linux Enthusiasts
>> *Subject:* [ale] Stupid Question on ksh script
>>
>>
>>
>> Hello everyone,
>>
>>
>>
>> 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:
>>
>>
>>
>> if [ ${0#/} = ${0} ] ; then
>>
>>         X=${PWD}/${0}
>>
>>     else
>>
>>         X=${0}
>>
>>     fi
>>
>>     cd $(dirname ${X})
>>
>>
>>
>> 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:
>>
>>  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.
>>
>>
>>
>> Does this sound right or I am missing something?
>>
>>
>>
>> Regards,
>>
>>
>>
>> Jonathan
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> Athena®, Created for the Cause™
>>
>> Making a Difference in the Fight Against Breast Cancer
>>
>>
>>
>>
>>
>>
>> *How and Why I Should Support Bottled Water! *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
>> http://www.bottledwatermatters.org/luv-bottledwater-iframe/dswaters 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!
>>
>>
>>
>> ---------------------------------
>> 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.
>> ----------------------------------
>>
>>
>>
>>
>> _______________________________________________
>> Ale mailing list
>> Ale at ale.org
>> http://mail.ale.org/mailman/listinfo/ale
>> See JOBS, ANNOUNCE and SCHOOLS lists at
>> http://mail.ale.org/mailman/listinfo
>>
>>
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo
>
>


-- 
-- 
James P. Kinney III

Every time you stop a school, you will have to build a jail. What you gain
at one end you lose at the other. It's like feeding a dog on his own tail.
It won't fatten the dog.
- Speech 11/23/1900 Mark Twain


*http://heretothereideas.blogspot.com/
<http://heretothereideas.blogspot.com/>*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.ale.org/pipermail/ale/attachments/20140311/b746d514/attachment-0001.html>


More information about the Ale mailing list