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] -
shell是一种程序设计语言,是访问操作系统内核的服务. Linux的shell种类常见的有: Bourne Shell(/usr/bin/sh或/bin/sh) Bourne Again Shell(/bin/bash) C Shell(/usr/bin/csh) K Shell(/usr/bin/ksh) Shell for Root(/sbin/sh) Shell脚本执行的两种方法 ./shell.sh 根据shell脚本第一行指定的shell执行 /bin/sh test.sh 根据命令
4.控制流结构 4.1.控制结构 4.2.if then else语句 格式: if 条件1 //如果条件1为真 then 命令1 //那么,执行命令1 elif 条件2 //如果条件1不成立,而条件成立 then 命令2 //那么,执行命令2 ……更多的elif then组合…… else 命令2 //如果条件1.2……都不成立,那么执行命令3 fi //完成,if语句必须以单词fi终止 最简可以是形式: if 条件 then 命令 fi 等价于 if
shell的if判断 2012-03-16 14:53:05 分类: Python/Ruby 1 概要 有时候你需要指定shell脚本中的依靠命令的成功与否来实施不同过程的行为. if 结构允许你来指定这样的条件. 最精简的 if 命令的语法是: if TEST-COMMANDS; then CONSEQUENT-COMMANDS; fi TEST-COMMAND 执行后且它的返回状态是0,那么 CONSEQUENT-COMMANDS 就执行.返回状态是最后一个命令的退出状态,或者当没有条
the Shell Profile: When a new interactive shell is started, /etc/profile, followed by /etc/bash.bashrc(if a bash shell), ~/.profile, and finally ~/.bashrc are executed in that order. PATH You can set your PATHenvironment variable to tell the shell wh
就像其他的编程语言一样,shell也有三种基本的结构:顺序结构.分支结构.循环结构.顺序结构就是按照命令的出现顺序依次执行,比较简单.如下分别介绍分支结构和循环结构. 分支结构 格式1: if command then commands fi # 或者 if command: then commands fi 说明:在其他编程语言中,if后面的command是一个等式,用来判断是true还是false:但是在bash shell中,该command是一个命令,if语句根据command执行结果的