shell小程序每行加下注释
发表于 : 2012-01-04 15:27
刚大三,初学Linux,希望各位大大为下面两段程序每行加一下注释,方便看懂,谢谢。
第一个是一个函数,可以计算任意个正整数的和,如果输入错误就返回-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
#
# 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
第一个是一个函数,可以计算任意个正整数的和,如果输入错误就返回-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
#
# 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