Handle cases when a script is called with relative or absolute paths #36

Closed
wants to merge 1 commit into
from

Projects

None yet

3 participants

@qbi
Contributor
qbi commented Oct 30, 2012

The script can be called in different ways. The user can just use the
filename or use relative/absolute paths. The last case led to errors
with the current script. It mkdir'd 'chapter*' and failed.

The commit will take the name of the shell script ($0) and determines
if there are slashes in it. If so, it removes the everything except
the path, checks if it is really a directory and constructs a correct
path for the for-loop.

The two changes in line 45 and 47 remove everything before the the
last slash including the slash. So especially the title doesn't
contain file paths.

@qbi qbi Handle cases when a script is called with relative or absolute paths
The script can be called in different ways. The user can just use the
filename or use relative/absolute paths. The last case led to errors
with the current script. It mkdir'd 'chapter*' and failed.

The commit will take the name of the shell script ($0) and determines
if there are slashes in it. If so, it removes the everything except
the path, checks if it is really a directory and constructs a correct
path for the for-loop.

The two changes in line 45 and 47 remove everything before the the
last slash including the slash. So especially the title doesn't
contain file paths.
a20ba51
@pettter
Member
pettter commented Oct 30, 2012

Nice work! However, consider the case where convert_to_tex has been moved out of source, or is called from a different source-ish tree (such as one downloaded using get_from_booki - see github/pettter/handbook).

I am not sure what is the natural expectation - that convert_to_tex works on the current directory, wherever that is, or if it works on the directory where it is placed.

Opinions?

@qbi
Contributor
qbi commented Oct 30, 2012

Well in that case it seems a good idea to add a variable $MDPATH (or however you want to call it):
MDPATH=src
In a second step I would check if this directory contains the markdown files:
if ! find $MDPATH -type f -name "*.md" >/dev/null 2>&1; then
echo "Please set the correct MDPATH"
fi

I don't see any other possibility to handle this case in a generic way.

@pettter
Member
pettter commented Oct 30, 2012

I'm questioning the premise - that the expected default behaviour is that convert_to_tex would use the files in the directory where it is placed, rather than the user's current working directory.

I do agree, however, that the "source" directory should be possible to be given as a command line argument (or environment variable or smth).

@qbi
Contributor
qbi commented Oct 31, 2012

Well, as far as I see it a user has the following options:

  1. Call the script from the current directory
  2. Call the script from some other directory
  3. Copy the script to another directory and call it from there

My proposed commit will handle the first two cases, but not the third one. If a user copies the script to some directory the current script can not determine where there markdown files are. So if we want to handle the last case we need to insert this information into the script. From my point of view a variable would be the best for that purpose.

My example in the comment above was not fully correct. A better version for the script is:
MDPATH=$(pwd)
This uses the current working directory where the script and also the directories with markdown files is located. We can test if there are markdown files and if not print a warning (plus add some comment into the script). What do you think?

@pettter
Member
pettter commented Nov 1, 2012

Well, there is a fourth case - the user has a similar hierarchy of files, and wishes to convert this to a PDF in a similar way.

E.g. the user has used the get_from_booki.pl script checked in at pettter@0023abc and wishes to create a PDF from this. Of course, it is possible to copy the script to this directory and use it in that way, but I question the assumption that this is more natural/UNIXy than simply running the script, whereever it may be, and have it work on the CWD by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment