Quantcast
Channel: How to check if a command succeeded? - Ask Ubuntu
Browsing all 11 articles
Browse latest View live

Answer by gndps for How to check if a command succeeded?

Prints the status of the last commandif [ $? == 0 ]; then echo passed;else echo failed; (exit 1); fi;Section echo passed and echo failed can be modified to do whatever you want depending on the status...

View Article



Answer by jcarballo for How to check if a command succeeded?

You can also configure bash to exit if a command fails, useful to avoid checks after every command. Place this at beginning of your script:set -e

View Article

Answer by Reinstate Monica for How to check if a command succeeded?

As mentioned in many other answers, a simple test of $? will do, like thisif [ $? -eq 0 ]; then something; fiIf you want to test if the command failed, you can use a shorter version in bash (but...

View Article

Answer by Alberto Salvia Novella for How to check if a command succeeded?

For easier debugging, I make commands to only output errors with:so [-tag] [commands]After that $? is 0 if success, otherwise failure.

View Article

Answer by Sergiy Kolodyazhnyy for How to check if a command succeeded?

It should be noted that if...then...fi and &&/|| type of approach deals with exit status returned by command we want to test( 0 on success ); however, some commands don't return a non-zero exit...

View Article


Answer by modrobert for How to check if a command succeeded?

command && echo $? || echo $?

View Article

Answer by Abraham Sanchez for How to check if a command succeeded?

command && echo OK || echo Failed

View Article

Answer by geirha for How to check if a command succeeded?

If you only need to know if the command succeeded or failed, don't bother testing $?, just test the command directly. E.g.:if some_command; then printf 'some_command succeeded\n'else printf...

View Article


Answer by Lekensteyn for How to check if a command succeeded?

The return value is stored in $?. 0 indicates success, others indicates error.some_commandif [ $? -eq 0 ]; then echo OKelse echo FAILfiLike any other textual value, you can store it in a variable for...

View Article


Answer by Shaun for How to check if a command succeeded?

$? should contain the exit status of the previous command, which should be zero for no error.So, something like;cd /nonexistantif [ $? -ne 0 ]then echo failedelse echo success!fifor most cases, it's...

View Article

How to check if a command succeeded?

Is there any way to check if there is an error in executing a command?Example : test1=`sed -i "/:@/c connection.url=jdbc:oracle:thin:@$ip:1521:$dataBase" $search`valid $test1function valid () { if...

View Article
Browsing all 11 articles
Browse latest View live




Latest Images