Test - Shell Scripting Tutorial
Test - Shell Scripting Tutorial
html
1 of 9 26-Jan-19, 7:01 PM
Test - Shell Scripting Tutorial https://www.shellscript.sh/test.html
Share
test test
[ [ test
[ test
$ type [
[ is a shell builtin
$ which [
/usr/bin/[
$ ls -l /usr/bin/[
lrwxrwxrwx 1 root root 4 Mar 27 2000 /usr/bin/[ -> test
$ ls -l /usr/bin/test
-rwxr-xr-x 1 root root 35368 Mar 27 2000 /usr/bin/test
[ ls
2 of 9 26-Jan-19, 7:01 PM
Test - Shell Scripting Tutorial https://www.shellscript.sh/test.html
if [$foo = "bar" ]
if test$foo = "bar" ] ]
[
==
= -eq
man
test
if while
test
if...then...else...
if [ ... ]
then
# if-code
else
# else-code
fi
fi if
esac
if [ ... ] then
;
if [ ... ]; then
# do something
fi
elif
if [ something ]; then
echo "Something"
elif [ something_else ]; then
echo "Something else"
else
echo "None of the above"
fi
3 of 9 26-Jan-19, 7:01 PM
Test - Shell Scripting Tutorial https://www.shellscript.sh/test.html
$ X=5
$ export X
$ ./test.sh
... output of test.sh ...
$ X=hello
$ ./test.sh
... output of test.sh ...
$ X=test.sh
$ ./test.sh
... output of test.sh ...
$X
/etc/hosts
#!/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"
;
if
\
4 of 9 26-Jan-19, 7:01 PM
Test - Shell Scripting Tutorial https://www.shellscript.sh/test.html
\ ;
; if then
test
-a -e -S
-nt -ot -ef
-O
if && ||
#!/bin/sh
[ $X -ne 0 ] && echo "X isn't zero" || echo "X is zero"
[ -f $X ] && echo "X is a file" || echo "X is not a file"
[ -n $X ] && echo "X is of non-zero length" || \
echo "X is of zero length"
[
test
if...then...else...
[...]
!=
5 of 9 26-Jan-19, 7:01 PM
Test - Shell Scripting Tutorial https://www.shellscript.sh/test.html
echo
$?
grep grep [0-9]
^
grep [^0-9]
>/dev/null 2>&1
grep -v [0-9]
#!/bin/sh
X=0
while [ -n "$X" ]
do
echo "Enter some text (RETURN to quit)"
read X
echo "You said: $X"
done
while [ -n "$X" ]
6 of 9 26-Jan-19, 7:01 PM
Test - Shell Scripting Tutorial https://www.shellscript.sh/test.html
$ ./test2.sh
Enter some text (RETURN to quit)
fred
You said: fred
Enter some text (RETURN to quit)
wilma
You said: wilma
Enter some text (RETURN to quit)
You said:
$
#!/bin/sh
X=0
while [ -n "$X" ]
do
echo "Enter some text (RETURN to quit)"
read X
if [ -n "$X" ]; then
echo "You said: $X"
fi
done
if
if [ ! -n "$X" ]; then
echo "You said: $X"
fi
if then
if then
if [ ! -n "$X" ]
echo "You said: $X"
then fi
Share
7 of 9 26-Jan-19, 7:01 PM
Test - Shell Scripting Tutorial https://www.shellscript.sh/test.html
Purchase flow
1. 1. Product preview
2. 2. Payment form
Optimize for conversion (?)
Require shipping information
More information: Required
3. 3. Receipt
Style
Pick a theme:
Locked
Customize:
No file selected.
8 of 9 26-Jan-19, 7:01 PM
Test - Shell Scripting Tutorial https://www.shellscript.sh/test.html
9 of 9 26-Jan-19, 7:01 PM