Self referencing script error message

Hello again all. I have a user editable script that I'd like to have point out the user error to. Problem is I'm having troubles getting an echoed error message to give me the line. Here's what I'm trying to do.

 grep -n $loc /this/script.sh 

where '$loc' is the argument passed to the script. Here's the USAGE: for the script:

./makeBackup /var/directory/toBackup/

The argument is stored as '$loc'. I want to echo " '$loc' is not a valid directory (grep'd location of error)" If I'm not being clear forgive me it's kinda early here. But basically I need to alert the user of the line number where they made their mistake in the error message.

So from the script you already test whether the directory passed is valid, and if it's not, you want to spit out something like "usage: ./makeBackup /path/to/dir: Not valid" or something similar?

the following works in sh/bash, ksh.

echo "usage: $0 ($1 not a valid dir)"

.. can be illustrated in the following scenario:

$ ./makeBackup /some/nonexistant/dir
usage: ./makeBackup (/some/nonexistant/dir not a valid dir)
$

HTH

Thanks for your response man I appreciate it. I can get that part of the error just fine but I want to reference the line number they made their mistake. See, in the script you put the directories you want to backup in the script itself. Instead of doing it for each and every directory from the command line, you simply put 'makeBackup /var/this/dirExists'. If that dir does not exist, I'd like the error message to reflect its not a valid location and then give the line number where they made their mistake. So if i grep that script I can see the line number if I use -n. I want that to be echo'd back to the user

As you said it's not clear enough..:wink: However im assuming this what your looking for..

loc=$1 # Stores the 1st argument to variable loc - /var/directory/toBackup/
echo "$(grep -n "$loc" /this/script.sh)  is not a valid directory"

Yea man sorry...totally not clear, that did the trick tho. Just one problem, I'd like the script to know where its at. So no matter which directory it's run from it can grep -n itself to find that error. I don't want a hardcoded path to it in the script.

How can you find where error occurs while the passed parameter isn't hardcoded in the script?

The parameter is hardcoded. I don't want the path to the script to be hardcoded as I want it to be able to reference itself no matter which directory it's in. The '$loc' parameter is hardcoded. That's the error I need it to spit out if '$loc' wasn't valid.

I want the error to say "You specified a target that wasn't valid and here's the line number of that invalid target" basically. The script needs to be able to do that no matter if it's in /var/root or /var/foo/bar/lorem/ipsum/dolor/we/are/the/world/we/are/the/children/

If you want the line number; the line in the script try:

ms:/u/purdym>cat /tmp/d
#!/usr/bin/ksh
echo "This error occured on line: $LINENO."

ms:/u/purdym>/tmp/d
This error occured on line: 2.
ms:/u/purdym>

That'd work but the only problem is $LINENO returns the line number of itself. Not where the error was located. So I need something that will look in the script and return where the error itself is

(Off topic. Avoid the variable name $LINENO - it is a largely undocumented reserved variable name and can change it's value).

Actually .... this may be "on-topic" if it is in the O/P's script.

Totally on-topic. It doesn't apply to all Shells but $LINENO is the current line in the Shell Script in many. I have seen other variants.
http://alasir.com/books/bsd/398-402.html

Advise change the name of your variable.