需求描述: 写shell脚本的过程中,有时会需要获取脚本的名字,比如,有的时候,脚本 中会有usage()这种函数,可能就会用到脚本的名字. 实现方法: shell脚本中,通过使用$0就可以获取到脚本的名字或者说脚本本身. 操作过程: 1.通过以下的脚本写了一个脚本的使用函数usage() #!/bin/bash #function usage means how to use this script. usage() { echo "Usage: $0 process_name1"
Linux shell脚本中shift的用法说明 shift命令用于对参数的移动(左移),通常用于在不知道传入参数个数的情况下依次遍历每个参数然后进行相应处理(常见于Linux中各种程序的启动脚本). 示例1:依次读取输入的参数并打印参数个数: run.sh: #!/bin/bash while [ $# != 0 ];do echo "第一个参数为:$1,参数个数为:$#" shift done 输入如下命令运行:run.sh a b c d e f 结果显示如下: 第一个参数为
Linux shell脚本编程 流程控制: 循环语句:for,while,until while循环: while CONDITION; do 循环体 done 进入条件:当CONDITION为“真”: 退出条件:当CONDITION为“假”: while CONDITION; do 循环体 控制变量的修正表达式 done 示例:求100以内所有正整数之和; #!/bin/bash # declare -i sum=0 declare -i i=1 while [ $i -le 100 ]; d
Linux shell脚本编程(二) 练习:求100以内所有偶数之和; 使用至少三种方法实现; 示例1: #!/bin/bash # declare -i sum=0 #声明一个变量求和,初始值为0 for i in $(seq 0 2 100); do sum=$(($sum+$i)) done echo "Even sum: $sum." 示例2: #!/bin/bash # declare -i sum=0 for i in {1..100}; do if [ $[$i%2] -