练习:求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] -eq 0 ]; then
sum=$[$sum+$i]
fi
done echo "Even sum: $sum." 增强型赋值:
+=
sum=$[$sum+$i]
let sum+=$i
-=,*=,/=,%=, let count=$[$conunt+1] -->let count+=1-->let count++
let count=$[$conunt-1] -->let count-=1-->let count-- 示例:显示/etc目录下所有的每个普通文件,而后统计一共有多少个这样的文件; #!/bin/bash
#
declare -i count=0 for file in /etc/*; do
if [ -f $file ]; then
let count++
echo "$count $file"
fi
done echo "Total: $count files." 测试表达式:
整数测试;-gt,-lt,-ge,-le,-eq,-ne
字符串测试:==,>,<,!=,-z,-n,=~ 注意:
(1)字符串等会比较测试[ "$hostname" == 'localhost' ]
(2)模式匹配测试:[[ "STRING" -~ PATTERN ]] 示例:写一个脚本实现如下功能;
获取当前主机的主机名:
如果当前主机的主机名为空,或者为localhost,则将其修改为www.mageedu.com 组合测试条件:
条件间逻辑运算:
与:多个条件要同时满足;
或:多个条件满足其一即可;
非:对指定的条件取反; 表达式组合:
与:[[ CONDITION -a CONDITION2 ]]
或:[[ CONDITION -o CONDITION2 ]]
非:[ !CONDITION ] 命令组合:
与:COMMAND1 && COMMAND2 <-- [ EXPR1 ] && [ EXPR2 ]
或:COMMAND1 || COMMAND2
非:!COMMAND false && true = false
false && false = false true && false = true
true && true = true if COMMAND; then
COMMAND2
fi
短路操作符:
true || true = true
true || false = true false || true = true
false || false = false if ! COMMAND1; then
COMMAND2
fi COMMAND1 && COMMAND2 || COMMAND3
if COMMAND1;then
COMMAND2
else
COMMAND3
fi 获取当前主机的主机名:
如果当前主机的主机名为空,或者为localhost,则将其修改为www.mageedu.com #!/bin/bash
#
hostname=$(hostname) if [ -z "$hostname" -o "$hostname"=="localhost" ]; then
hostname www.magedu.com
fi 练习:写一个脚本
(1)传递两个文本文件路径给脚本;
(2)显示两个文件中空白行数较多的文件及其空白行的个数;
(3)显示两个文件中总行数较多的文件及其总行数;
练习:写一个脚本
(1)提示用户输入一个字符串;
(2)判断:
如果输入的是quit,则退出脚本;
否则,则显示其输入的字符串内容; 练习:写一个脚本,打印九九乘法表:
循环嵌套 #!/bin/bash
#
for j in {1..9}; do
for i in $(seq 1 $j); do
echo -n -e "${i}X${j}=$[$i*$j]\t"
done
echo
done 多分支的if语句:
单分支:
if CONDITION; then
if-true-分支
fi
双分支:
if CONDITION; then
if-true-分支
else
if-false-分支
fi 多分支:
if CONDITION1; then
if -CONDITION-true-分支
elif CONDITION2; then
if-CONDITION2-true-分支
...
else
if-ALL-false-分支
fi 示例:通过脚本参数传递一个文件路径给脚本,判断其类型: #!/bin/bash
#
if [ $# -lt 1 ]; then
echo "Usage: $0 <path>"
exit 1
fi if [ -f $1 ]; then
echo "Rgulat file."
elif [ -d $1 ]; then
echo "Directory."
elif [ -h $1 ]; then
echo "Symbolic link."
elif [ -b $1 ]; then
echo "Block special."
elif [ -c $1 ]; then
echo "Charactoer special."
elif [ -S $1 ]; then
echo "Socket file."
else
echo "file not exist or unknow type."
fi 示例:脚本可接受四个参数
start:创建文件/var/lock/subsys/SCRIPT_NAME
stop:删除此文件
restatr:删除此文件并重新创建
status:如果文件存在,显示为“running”;否则,显示为“stopped” basename命令:
取得路径的基名:
示例: #!/bin/bash
#
prog=$(basename $0)
lockfile="/var/lock/subsys/$prog"
#echo $lockfile if [ $# -lt 1 ]; then
echo "Usage: $prog start|stop|restart|status"
exit 1
fi if [ "$1" == "start" ]; then
if [ -f $lockfile ]; then
echo "$prog is started yes."
else
touch $lockfile && echo "Starting $prog ok..." || echo "Starting $prog failed.."
fi
elif [ "$1" == "stop" ]; then
if [ -f $lockfile ]; then
rm -f $lockfile && echo "Stop $prog ok..." || echo "Stop $prog failed.."
else
echo "$prog is stopped yet."
fi
elif [ "$1" == "restart" ]; then
if [ -f $lockfile ]; then
rm -f $lockfile && touch $lockfile && echo "Restart $prog ok..."
else
touch $lockfile && echo "$prog is stopped, Starting $prog ok..."
fi
elif [ "$1" == "status" ]; then
if [ -f $lockfile ]; then
echo "Running..."
else
echo "Stopped..."
fi
else
echo "Usage: $prog start|stop|restart|status"
exit 1
fi case语句:
简洁版多分支if语句:
使用场景:判断某变量的值是否为多种情形中的一种时使用; 语法:
case $VARIABLE in
PATTERN1)
分支1
;;
PATTERN2)
分支2
;;
PATTERN3
分支3
;;
...
*)
分支n
;;
esac PATTERN可使用glob模式的通配符:
*:任意长度的任意字符;
?:任意单个字符;
[]:指定范围内的任意单个字符;
a|b:多选1; 示例:提示键入任意一个字符,判断类型:
脚本如下:
#!/bin/bash
#
read -p "Plz enter a character: " char case $char in
[a-z])
echo "A character."
;;
[0-9])
echo "A digit."
;;
*)
echo "A special character."
;;
esac 示例:脚本可接受四个参数
start:创建文件/var/lock/subsys/SCRIPT_NAME
stop:删除此文件
restatr:删除此文件并重新创建
status:如果文件存在,显示为“running”;否则,显示为“stopped” 脚本如下: #!/bin/bash
#
prog=$(basename $0)
lockfile="/var/lock/subsys/$prog"
#echo $lockfile if [ $# -lt 1 ]; then
echo "Usage: $prog start|stop|restart|status"
exit 1
fi
case $1 in
start)
if [ -f $lockfile ]; then
echo "$prog is started yes."
else
touch $lockfile && echo "Starting $prog ok..." || echo "Starting $prog failed.."
fi
;;
stop)
if [ -f $lockfile ]; then
rm -f $lockfile && echo "Stop $prog ok..." || echo "Stop $prog failed.."
else
echo "$prog is stopped yet."
fi
;;
restart)
if [ -f $lockfile ]; then
rm -f $lockfile && touch $lockfile && echo "Restart $prog ok..."
else
touch $lockfile && echo "$prog is stopped, Starting $prog ok..."
fi
;;
status)
if [ -f $lockfile ]; then
echo "Running..."
else
echo "Stopped..."
fi
;;
*)
echo "Usage: $prog start|stop|restart|status"
exit 1
esac