if分支
#! /bin/bash# take exercise "if"x=6if [ $x = 5 ];then echo "x equals 5"elif [ $x = 6 ];then echo "x equals 6"else echo "x does not equal 5"fi
判断一个输入是奇数还是偶数,并对输入进行验证
#! /bin/bash#check your input is negative or positive and check it is even or oddwhile :do echo -n "please input a number: " read x if [[ "$x" =~ ^-?[0-9]+$ ]]; then #使用正则表达式验证输入x是否有效 echo "input checked..." else echo "your input is wrong!!">&2 exit 1 fi echo "the number you input is $x" if [ -z "$x" ]; then #验证x是否为空 echo "nothing is input!!">&2 exit 1 fi if [ $x -eq 0 ]; then echo "your input is 0!" else if [ $x -lt 0 ]; then echo "your input is negative!" else echo "your input is positive!" fi if [ $((x%2)) -eq 0 ]; then echo "your input is even" else echo "your input is odd" fi fidone
报告计算机的设备使用情况
#! /bin/bash#program to output a system information pageTITLE="system information report" #定义常量CURRENT_TIME=$(date) #调用date命令,并赋给CURRENT_TIMETIME_STAMP="Generated at $CURRENT_TIME , By $USER"report_uptime(){ #函数定义,如果在函数内定义局部变量的话可以使用local关键字 cat<<- _EOF_ #表明从_EOF_到下一个_EOF_都将使用cat命令进行输出,这样就可以省略每行都写的echoSystem uptime
$(uptime)#调用uptime命令 _EOF_ return}report_disk_space(){ cat<<- _EOF_disk Space Utilization and use station
$(df -h)_EOF_ return}report_home_space(){ cat<<- _EOF_Home Space Utilization and use station
$(du -sh)_EOF_ return}cat <<_EOF_$TITLE $TITLE
$TIME_STAMP
$(report_uptime) $(report_disk_space) $(report_home_space) _EOF_
然后可以通过firfox进行调用
sysinfo>sysinfo.html
firefox sysinfo.html