在linux的shell中 if 语句通过关系运算符判断表达式的真假来决定执行哪个分支.Shell 有三种 if ... else 语句: if ... fi 语句: if ... else ... fi 语句: if ... elif ... else ... fi 语句. 1) if ... else 语句 if ... else 语句的语法: if [ expression ] then Statement(s) to be executed if expression is true f
1. if/else 语句 语法: if condition; then commands;elif condition; then commands;else commands;fi 示例:需求:脚本需要1个参数才能正确运行,而在脚本执行时,如果指定的参数个数不等于1,则shell脚本就应该打印出一个错误信息,告知用户指定的参数个数不对,然后结束脚本的执行.#!/bin/bashif [ $# -ne 1 ];then echo "Error! Arguments are not correc
定义好了一个可以输出带颜色行号以及行数据的函数print_with_line_num,f()是测试函数.在f()函数中,读取文件并输出读取的每一行数据,但根据参数选项决定是普通输出行还是同时输出带颜色行号的行数据. 这可以当作是偏函数.闭包.作用域的一个用法示例. 脚本内容如下: #!/usr/bin/perl -w use strict; use 5.010; # print string with colored line_num # arg1: line num # arg2: strin