How to debug or see details with bash "exit code 1"? - bash

I have a simple bash script in azure devops which goes wrong, I want to see error details, but I dont know how:
Generating script.
Script contents:
cat pipelines\\backend\\services\\Authorization_Service.json
C:\Windows\system32\bash.exe -c pwd
##[error]The process 'C:\Windows\system32\bash.exe' failed with exit code 1
Finishing: Bash

Related

Gitlab CI ignores script exit code other than 1

I'm trying to set up a GitLab pipeline, so that certain exit_codes are okay for a script I'm executing.
I have tried both shell and a ruby script, but both seem to have the same behaviour.
test_job:
stage: build
image: ruby:3.0
script:
- chmod +x ci/wrap.sh
- ./ci/wrap.sh
allow_failure:
exit_codes:
- 64
As you can see, I am just executing the script and nothing more, my expectation would be, that the last script executed is used a the exit status for the job.
In the script I'm only calling exit 64, which should be a "allowed failure" in that case, the pipeline log however says that the job failed because of exit code 1:
How do I get GitLab to accept the exit code of this (or a ruby) script as the job exit code?
I found a way to fix this problem. Apparently Gitlab Runner uses the -e flag, which means that any non-zero exit code will cancel the job. This can be updated by using set +e, but then you still need to capture the actual exit code for the job.
Using $? in two different lines of the configuration does not work, because Gitlab does echo calls in-between them.
So the exit code needs to be captured directly, example:
script:
- set +e
- ruby "ci/example.rb" || EXIT_CODE=$?
- exit $EXIT_CODE
Here's my trick for turning off early failure and checking the result code later:
script:
- set +e +o pipefail
- python missing_module.py 2>&1 | grep found; result=$?
- set -e -o pipefail
- "[ $result == 0 ]"
This turns off early exit and runs a command that we consider to have an acceptable failure if "found" is in the error text. It then turns early exit back on and tests whether the exit code we saved was good or not.

$? from bash script command executed by TCL (open pipe) on windows returns wrong value

I've got tcl script with two ways of execution bash script:
#exec bash ./run.sh
open "|bash ./run.sh r"
The bash script is shown below:
#!/bin/bash
ls
if [ "$?" != "0" ]; then
echo "ERROR: Status failed!" > status
else
echo "Everything is OK!" > status
fi
I'm using tclsh for Windows with bash from git bash. When I use:
exec bash ./run.sh
I've got in status file:
Everything is OK!
otherwise:
open "|bash ./run.sh r"
got:
ERROR: Status failed!
Is there any possibility to correctly detect exit code when opened the tcl pipe?
You don't describe whether you get different results out of the ls part of the script. That matters; the ls command is most certainly capable of changing its behaviour according to the environment in which it is invoked. This matters because Tcl executes subprocesses (on Windows) directly using the CreateProcess() system call, rather than the various wrapped versions that Cygwin and git bash use. Other possibilities are that you're launching the script in a different directory and so on.
However, in general we'd expect a script to behave very similarly when launched via exec or via open |… r as they share a common core of functionality. The only differences are to do with how output and termination are waited for.
If you create a subprocess pipeline, by default you won't get to find out about errors from it until you close the pipeline. exec generates any errors “immediately” because it doesn't return control to you until the subprocess has terminated and all output has been read.

How to debug a tcsh script and put the debugged output in a file?

I want to debug my tcsh script. Like in sh script if we add set command with -x then it prints the execution of script.
#! /bin/sh
set -x
exec 2>/usr/bin/error.log
#My script code
And I tried this as explained here . But still don't know how to print in a file? I am writing below code:
#! /bin/tcsh
set echo
exex 2>/usr/bin/error.log
I am getting permission denied error.I have also tried running as root still same error. But that can be the error in my script. So I want to check in error.log where does the error occur. But after I run script the error.log file is not getting generated. Is there any other way to write stderror for tcsh? Can we use exec command in tcsh?
Also are tcsh and csh both same things or different?

How to change exit code message of bash

I have a CI CD pipeline where I use a bash task to execute some script.
I want the build to fail if the script fails. So now I implemented an exit code where the bash script needs to fail. All good except for the error message.
I want to use a exit code whereby I can set an error message something like:
exit "script failed because alerts were found"
But I found online that you can only pass integers to your exit code.
Tried to fix it with:
echo "fail message"
exit code 2
But on my pipeline I got the Bash exited with code '2'. message and after i open the task output i can see my echo message.
Don't know if this is a Azure DevOps issue or I can fix this in bash. Anyone has any ideas?
[EDIT] Tried it with trap inside my local machine, and the custom error message works. But the same thing didn't work in Azure DevOps. I might think this is not possible in Azure DevOps -_-. Is there someone who already tried this ?
Perhaps the console in the screenshot shows only the script's standard error?
I would try something like:
echo "fail message" 1>&2
exit code 2
I'd suggest you have a look at "trap" command :
https://www.shellscript.sh/trap.html
and try something like :
trap 'your_function' EXIT

Jenkins execute shell output mystery

I'm tyring to run some build script in MacOS 10.11 but get no information about errors occurred during it's running. Even I try
<hudson.tasks.Shell>
<command>echo "fucking Jenkins!!!"
#!/bin/bash -l
echo "fucking Jenkins!!!"
/bin/sh -e
echo "fucking Jenkins!!!"
./build.sh $BUILD_NUMBER "Jenkins test build"
echo "fucking Jenkins!!!"</command>
</hudson.tasks.Shell>
I get in the console output
git rev-list ...
and then failure or success depending I set -e choice or not. Even echo command writes nothing. But the fact of failure doesn't help at all if there's no more information. No more options I could find over the Web. What is the secret of the execute shell plugin to make it write something to the console output.

Resources