#--------------------------------------------------------------
#
#
#!/bin/sh
line_length=20
line_count=$1
char="*"
function usage
{
name=$(basename $0)
echo -e "usage:$name line_count_number/n"
exit 1
}
argc=$#
[[ ! $argc -eq 1 ]] && usage
length=0
count=0
>"test.txt"
file="test.txt"
while [[ $count -le $line_count ]] ;do
while [[ $length -le $line_length ]] ;do
echo -e "${char}/c">>$file
((length = length+1))
done
echo >>$file
((count = count+1))
done
#----------------------------------------------
#name:random2.sh
#note:generate a limited random number
#!/bin/sh
function check
{
argc=$#
if [[ $argc -lt 1 ]] ;then
echo -e "ERROR! usage: $0 limit_number/n"
exit
fi
}
function random
{
echo -e "the random number is :$RANDOM/n"
}
function limit_random
{
result=$(($RANDOM%$NUMBER+1))
echo -e "the limit number is :$result/n"
}
NUMBER=$1
#check
if [[ -z $1 ]] ;then
random
else
limit_random
fi
#----------------------------------
#name:read2.sh
#note:read 2 variables from a file
#!/bin/sh
while read a b ;do
if [ "$a" = "ok" ] || [ "$b" = "ok" ] ;then
echo -e " you are passed/n"
fi
done<test
#---------------------------------------------------------
#
#
#!/bin/sh
i=0
while (read data) ;do
${a[$i]}=$data
((i=i+1))
done<"test.txt"
echo -e "${ a[0]}/n"
#--------------------------------------------------
#name:recursive.sh
#note:calculate a recursive number
#!/bin/sh
if [ -z $1 ] ;then
echo -e "usage:$0 recursive_number/n"
exit 1
fi
recursive ()
{
local number=$1
if [ $number -eq 0 ] ;then
result=1
else
let "decnum=$number-1"
recursive $decnum
let "result=$number*$?"
fi
return $result
}
recursive $1
echo -e "the recusive of $1 is $result/n"
#---------------------------------------------------------
#name:search.sh
#note:return the result of searching
#!/bin/sh
PATTERN=$1
FILENAME=$2
grep "$PATTERN" $FILENAME>/dev/null
if [[ $? -eq 0 ]] ;then
echo -e "$PATTERN is found in this file/n"
else
echo -e "$PATTERN is not here/n"
fi
##########################################################
#!/bin/sh
#scriptname:select.sh
#purpose:select.....do......done
function choose
{
echo -e "choose one person/n"
select choice in peter YangTT ;do
case $choice in
peter)
echo -e "He will success/n"
break ;;
YangTT)
echo -e "I love this girl so much/n"
break ;;
*)
echo -e "please choose again/n";;
esac
done
}
choose
#####################################
#
#
#!/bin/sh
set `date`
while [[ $# -gt 0 ]]
do
echo "$1"
shift
done
######################################
#################testfile#############
#apple
#pear
#peach
#banana
#
#scriptname:speller.sh
#purpose:read a line from a file,and update the content
#!/bin/sh
exec <testfile
while read fruit ;do
echo -e "Do you like $fruit(y/n)/n"
read answer</dev/tty
case $answer in
n|N)
echo -e "OK,Type what you like/n"
read new</dev/tty
sed -e s/$fruit/$new/ testfile>tmp
mv tmp testfile
echo -e "updata success/n";;
*)
echo -e "skiping /n";;
esac
done
###############################################
#scriptname:square.sh
#purpose:calculate number's square
#!/bin/sh
function square
{
((result=$1*$1))
echo -e "the result of square $1 is $result/n"
}
echo -e "input a number/n"
read data
square $data
#------------------------------------------------
#
#
#!/bin/sh
string=$1
argc=$#
if [[ ! $argc -eq 1 ]] ;then
echo -e "usage:$0 test_string/n"
fi
case $gtring in
+([0-9]))
echo -e "it is number/n"
;;
+([a-zA-Z]))
echo -e "it is character/n"
;;
*)
echo -e "unknown/n"
;;
esac
##############################################################
#scriptname:trap2.sh
#purpose:using trap function
#!/bin/sh
trap 'echo -e "ctrl+c will not terminate this programme/n"' 2
echo -e "type /"stop/" to exit/n"
while true
do
echo -n "switch->"
read data
if [ $data = "stop" ] ;then
break
fi
done
#------------------------------------------
#name:trap.sh
#note:ctrl+c signal 2,when hit ctrl+c ,execute function hi and exit
#!/bin/sh
function hi
{
echo -e "hi,all!!/n"
}
trap 'hi ;exit 1' 2
read hi
echo $hi
#-------------------------------------------
#name:update.sh
#note:creating a specified file
#!/bin/sh
clear
echo -e "/nDo you want to create a file?(Y|N)/n"
read answer
case $answer in
Y|y) ;;
*) echo -e "/n all right ,operation is cancle!/n";;
esac
cat /dev/null >test
for input in wuhan danian shanghai beijing ;do
echo $input >>test
done
#--------------------------------------------------
#name:print_pic
#note:print a figure
#!/bin/sh
a=0
while true ;do
a=$(expr $a + 1) ;
case $a in
1) echo -e "+/c" ;;
2) echo -e "*/c" ;;
3) echo -e "*/c" ;;
4) echo -e "*/c" ;;
5) echo -e "*/c" ;;
6) echo -e "*/c" ;;
7) echo -e "*/c" ;;
8) echo -e "*/c" ;;
9) echo -e "*/c" ;;
10) echo -e "+/n" ;;
*) echo "finish!"
exit 0 ;;
esac
sleep 1
done