Handling DCL ON ERROR actions after first error? - error-handling

The OpenVMS DCL command HELP ON EXAMPLE displays:
ON
Examples
1.$ ON SEVERE_ERROR THEN CONTINUE
A command procedure that contains this statement continues
to execute normally when a warning or error occurs during
execution. When a severe error occurs, the ON statement signals
the procedure to execute the next statement anyway. Once
the statement has been executed as a result of the severe
error condition, the default action (ON ERROR THEN EXIT) is
reinstated.
According to the help if neither [-]x.for nor [-]y.for exist then the last two lines will not be executed:
$ on error then $ continue
$ rename [-]x.for []
$ rename [-]y.for []
$ type *.for
Is there a way to set the ON ERROR handling as in the first line w/o placing an ON ERROR statement between each line of the script?

If the ON ERROR fires, you have to re-establish it. It looks like you
don't know whether any of the files exists. So the ON ERROR needs to be
re-established after the first failing command.
You can do this in a subroutine, like in:
$ on error then $ gosub on_error
$ rename [-]x.for []
$ rename [-]y.for []
$ on error then $ exit
$ type *.for
$ exit
$
$ on_error:
$ on error then $ gosub on_error
$ return
Also, you can handle this differently, with disabling error checking (SET
NOON):
$ set noon
$ rename [-]x.for []
$ rename [-]y.for []
$ set on
$ type *.for
or establishing error handling only for sever errors (ON SEVERE_ERROR):
$ on severe_error then $ exit
$ rename [-]x.for []
$ rename [-]y.for []
$ on error then $ exit
$ type *.for

Related

Executing BTEQ file via shell script (BTEQ: Command not found error)

I'm trying to set up an environment to execute BTEQ script via shell script in the local machine. On running the shell script I'm getting an error of BTEQ: Command not found. Not sure what I'm doing wrong.
I've created a separate .tdlogon file which contains .LOGON credentials. BTEQ script is a simple create table statement that I'm trying to execute.
My .tdlogon file is something like
.logon servername/uname,pwd
I'm calling the file like this
#!/bin/bash
server_path=/Users/xyz/xyz
log_path=/Users/xyz/xyz/logs
echo -e 'Starting the script'>> ${log_path}/test_log.log
cat ${server_path}/.tdlogon ${server_path}/code/temp_query.btq | bteq >> ${log_path}/test_log.log 2>&1
if [ ${rtn_code} -ne 0 ] ; then
echo -e 'Script completed successfully'>> ${log_path}/test_log.log
exit 0
else
echo -e 'Error in the script'>> ${log_path}/test_log.log
exit 1
fi
On executing the above code I'm getting below error in the log file
line 10: bteq: command not found
Appreciate any guidance related to this.
Seems like your Linux is not pointing to the bteq path. Update the bteq path:
export PATH=/usr/bin/bteq:$PATH
And, in some cases, there will be bteq32 instead of bteq in that case set path as:
export PATH=/usr/bin/bteq32:$PATH

Why doesn't sh with errexit work with a command list?

If I run the following shell script as a normal user, it aborts at line three as expected:
set -o errexit
echo foo > /bar
echo $?
Here is the output:
$ sh test1.sh
test.sh: 3: test.sh: cannot create /bar: Permission denied
However, if the echo command is a part of a compound list, the execution continues after the failing command and prints the exit code:
set -o errexit
{ echo foo; } > /bar
echo $?
Here is the output:
$ sh test2.sh
test.sh: 3: test.sh: cannot create /bar: Permission denied
2
How come the script doesn't abort? On the other hand, if I change the curly braces to parentheses it works like how I would expect.
The POSIX specification states that a shell "may exit" if a redirection error occurs with a compound command.
bash chooses to exit if the compound command is a subshell command ((...)), but otherwise chooses not to. I am not aware of the rationale for this distinction; it may be historical in nature.
set -e has many quirks, and often will not behave the way you expect. Many people advise that you simply not use it.

How to get error message from ditto command , when it fails to archive

Using ditto command we are archiving folder. When folder contains some files which does not have read permission. It fails to archive. That time ditto command logs error message saying " ditto: "Path" : Permission denied. How to get this error message.
As with any UNIX command, errors are written to stderr, which can be captured by adding 2> file to end of the command:
$ ditto src dst 2> error
$ cat error
ditto: /Users/andy/tmp/src/./x: Permission denied
If you are running ditto from a shell script, then something like this should work:
#!/bin/sh
errfile=/tmp/errors.$$
(cd ~/tmp; ditto src dst 2> $errfile)
if [ $? -ne 0 ]; then
echo There was a problem:
cat $errfile
else
echo Everything is cool
fi

Pig: how to exit on failure?

-- do something
store result into '$RESULT.tmp';
rmf $RESULT
mv $RESULT.tmp $RESULT
If exceptions thrown before rmf $RESULT, then the script should exit immediately.
This can be achieved with the -F or -stop_on_failure command line flag. If used, Pig will stop execution when the first failed job is detected and discontinue further processing. This also means that file commands that come after a failed store in the script will not be executed (this can be used to create "done" files).
This is how the flag is used:
$ pig -F myscript.pig
or
$ pig -stop_on_failure myscript.pig
Source: http://pig.apache.org/docs/r0.10.0/perf.html#error-handling

VxWorks compiling error

I have a problem when I compile a little program in WindWriver Workbench 3.3. I'm making the first tutorial, HelloWorld, but when I launch the compiler, it return the next error:
C:\Windows\Temp\make45206.sh: syntax error near unexpected token `&'
C:\Windows\Temp\make45206.sh: C:\Windows\Temp\make45206.sh: line 1:
`if [ ! -d "`dirname "HelloWorld_partialImage/Debug/HelloWorld_partialImage.o"`" ];
then mkdir -p "`dirname "HelloWorld_partialImage/Debug/HelloWorld_partialImage.o"`";
fi;
echo "building HelloWorld_partialImage/Debug/HelloWorld_partialImage.o";
dld -tX86LH:vxworks69 -X -r5 -f 0x90,1,1 -o "HelloWorld_partialImage/Debug/HelloWorld_partialImage.o" HelloWorld_partialImage/Debug/Objects/HelloWorld/main.o C:\WINDOWS\Microsoft.NET\Framework\v3.5;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;C:\Archivos de programa\Microsoft Visual Studio 9.0\VC\ATLMFC\LIB;C:\Archivos de programa\Microsoft Visual Studio 9.0\VC\LIB; && if [ "0" = "1" ];
then plink "HelloWorld_partialImage/Debug/HelloWorld_partialImage.o";
fi'
C:\WindRiver\utilities-1.0\x86-win32\bin\make.exe: *** [HelloWorld_partialImage/Debug/HelloWorld_partialImage.o] Error 2
I checked the Makefile but I don't see any error. Could you help me?
What is the contents of your Makefile?
I think the ; separators on the pile of long pathnames here might be the issue:
\Microsoft Visual Studio 9.0\VC\LIB; && if [ "0" = "1" ];
sh will interpret the ; as multiple commands to execute:
$ echo hello;echo hi
hello
hi
$
the ; && in this is going to confuse the shell, because both ; and && serve as command separators:
$ echo hello ; && echo hi
bash: syntax error near unexpected token `&&'
$
Try replacing the ; characters with : in whatever piece of the Makefile leads to those pathnames. (Or maybe spaces.)