整理中…
printf
printf "0x%0x\n" 255
输出
0xff
test
s="hi"
test "$s" = "hi"
echo $?
[ "$s" = "hi" ]
echo $?
输出
0
0
if
if command
then
commands if condition true
else
commands if condition false
fi
if command 检测的是命令的退出码$?是否为0
if ls; then
echo "ls ok"
else
echo "ls not ok"
fi
if test ls; then
echo "ls ok"
else
echo "ls not ok"
fi
if [ ls ]; then
echo "ls ok"
else
echo "ls not ok"
fi
输出
ls ok
ls ok
ls ok
s="c"
if [ $s = "a" ]; then
echo $s
elif [ $s = "b" ]; then
echo $s
else
echo "unknow"
fi
输出
unknow
for
for ((i = 0; i < 3; i++)) {
echo -n "$i "
}
echo ""
for i in 0 1 2; do
echo -n "$i "
done
echo ""
输出
0 1 2
0 1 2
while
i=0
while [ $i -lt 3 ]; do
echo -n "$i "
i=`expr $i + 1`
done
echo ""
i=0
while [ $i -lt 3 ]; do
echo -n "$i "
((i++))
done
echo ""
输出
0 1 2
0 1 2
IFS
分割符
s="/usr/local/bin:/usr/bin:/bin"
for p in $s; do
echo $p
done
echo ""
IFS=:
for p in $s; do
echo $p
done
输出
/usr/local/bin:/usr/bin:/bin
/usr/local/bin
/usr/bin
/bin