How to get the return value of xcodebuild? - xcodebuild

I'm using xcodebuild inside a bash script on a continuous integration server.
I would like to know when a build as failed in the script, so I can exit prematurely from it and mark the build as failed.
xcodebuild displays a BUILD FAILED message to the console, but I don't succeed in getting a return value.
How can I achieve this?
Thanks in advance

I solved my problem using this command: xcodebuild -... || exit 1

You can use the "$?" variable to get the return code of the previous command.
xcodebuild -...
if [[ $? == 0 ]]; then
echo "Success"
else
echo "Failed"
fi

xcodebuild always returns 0, regardless of the actual test result. You should check for either ** BUILD FAILED ** or ** BUILD SUCCEEDED ** in the output to know whether tests pass or not.

Xcodebuild can return any of the error codes listed below and not restricted to EX_OK (or int 0).
However, I learnt from solution provided by Dmitry and modified as following. It works for me and I hope it could be helpful.
xcodebuild -project ......
if test $? -eq 0
then
echo "Success"
else
echo "Failed"
fi
Error codes
The following are the error codes that can be returned by sysexits archive:
EX_OK (0): Successful exit.
EX_USAGE (64): The command was used incorrectly, e.g., with the wrong number of arguments, a bad flag, a bad syntax in a parameter, or whatever.
EX_DATAERR (65): The input data was incorrect in some way. This should only be used for user's data and not system files.
EX_NOINPUT (66): An input file (not a system file) did not exist or was not readable. This could also include errors like ``No message'' to a mailer (if it cared to catch it).
EX_NOUSER (67): The user specified did not exist. This might be used for mail addresses or remote logins.
EX_NOHOST (68): The host specified did not exist. This is used in mail addresses or network requests.
EX_UNAVAILABLE (69): A service is unavailable. This can occur if a support program or file does not exist. This can also be used as a catchall message when something you wanted to do doesn't work, but you don't know why.
EX_SOFTWARE (70): An internal software error has been detected. This should be limited to non-operating nonoperating operating system related errors as possible.
EX_OSERR (71): An operating system error has been detected. This is intended to be used for such things as cannot fork'',cannot create pipe'', or the like. It includes things like getuid returning a user that does not exist in the passwd file.
EX_OSFILE (72): Some system file (e.g., /etc/passwd, /var/run/utmp, etc.) does not exist, cannot be opened, or has some sort of error (e.g., syntax error).
EX_CANTCREAT (73): A (user specified) output file cannot be created.
EX_IOERR (74): An error occurred while doing I/O on some file.
EX_TEMPFAIL (75): Temporary failure, indicating something that is not really an error. In send-mail, sendmail, mail, this means that a mailer (e.g.) could not create a connection, and the request should be reattempted later.
EX_PROTOCOL (76): The remote system returned something that was ``not possible'' during a protocol exchange.
EX_NOPERM (77): You did not have sufficient permission to perform the operation. This is not intended for file system problems, which should use EX_NOINPUT or EX_CANTCREAT, but rather for higher level permissions.
EX_CONFIG (78): Something was found in an unconfigured or misconfigured state.
For more info click here.

Maybe it's not because of the xcodebuild not returning non-zero when build failed. Your shell script continuing to run regardless of the returning-error line might be the result of that you didn't run the script with a "-e" option.
Try put #!/bin/bash -e ahead of the script file.

Generally, you can always check the exist value for the last run process in Unix bash by:
$ echo $?
where $? is a built-in placeholder of the exist value of last executed command. For more details about other bash's built-in variables see here.
So, first run your command you want to investigate its return code, then run echo as above.

Whether the compiled product (.a or .ipa file)exists

Related

Automic: How to implement Error-Handling in Script

I want to check a file in Unix via Automic. If the file doesnt exist it should switch the host and check if the file is there.
The problem is, that I dont now how to implement a error handling.
Everytime the script object is processing and cant find the file the skript aborted. I need a new starting point in the skript but "ON_ERROR" or ":RESTART" doesnt work.
How can I implement a logic like this: IF the script aborted due to the error-massage 'No such file or directory'start the script from here instead.
Thank you very much for your help!
Best regards
I have solved it. Use the function PREP_PROCESS_FILENAME to check if the file exists in the folder!
You have to start the task twice in the same workflow. The task-job checks if the script exists otherwise nothing to do.
if [ -f "/path/to/script" ]
then
bash /path/to/script
else
echo "Script not found"
fi
In Post-Script you can modify the state for the empty task with :MODIFY_STATE. Depend on report or returncode

Check whether process running linux and start with certain description

I have multiple scripts running. All the scripts have the same name but the commands they execute are doing different things.
I am trying to figure out if a certain instance of the script is running and if so I don't want it to run again. This is difficult because all of the scripts have the same name so it could be a false positive by using pgrep.
My idea was if there is any attribute or description I could attach when it is first run, then I can grep for that unique description and tell which instance is running, is there any way to do this?
Does anyone have any other ideas?
Thanks
You can implement below logic to check weather script is already running or not.
if [ -f Script1.lck ];then
echo "ALREADY INSTANCE RUNNING `date`"
echo "EXITING"
else
echo "NO INSTANCE RUNNING "
touch Script1.lck
#Your Script code here.............
rm -f Script1.lck
fi
*I used concept that every time it runs checks for Script1.lck file if file not exists than it means no instance is running. So it creates file and start executing your code.
Suppose in between you executed the script then it checks for .lck file and and .lck already exists due to previous instance.
*In last i removed the .lck file.
*By using different lck file names for different script you can check which script is running.

Zsh trouble when using echo with color/formatting characters

I'm just switch to zsh and now adapting the alias in which was printing some text (in color) along with a command.
I have been trying to use the $fg array var, but there is a side effect, all the command is printed before being executed.
The same occur if i'm just testing a echo with a color code in the terminal:
echo $fg_bold[blue] "test"
]2;echo "test" test #the test is in the right color
Why the command print itself before to do what it's supposed to do ? (I precise this doesn't happen when just printing whithout any wariable command)
Have I to set a specific option to zsh, use echo with a special parameter to get ride of that?
Execute the command first (keep its output somewhere), and then issue echo. The easiest way I can think of doing that would be:
echo $fg[red] `ls`
Edit: Ok, so your trouble is some trash before the actual output of echo. You have some funny configuration that is causing you trouble.
What to do (other than inspecting your configuration):
start a shell with zsh -f (it will skip any configuration), and then re-try the echo command: autoload colors; colors; echo $fg_bold[red] foo (this should show you that the problem is in your configuration).
Most likely your configuration defines a precmd function that gets executed before every command (which is failing in some way). Try which precmd. If that is not defined, try echo $precmd_functions (precmd_functions is an array of functions that get executed before every command). Knowing which is the code being executed would help you search for it in your configuration (which I assume you just took from someone else).
If I had to guess, I'd say you are using oh-my-zsh without knowing exactly what you turned on (which is an endless source of troubles like this).
I don't replicate your issue, which I think indicates that it's either an option (that I've set), or it's a zsh version issue:
$ echo $fg_bold[red] test
test
Because I can't replicate it, I'm sure there's an option to stop it happening for you. I do not know what that option is (I'm using heavily modified oh-my-zsh, and still haven't finished learning what all the zsh options do or are).
My suggestions:
You could try using print:
$ print $fg_bold[red] test
test
The print builtin has many more options than echo (see man zshbuiltins).
You should also:
Check what version zsh you're using.
Check what options (setopt) are enabled.
Check your ~/.zshrc (and other loaded files) to see what, if any, options and functions are being run.
This question may suggest checking what TERM you're using, but reading your question it sounds like you're only seeing this behaviour (echoing of the command after entry) when you're using aliases...?

Return code/Exit status of the WebPICmd.exe

Is there a way to detect the exist status of WebPICmd.exe (Command line version of WebPlatformInstaller), so that I can use it in my script.
I need to know if installation of particular product succeeded or not. (Trying to install PHP53, which seems to fail randomly due to download failures)
This can be detected using $LASTEXITCODE variable in PowerShell.
In dos mode it should be LASTERRORLEVEL.
Remember to check this variable immediately after running the WebPICmd.exe so that it's return code is not overwritten by something else down the line.

How can I stop sourcing a (t)csh script on a certain condition without exiting the shell?

I have to source a tcsh script to modify environment variables.
Some tests are to be done and if any fails the sourcing shall stop without exiting the shell. I do not want to run the script as a subprocess because I would need to modify env variables in the parent process which a subprocess cannot do. This is similar but different to this question where the author actually can run the script as a subprocess.
The usual workaround is to create an alias which runs a script (csh/bash/perl/python/...) which writes a tempfile with all the env var settings and at the end sources & deletes that tempfile. Here's more info for those interested (demoing a solution for bash). For my very simple and short stuff I'm doing that additional alias is not wanted.
So my workaround is to provoke a syntax error which stops any source execution. Here's an example:
test $ADMIN_USER = `filetest -U: $SOME_FILE` || "Error: Admin user must own admin file"
The shortcircuit || causes the error text to be ignored in case of goodness. On a test failure the error text is interpreted as a command, not found, the source stops and produces a reasonable error message:
Error: Admin user must own admin file: Command not found.
Is there any nicer way in doing this? Some csh/tcsh built-in that I've overlooked?
Thanks to a discussion with the user shellter I just verified my assumption that
test $ADMIN_USER = `filetest -U: $SOME_FILE` || \
echo "Error: Admin user must own admin file" && \
exit
would actually quit the enclosing interactive shell. But it does not.
So the answer to my above question actually is:
Just use a normal exit and the source will stop sourcing the script while keeping the calling interactive shell running.