Shell编程学习,两个小程序

Post by zerob13

shell编程真不好写阿。。。脚本语言都是有那么一些麻烦的特性在。。。
最近做实验的时候写了两个学习性的小程序,还不错感觉有点感觉了~哈哈
第一个是一个函数,可以计算任意个正整数的和,如果输入错误就返回-1

!/bin/bash # mysum sum=0; function mysum(){ while [ $# -ne 0 ] do if [ echo $1 | grep "^[0-9][0-9]*$" ] then sum=$(expr $sum + $1) shift else sum=-1 break fi done echo $sum }

然后是调用这个函数的测试脚本

!/bin/bash . ./mysum mysum $@

然后是一个简单的通讯录程序,我想实现的比较好的,可惜技术不够。。。最后做的比较挫。。。大家凑合看吧。。

!/bin/bash # # MENU # function find() { echo "input Name:" read name cat data.txt |grep "^$name" read a } function add() { echo "input Name:" read name echo "input Number:" read number te="$name $number" echo $te echo $te >>data.txt read a } function Edit() { echo "input Old Name:" read nameo echo "input New Name:" read namen echo "input Old Number:" read numbero echo "input New Number:" read numbern teo="$nameo $numbero" ten="$namen $numbern" sed '/$teo/d' data.txt echo $ten >>data.txt } if [ -f data.txt ] then echo else "">>data.txt fi while ls > /dev/null 2>&1 do clear echo echo echo echo echo " 1: Show Date and Time" echo " 2: Find Phone Number" echo " 3: Add Phone Number Record" echo " 4: Edit Phone Number Record" echo " 5: Exit" echo -e "Enter your choice: \c" read option case $option in 1) date read $a;; 2) echo "Finding..."; find;; 3) echo "Add...";add ;; 4) echo "Editing...";Edit ;; 5) echo "Bye...";break ;; *) echo "Invalid input";; esac echo done