Difference between revisions of "Script Course"
Jump to navigation
Jump to search
Line 17: | Line 17: | ||
[ -e /etc/passwd ] | [ -e /etc/passwd ] | ||
-e True if file exists | -e True if file exists | ||
'''Exit Status return codes''' | |||
*Every command returns an exit status | |||
* range from 0 - 255 | |||
* 0 = success | |||
* Other than 0 = error condition | |||
* use for error checking | |||
'''$?''' Contains the return code of the previously executed command | '''$?''' Contains the return code of the previously executed command | ||
ls /not/here | ls /not/here | ||
Line 29: | Line 38: | ||
echo "$HOST unreachable" | echo "$HOST unreachable" | ||
fi | fi | ||
=[[Bash| Bash menu]]-[[Main_Page| Home]]= | =[[Bash| Bash menu]]-[[Main_Page| Home]]= |
Revision as of 19:11, 17 March 2019
File Operators Tests
- -d File True if file is a directory
- -e File True if file exists
- -f File True if file exists and is a regular file
- -r File True if file is readable by you
- -s File True if file exist and is not empty
- -w File True if the file is writable by you
- -x File True if the file is executable by you
- -z String True if string is empty
- -n String true if string is not empty
Syntax
[condition to test for]
Example
[ -e /etc/passwd ] -e True if file exists
Exit Status return codes
- Every command returns an exit status
- range from 0 - 255
- 0 = success
- Other than 0 = error condition
- use for error checking
$? Contains the return code of the previously executed command
ls /not/here echo "$?"
-
HOST = "google.com" ping -c 1 $HOST if [ "$?" -eq "0" ] then echo "$HOST reachable." else echo "$HOST unreachable" fi