, ,
if [ -f this_file ]; then
if [ -f that_file ]; then
if [ -f other_file ]; then
echo "All files present"
fi
fi
fi
, .
if [ -f this_file ]; then
foo="True"
elif [ -f that_file ]; then
foo="True"
elif [ -f the_other_file ]; then
foo="True"
else
foo="False"
fi
if [ "$foo" = "True" ]; then
echo "One of the files exists"
fi
, if, . : - (AND list) - (OR list). , .
-
, . : 1 && 2 && 3 &&...
, true (), , . , false (), . && .
, , . - , , .
6. -
file_one ( , , ) file_two. - - .
#!/bin/sh
touch file_n
rm -f file_two
if [ -f file_one ] && echo "hello" && [ -f file_two ] && echo "there"
then
echo "in if"
else
echo "in else"
fi
exit 0
hello
in else
6 touch rm , file_n , file_two . - [ -f file_one ], true, c. , echo. (echo true). [ -f fiie_two ]. false, . . . false, echo . - false, if else.
|
|
-
, true, . :
1 || 2 || 3 ||...
. false, . , true, .
- -, , - false.
7.
#!/bin/sh
rm f file_one
if [ -f file_one ] || echo "hello" || echo "there"
then
echo "in if"
else
echo "in else"
fi
exit 0
:
hello
in if
. [ -f file one ] false, . echo, true, - . if true, - ( echo) .
, , .
, , . . , , . (short circuit evaluation).
:
[ -f file_one ] && true || false
. , , , .
, , - -, , {} . :
get_confirm && {
grep -v "$cdcatnum" $tracks_file > $temp_file
cat $temp_file > $tracks_file
echo
add_record_tracks
}
case
case . :
s in
[ | ]...) ;;
[ | ]...) ;;
es
case , . , , if, elif else.
(;;). case , case.
|
|
case . , case .
case , *, . , , , .
case, .
8.
#!/bin/sh
echo " ? yes no "
read timeofday
case "$timeofday" in
yes) echo " ";;
no) echo " ";;
) echo " ";;
n) echo " ";;
*) echo " , $timeofday . yes no ";;
esac
exit 0.
case, timeofday -. , case , ), .
case , . , *. * , , case, -. , .
9
#!/bin/sh
echo " ? yes no "
read timeofday
case "$timeofday" "in
yes | | Yes | YES) echo " ";;
n* | N*) echo: " ";;
*) echo." , .";;
esac
exit 0
case - , , . , , . , *, , . , never, n*, , .
for
for , . , , . :
for in
do
done
10.
#!/bin/sh
for foo in bar dog 413 do
echo $foo
done
exit 0
:
bar
dog
, for foo in bar dog 413 for foo in "bar dog 413"? , , , . .
foo for . , 413 , dog.
|
|
11. for
for . .
first. * . $file for.
. , , "f", .sh. :
#!/bin/sh
for file in $(ls f*.sh); do
echo $file
done
exit 0
$(). for , $(). f*.sh, , .
, , , , .
while
, for , , , .
, , , while :
while do
done
12. .
#!/bin/sh
echo "Enter password"
read trythis
while [ "$trythis"! = "secret" ]; do
echo "Sorry, try again"
read trythis
done
exit 0
- :
Enter password
password
Sorry, try again
secret
$
, while. , do done, , (true). c, trythis secret. , $trythis secret. , done.
until
until :
until
do
done
while, . , , (true).
13. , . 0.
#!/bin/sh
sum=0
read num
until [ $num eq 0 ]; do
sum=$((sum+num))
read num
done
echo $sum
exit 0
, , while; , until.
, . , . : , .
|
|
, .
_ ()
{
}
14.
.
#!/bin/sh
foo () {
echo "Function foo is executing"
}
echo "script starting"
foo
echo "script ended"
exit 0
C :
script starting
Function foo is executing
script ended
. foo () {, , , foo. foo }. foo, , . , , foo.
, , , Pascal, , , (forward) . , , , , .
, $*, $#, $1, $2 . . . , . , .
return. , . echo , .
foo () { echo JAY;}
result="$(foo)"
local. . , . , , . , .
#!/bin/sh
sample_text="global variable"
foo () {
local sample_text="local variable"
echo "Function foo is executing"
echo $sample_text
}
echo "script starting"
echo $sample_text
foo
echo "script ended"
echo $sample_text
exit 0
return, , .
, my_name, , , true false. , , .
1. yes_or_no
#!/bin/sh
yes_or_no () {
echo "Is your name $*? "
while true do
echo -n "Enter yes or no: "
read x
case "$x" in
| yes) return 0;;
n | no) return 1;;
*) echo "Answer yes or no"
esac
done
}
2. .
echo "Original parameters are $*"
if yes_or_no "$1"
then
echo "Hi $1, nice name"
else
echo "Never mind"
fi
exit 0
:
$./m_nm Rick Neil
Original parameters are Rick Neil
Is your name Rick?
Enter yes or no: yes
Hi Rick, nice name
$
, , . if yes_or_n, $1 Rick. , $1, $2 . ., . if .
|
|
. , "" , ( ), ( ). . , - POSIX. , , , .
, , , .
break
break for, while until , .
15
#!/bin/sh
rm -rf fred*
echo > fred1
echo > fred2
mkdir fred3
echo > fred4
for file in fred*
do
if [ -d "$file" ]; then
break;
fi
done
echo first directory starting fred was $file
rm -rf fred*
exit 0
continue
, for, while until . .
16
#!/bin/sh
rm -rf fred*
echo > fred1
echo > fred2
mkdir fred3
echo > fred4
for file in fred*
do
if [ -d "$file" ]; then
echo "skipping directory $file"
continue
fi
echo file is $file
done
rm -rf fred*
exit 0
continue , .
, . , . . . ,
for in 1 2 3
do
echo before $x
continue 1
echo after $x
done
:
before 1
before 2
before 3
echo printf
echo - . Linux - echo -n "string to output"
Printf
printf . / , echo , , .
printf .
printf " " 1 2...
C++. , . , escape- . , \ %, .
. 3 escape-.
3. escape-
Escape- | |
\" | |
\\ | |
\n | |
\r | |
\t | |
\v | |
\ | |
\ | HH |
%, . . 4.
4
d | |
c | |
s | |
% | % |
, :
$ printf "%s\n" hello
hello
$ printf "%s %d\t%s" "Hi There" 15 people
Hi There 15 people
, Hi There ("").
return
return , . , , . , return .
shift
shift - , $2 $1, $3 - $2 . . $1 , $0 . shift , . $* $# -.
shift , , 10 , shift 10- .
, :
#!/bin/sh
while [ "S1"!= ""]; do
echo "$1"
shift
done
exit 0
stat
stat , .
.
stat res
res , res .
stat -f res
res , , c res.
, . , %s stat
stat res c %s
stat stat --help