Pretty new to shell scripting. I am trying to do the following:
#!/bin/bash unzip myfile.zip #do stuff if unzip successful
I know that I can simply group the teams together with && , but there is quite a piece, it will not be terribly supported.
&&
You can use the exit status of the command explicitly in the test:
if ! unzip myfile.zip &> /dev/null; then # handle error fi
Can you use $? . It returns:- 0 if the command was successfully completed.-! 0 if the team was unsuccessful.
$?
So you can do
#!/bin/bash unzip myfile.zip if [ $? -eq 0 ]; then #do stuff on unzip successful fi
and in case of an error, you can even use this:
[ $? ] && echo "error!"
$ cat a hello $ echo $? 0 $ cat b cat: b: No such file or directory $ echo $? 1
Variable $? contains the exit status of the previous command. The successful exit status for (most) teams is (usually) 0, so just check this ...
#!/bin/bash unzip myfile.zip if [ $? == 0 ] then # Do something fi
If you want the shell to check the result of executed commands and stop interpreting when something returns a non-zero value, you can add set -e , which means Exit immediately if the command exits with non-zero status. I often use this in scripts.
set -e
#!/bin/sh set -e # here goes the rest
Source: https://habr.com/ru/post/946504/More articles:50% / 50% div, on click 100% animation - jqueryUsing the Ant classpath in Eclipse - javaObject is located after PostAsync with HttpClient - .netMonodroid environment variables - variablesHow to effectively implement Grails services with Spring resource.groovy - spring100% resizable divs floating to the left inside a div container - jqueryZxing barcode scanner plugin for an Android phone not running on Nexus 7 that only has a front camera - androidSafety Locking Grid - c ++Using Apache Ivy with netbeans - javahttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/946509/how-do-i-add-a-folder-full-of-jar-files-to-my-classpath-in-eclipse&usg=ALkJrhhWzJq6hTE2RSb0DroIyJXpNRHBaAAll Articles