现在的位置: 首页 > 综合 > 正文

linux-shell:shell脚本编程

2013年12月01日 ⁄ 综合 ⁄ 共 2048字 ⁄ 字号 评论关闭

 bash -v sum.sh表示将代码执行之前,先将代码显示出来

[gexing111@gexing111 myapps]$ bash -v sum.sh

#!/bin/bash
let a=30
let b=20
let sum="$a + $b"
if test $a -lt $b
 then
   echo "OK"
 else
   echo "NO"
fi

NO

 bash -v sum.sh表示将代码执行过程中显示出来

[gexing111@gexing111 myapps]$ bash -x sum.sh
+ let a=30
+ let b=20
+ let 'sum=30 + 20'
+ test 30 -lt 20
+ echo NO

NO

程序1:Hello.sh

#!/bin/bash
clear
echo "Welcome to my first Linux Program!"
echo "Enter your name :"
read response
echo "Hello,$response!"

程序2:readEmployees.sh

#!/bin/bash
clear
echo "Plz input your Name"
read Name
echo "Plz input your Age"
read Age
echo "Name:$Name Age:$Age">>mydata.dat

echo ""
echo "Employees:"
echo ""
cat mydata.dat

程序3:sum.sh

#!/bin/bash
let a=30
let b=20
let sum="$a + $b"
if test $a -lt $b
 then
   echo "OK"
 else
   echo "NO"
fi

程序4:mymenu.sh

#!/bin/bash
declare flag="1"
while [ $flag -eq "1" ]
do
        echo "The Telephone Book"
        echo ""
        echo "1.Display A Telephone Number"
        echo "2.Add A New Telephone NUmber"
        echo ""
        echo "Q Quit"
        echo ""
        echo "Enter your selection:"
        read selection

        case $selection in
        "1")
                echo "You want to display a telephone number."
                getnum
                ;;
        "2")
                echo "You want to add a new telephone number."
                addnum
                ;;
        "q")
                flag="0"
                ;;
        "Q")
                flag="0"
                ;;
        *)
                echo "You made an invalid selection."
        esac
done

5,程序friends.sh

#!/bin/bash
for friend in "Mary Jones" "Joe Smith" "Sue Jones"
do
        echo "Hello,$friend"
done

6,程序raining.sh

#!/bin/bash
declare raining="1"
while [ $raining -eq "1" ]
do
        clear
        echo ""
        echo "Is it raining?"
        echo ""
        echo "1,YES"
        echo "2,NO"
        echo ""
        echo "Enter your selection:"
        read raining
done

echo "It stopped raining."

7,程序blink.sh

#!/bin/bash
clear
declare count1=1
declare count2

while [ $count1 -lt 6 ]
do
        echo "Warning:There is a bug in your program!"
        let count2=1
        while [ $count2 -lt 20000 ]
        do
                let count2="$count2 + 1"
        done
        clear
        let count2=1
        while [ $count2 -lt 20000 ]
        do
                let count2="$count2 + 1"
        done

        let count1="$count1 + 1"
done


8,程序break.sh

#!/bin/bash
let n=1
while [ $n -eq 1 ]
do
        echo "Enter your name or \"stop\" to end:"
        read name
        case $name in
        "stop")
                echo "Bye!"
                break
                ;;
        *)
                echo "Hi,$name!"
                ;;
        esac
done

9,程序function_dis.sh

#!/bin/bash
clear
function display
{
        echo "Welcome to the world"
        echo "of functions."
}

display


10,程序function_verify.sh

#!/bin/bash
clear
function verify
{
        if [ $# -ne 2 ]
        then
                echo "Wrong number of arguments!"
        #字符串的比较用=  而数字的比较用-eq
        elif [ $1 = "gexing111" ] && [ $2 = "111gexing" ]
        then
                echo "Verified"
        else
                echo "Rejected"
        fi
}
verify $1 $2

抱歉!评论已关闭.