linux-shell-过程式编程语言的代码执行顺序

    技术2022-08-04  97

    过程式编程语言的代码执行顺序

    顺序执行: 逐条执行;

    选择执行: 代码有一个分支:条件满足时才会执行; 两个或以上的分支:只会执行其中一个满足条件分支;

    循环执行: 代码片段(循环体)要执行0,1或多个来回;

    选择执行: 单分支的if语句:

    if 测试条件 ;then 代码分支 fi

    或:

    if 测试条件 then 代码分支 fi

    双分枝的if语句:

    if 测试条件;then 条件为真时的分支 else 条件为假时的分支 fi

    练习:命令行传两个参数,比较后输出最大数 方法一:

    #!/bin/bash # if [ $# -lt 2 ];then echo "Two integers" exit 2 fi declare -i biger if [ $1 -gt $2 ];then biger=$1 fi if [ $1 -le $2 ];then biger=$2 fi echo "The biger one is $biger"

    方法二:

    #!/bin/bash # if [ $# -lt 2 ];then echo "Two integers" exit 2 fi declare -i biger=$1 if [ $1 -le $2 ];then biger=$2 fi echo "The biger one is $biger"

    比较这两个方法都能完成目的, 方法一:逻辑清晰 方法二:逻辑巧妙 我们代码的目的是为了得到结果,方法二简便,更应提倡方法二.

    Processed: 0.016, SQL: 9