if then

  1. // good
  2. if [ -d public ]
  3. then
  4. echo "public exist"
  5. if
  6. // good
  7. if [ -d public ]; then
  8. echo "public exist"
  9. if
  10. // error: ifthen写成一行时,条件后必须加上分号
  11. if [ -d public ] then
  12. echo "public exist"
  13. if
  14. // error: shell对空格比较敏感,多个空格和少个空格,执行的含义完全不同
  15. // 在[]中,内侧前后都需要加上空格
  16. if [-d public] then
  17. echo "public exist"
  18. if

if elif then

  1. if [ -d public ]
  2. then
  3. echo "public exist"
  4. elif
  5. then

循环

switch

常用例子

判断目录是否存在

  1. if [ -d public ]
  2. then
  3. echo "public exist"
  4. if

判断文件是否存在