[ale] Stupid Question on ksh script

Lightner, Jeff JLightner at dsservices.com
Tue Mar 11 14:12:32 EDT 2014


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.
----------------------------------


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.ale.org/pipermail/ale/attachments/20140311/0c4c8cb1/attachment-0001.html>


More information about the Ale mailing list