Created
July 21, 2018 10:43
-
-
Save xie-qianyue/a0bd1d5d6e542fb7c024fe17c29c8ad8 to your computer and use it in GitHub Desktop.
test if shell script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
if [ "$X" -lt "0" ] | |
then | |
echo "X is less than zero" | |
fi | |
if [ "$X" -gt "0" ]; then | |
echo "X is more than zero" | |
fi | |
[ "$X" -le "0" ] && \ | |
echo "X is less than or equal to zero" | |
[ "$X" -ge "0" ] && \ | |
echo "X is more than or equal to zero" | |
[ "$X" = "0" ] && \ | |
echo "X is the string or number \"0\"" | |
[ "$X" = "hello" ] && \ | |
echo "X matches the string \"hello\"" | |
[ "$X" != "hello" ] && \ | |
echo "X is not the string \"hello\"" | |
[ -n "$X" ] && \ | |
echo "X is of nonzero length" | |
[ -f "$X" ] && \ | |
echo "X is the path of a real file" || \ | |
echo "No such file: $X" | |
[ -x "$X" ] && \ | |
echo "X is the path of an executable file" | |
[ "$X" -nt "/etc/passwd" ] && \ | |
echo "X is a file which is newer than /etc/passwd" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We test with:
$ X=5
$ export X
$ ./test_if.sh
... output of test_if.sh ...
$ X=hello
$ ./test_if.sh
... output of test_if.sh ...
$ X=test_if.sh
$ ./test_if.sh
... output of test_if.sh ...