(, , ). , , .
, $foo, , . $- , . , .
1.
, :
#!/bin/sh
myvar="Hi there"
echo $myvar
echo "$myvar"
echo '$myvar'
echo \$myvar
echo Enter some text
read myvar
echo '$myvar' $myvar
exit 0
:
$./variable
Hi there
Hi there
$myvar
$myvar
Enter some text
Hello world
$myvar Hello World
myvar, Hi there. echo, , $ . , . read .
. , ( ), . :
$
$
$PS1 . $, bash . , [\u@\h \w]$ , , $.
$PS2 , ; >
$#
-
, . , $# 0.
- . 1.
1
- | |
$1, $2,... | , |
$* | , ifs. |
. , , .
|
|
, , , .
test [
[ test - . [ test - , , [ ]. [ .
test , Linux, test. , , test.
test : . : test -f <_>,
if test -f fred.c then
fi
To :
if [ -f fred.c ] then
fi
test ( ) , .
[ . , , [ , test, .
then , if, test then:
if [ -f fred.c ]; then fi
, test, :
,
,
.
. 2.
2.
1 = 2 | True (), | |
1!= 2 | True (), | |
-n | True (), null | |
-z | True (), null ( ) | |
1 -eq 2 | True (), | |
1 -n 2 | True (), | |
1 -gt 2 | True (), 1 , 2 | |
1 -g 2 | True (), 1 2 | |
1 -lt 2 | True (), 1 , 2 | |
1 -le 2 | True (), 1 2 | |
! | True (), , | |
-d | True (), | |
- | True (), | |
-f | True (), | |
-r | True (), | |
-s | True (), | |
-w | True (), | |
- | True (), | |
2 /bin/bash.
#!/bin/sh
if [ -f /bin/bash ]
then
echo "file /bin/bash exists"
fi
if [ -d /bin/bash ]
then
echo "/bin/bash is a directory"
|
|
else
echo "/bin/bash is NOT a directory"
fi
, , . test, .
, .
, , .
if
if : .
if then
else
fi
if , , .
3
#!/bin/sh
echo " ? yes no"
read timeofday
if [ $timeofday = "yes" ]; then
echo " "
else
echo " "
fi
exit 0
:
? yes no yes
yes
$
timeofday [. if, .
, if ; .
elif
, . -, n () yes (). , elif, , else if ( 4).
, , - yes n. else elif :
4
#!/bin/sh
echo " ? yes no"
read timeofday
if [ $timeofday = "yes" ]
then
echo " "
elif [ $timeofday = "no" ]; then
echo " "
else
echo ", $timeofday . yes no "
exit 1
fi
exit 0
4 , , true, elif . , , 1, .
,
, . , <Enter>. :
[: =: unary operator expected
? if. timeofday, . , if : if [ = "yes" ] . : if [ "$timeofday" = "yes" ]
:
if [ "" = "yes" ]
:
5.
#!/bin/sh
echo " ? yes no "
read timeofday
if [ "$timeofday " = "yes" ]
|
|
then
echo " "
elif [ "$timeofday" = "no" ]; then
echo " "
else
echo ", $timeofday . yes no "
exit 1
fi
exit 0
, <Enter>.
. , echo , printf (. "printf" ) echo. bash echo n. :
echo -n " ? yes no: "
.