• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

XiaoXiaoli

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

shell编程_流程控制_if

if-then

 1 语法格式1
 2   if command
 3   then
 4      comands
 5   fi
 6  
 7 语法格式2
 8  if [ command ];then
 9     comands
10   fi    

 

#! /bin/bash 
if date
then
  echo "it worked"
fi
#date命令执行成功,退出状态码为0,then中的echo语句就会输出。

  

#! /bin/bash 
if asdfg
then
        echo "it did not work"
fi
echo "we are outside of the if statement"
#asdfg命令无法执行,退出状态码非0,then中的echo语句无法执行。

 

#! /bin/bash
if [ $# != 1 ];then
    echo "program argv[1]"
fi

  

  

 

if-then-else

if-then-else语句其语法格式如下。

if command
then
    commands
else
    comands
fi

 

嵌套if

if command1
then
    command set 1
elif command2
then
    command set 2
elif command3
then
    command set 3
elif command
then
  command set4      
fi

  

 

posted on 2020-12-22 21:27  XiaoXiaoli  阅读(119)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3