脚本编程:
顺序结构 选择结构 if case 循环结构 for while until while循环:适用于循环次数未知的场景,要有退出条件语法: while CONDITION; do statement ... done 计算100以内所有正整数的和#!/bin/bashdeclare -i I=1declare -i SUM=0while [ $I -le 100 ]; do let SUM+=$I let I++doneecho $SUM练习:转换用户输入的字符为大写,除了quit:#!/bin/bash#read -p "Input something: " STRINGwhile [ $STRING != 'quit' ]; do echo $STRING | tr 'a-z' 'A-Z' read -p "Input something: " STRINGdone练习:每隔5秒查看hadoop用户是否登录,如果登录,显示其登录并退出;否则,显示当前时间,并说明hadoop尚未登录:#!/bin/bash#who | grep "hadoop" &> /dev/nullRETVAL=$?while [ $RETVAL -ne 0 ]; do echo "`date`, hadoop is not log." sleep 5 who | grep "hadoop" &> /dev/null RETVAL=$?doneecho "hadoop is logged in."写一个脚本:1) 显示一个菜单给用户:d|D) show disk usages.m|M) show memory usages.s|S) show swap usages.*) quit.2) 当用户给定选项后显示相应的内容; 扩展: 当用户选择完成,显示相应信息后,不退出;而让用户再一次选择,再次显示相应内容;除了用户使用quit;#!/bin/bash#cat << EOFd|D) show disk usages.m|M) show memory usages.s|S) show swap usages.*) quit.EOFread -p "Your choice: " CHOICEwhile [ $CHOICE != 'quit' ];do case $CHOICE in d|D) echo "Disk usage: " df -Ph ;; m|M) echo "Memory usage: " free -m | grep "Mem" ;; s|S) echo "Swap usage: " free -m | grep "Swap" ;; *) echo "Unknown.." ;; esacread -p "Again, your choice: " CHOICEdone
练习:写一个脚本
从键盘让用户输入几个文件,脚本能够将此几个文件归档压缩成一个文件;read: -p “PROMPT": 给出提示