linux shell腳本進階

#!/bin/bash
#largest_test
#
echo
echo -e  "please input three numbers and I will show you the largest of them:\c"
read	 N1  N2  N3
echo 	$N1 $N2  $N3
if  test “$N1” -ge “$N2”
then
    if  test “$N1” -ge  “$N3”
    then
        echo "the largest is: $N1"
    else
        echo "the largest is: $N3"
    fi
elif  test “$N1” -ge “$N3”
then
    echo "the largest is: $N2"
elif  test “$N2” -ge “$N3”
then
    echo "the largest is: $N2"
else
    echo "the largest is: $N3"
fi




# largest_expr
echo  -e    "please input three numbers and I will show you the largest of them:\c"
read N1 N2 N3
echo $N1 $N2 $N3
if	expr	${N1}	  \>=	  ${N2}
then
    if	expr ${N1}	 \>=	   ${N3}
    then
        echo "the largest is: $N1"
    else
        echo "the largest is: $N3"
    fi
elif 	expr	 ${N1} 	\>=   	${N3}
then
    echo "the largest is: $N2"
elif 	expr	 ${N2} 	\>=	  ${N3}
then
    echo "the largest is: $N2"
else
    echo "the largest is: $N3"
fi


#!/bin/bash
#
echo
DATE1=`date`
sleep 1
DATE2=`date`
if   test   "$DATE1" 	 =  "$DATE2"
then
    echo "my god ! The computer clock is dead"
else
    echo "Everything is fine"
fi
echo



# !/bin/bash
# backup  a file and invoke vi to edit this file
# adding if-then-else statement
#
echo  -e   "input a file name in the current directory:\c"
read 	 FILE
if 	[	 -n 	"$FILE"		 ]
then
    cp 	$FILE  	${FILE}_bak
    vi  $FILE
else
    echo "you must input a filename,try again"
fi
echo


#!/bin/bash
# 
# backup  a file and invoke vi to edit this file
# adding if-then statement
echo
echo 	-e 	"input a file name using absolute directory:\c"
read 	FILE
if	   [ 	"$FILE" 		!= 	"" 		]
then
    cp 	$FILE 	 	${FILE}_bak
    vi 	$FILE
fi



#!/bin/bash
#using the test command
#
echo
echo "please input three numbers and I will show you the largest of them:\c"
read N1 N2 N3
echo $N1 $N2 $N3
if  test  ${N1}  -ge  ${N2}   -a  ${N1}  -ge  ${N3}
then
    echo "the largest is: $N1"
elif  test  ${N2}  -ge ${N1} -a  ${N2}  -ge  ${N3}
then
    echo "the largest is: $N2"
else
    echo "the largest is: $N3"
fi


#!/bin/bash
#
#	script_testfor
#
for count in 1 2 3 8
do
		echo "in the loop for  "$count"  times"
done


#!/bin/bash
#
# script_testwhile
#
echo
STAT=y
count=0
while    [    $STAT   =   y   ]
do
    count=`expr   $count    +   1`
    echo -e  "I will get ¥1  as long as you type y:\c"
    read STAT
done
echo “I will  get  ¥`expr $count  -  1`  , thanks "




#!/bin/bash
#
# script_testwhile2  
#
echo
STAT=y
count=0
while    [    $STAT   =   y   ]
do
    let  count=count+1
    echo -e "I will get ¥1 as long as you type y:\c"
    read STAT
done
let count=count-1
echo " I get ¥${count} , thanks "



#!/bin/bash
#
# script_testcase1
#
echo
HOUR=`date  +%H`
case   $HOUR  in
    0?|1[0-1])    echo "Good morning";;
    1[2-7])         echo "Good afternoon";;
    *)                 echo "Good evening";;
esac



#!/bin/bash
#
# script_testcase1
#
echo
HOUR=`date  +%H`
case   $HOUR  in
    0?|1[0-1])    echo "Good morning";;
    1[2-7])         echo "Good afternoon";;
    *)                 echo "Good evening";;
esac



# testcase
#
echo
echo	"0:  Exit"
echo 	"1:  Show Date and time"
echo 	"2:  List your home directory"
echo 	"3:  Display calendar"
echo 	-e	  "Enter your choice option:_\b\c"
read 	option
case 	$option 	in
    0) echo	"good bye ";;
    1) date;;
    2) echo	"$HOME";;
    3) cal;;
    *) echo	"invalid input try again";;
esac


···
發佈了19 篇原創文章 · 獲贊 3 · 訪問量 2200
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章