LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Return Value (https://www.linuxquestions.org/questions/linux-newbie-8/return-value-666219/)

Swapna173 08-29-2008 01:48 AM

Return Value
 
Hi Guys,

I wanted to track the return value of certain errors in the linux process. How can i get the error numbers.

I wrote a script which takes database dumps and process with sqlldr and procedure.

In this case dumps exported successfully, but loader failed due to insufficient space. So i need to check the return value of insufficient space for each process and proceed. The script should stop process to proceed further in case of only insufficient failure.

helptonewbie 08-29-2008 02:23 AM

hello,
$? will normally give you the exit code in bash
in a script you would..
./run script (or command)
echo $? OR EXITSTATUS=$?;echo ${EXITSTATUS}

As long as the script or command does create the correct exit codes...sometimes you might find a command or script doesn't support them. Generally an error/failure would produce an exit code of 1 and success is generally exit code of 0.

If you have error checking in your scripts and you execute other scripts of your own and want to provide error exit codes for your own scripts, then you can when exiting a script do it with...
exit 0 #script completed successfullly
OR
exit 1 #script failure

Generally i use them in my own scripts when required, i run checks on whatever the script is supposed to have done successfully and then depending on those checks i then exit the script with status codes of 1 or 0

hope this helps, regards

ps- without seeing the script and the commands its hard to say more than this

Agrouf 08-29-2008 02:24 AM

So, you want to stop a script when the disk is full, is that what you want?
What about putting something like that in your script:
Quote:

fill=$(df -h /your/mount/point | tail -1 | sed "s/ */:/g" | cut -d: -f5)
[ ${fill%%%} -gt 99 ] && exit 1

helptonewbie 08-29-2008 03:30 AM

I'm intrigued now...more often lately (twice in the last two days) have i suddenly come across this %%% with variables. Can you point me in a direction of a sufficient doc for this...

I looked and found it does some kind of substitution type of thing...removing values from the variable and stuff ...as you can tell i don't really have a clue what it does :)

Agrouf 08-29-2008 05:12 AM

Actually, %% trims on the right.
let's consider this variable:
Quote:

var="the file system is full at 90%"
I want to get the last % off:
Quote:

echo ${var%%%}
the file system is full at 90
Now let's say I want to remove the last word. How do I do that?
Easy: I do exactly the same thing, but instead of removing %, I remove a space and anything that's after it (in regular expression, it means " *":
Quote:

echo ${var%% *}
the
Oops! Actually, " *" matches just after the first word. That is because %% takes the biggest match possible.
Fortunatelly, I have another trick: the % is the same as %%, but matches the smallest possible match:
Quote:

echo ${var% *}
the file system is full at
Good! I'm removed the last word.
But what if I want to remove the first word?
Well, there is another trick: the '#' is exactly like '%' (and '##' is like %%) but on the left:
Quote:

echo ${var##* }
90%
echo ${var#* }
file system is full at 90%
The documentation for this is in the bash documentation (man bash), which is very, VERY looooong.

helptonewbie 08-29-2008 07:10 AM

Ah cheers, after having a little go myself i get it now thanks...

%%[expression to match to remove everything from first match to end of contents of variable]*

##[go the opposite way]*
%%[t] means remove the last character if it matches t

well you may not understand what i wrote hear but the main thing is i get it :)

Thanks


All times are GMT -5. The time now is 03:02 PM.