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

note of SHELL (二)

2014年01月12日 ⁄ 综合 ⁄ 共 4075字 ⁄ 字号 评论关闭

#------------------------------------------------------------
#name:call_file.sh
#note:call shell file
#!/bin/sh
if  [[ -f "test.sh" ]] ;then
        sh countdown.sh 5
        else
    echo -e "file is no exists/n"
        fi

#########################################
################ tmp ####################
#pear
#banana
#apple
#scriptname:chang_name
#purpose:chang file content which you do not like
#!/bin/sh
while read data
do
        echo -e "$data/n"
        echo -e "do you like this fruit(y/n)/n"
        read answer</dev/tty
         case $answer in
        Y|y)
            echo -e "ok ,skip this/n"
        #    exit 0
            ;;
        *)
            echo -e "input your favorite fruit/n"
            read i</dev/tty
            sed -e "s/$data/$i/g" tmp>tmp2
            mv tmp2 tmp
        esac
done<tmp

#----------------------------------------------
#name:check_online.sh
#note:test if eth0 connected
#!/bin/sh

if [ !  $UID -eq 0 ] ;then
        echo -e "Change to root account please!!!/n"
        exit 1
        fi
while [ 1 ] ; do
        ifconfig eth0 |grep up>/dev/null && echo -e "It is connected /n" && exit
        echo "You are log out/n"
        sleep 3
done

##################################################
#script:color.sh
#purpose:using regular expressions
#!/bin/sh
more $0
echo -e "choose a color from red blue green/n";
read color
case $color in
[bB]l*)
echo -e "the sky is $color/n";;
[gG]reen)
echo -e "the tree is $color/n";;
[rR]ed)
echo -e "$color is warm color/n";;
*)
echo -e "input error/n";;
esac

 

#-----------------------------------------------------
#
#
#!/bin/sh
declare -a name
name=(`cat "test.txt"`)
echo -e "the array /@name is ${name[*]}/n"
echo -e "this array has " ${#name[*]} "elements/n"

 

###############################################
#scriptname:backup.sh
#purpose:backup file to the specified directory
#!/bin/sh
for file in 1 2 3 4 ;do
        if [[ ! -f $file ]];then
                echo -e "$file is not exists!!!/n"
                fi
                cp $file old2
                echo -e "$file is backup success!!!/n"
    done

 

#######################################
#scriptname:cal.sh
#purpose:print a few numbers
#!/bin/sh
i=1
while [[ $i -le 10 ]] ;do
        echo -e "$i/n"
        ((i=i+1))
done

 

################################################
#scriptname:calculator.sh
#purpose:calculate two numbers ,this script
#        supports float point
#!/bin/sh

cat<<logo
***********************************************

  welcome to calculator programme

***********************************************
logo

echo -e "input the first number/n"
read num1

echo -e "input the operator/n"
select symbol in + - /* / ;do
case $symbol in
+)
ope="+"
echo -e "you choose +/n"
break;;
-)
ope="-"
echo -e "you choose -/n"
break;;
'*')
ope="*"
echo -e "you choose */n"
break;;
'/')
ope="/"
echo -e "you choose //n"
break;;
esac
done

echo -e "input the second number/n"
read num2

#((result=num1"$ope"num2))
result=$(echo $num1"$ope"$num2|bc )

echo -e  "the result is $result/n"

 

#-----------------------------------------------------
#script:find.sh
#purpose:find the specified file in specified path,list
#        the result
#!/bin/sh
path=$1
file=$2
argc=$#
if [[ ! argc -eq 2 ]] ;then
        echo "usage:$0 path_to_search file_to_search /n"
        exit 1
fi

find $path -name "$file" -print >test

if [[ -s test ]] ; then
   number=`cat test|wc -l`
   more test
    echo -e "find $number files/n";
else
        echo -e "this file is not exist/n" ;
fi

 

#----------------------------------------------------
#name:function.sh
#note:creating function in shell
#!/bin/sh
start()
{
        echo "--------------------------------"
        echo "game"
        echo "is "
        echo "start"
}

for ((a=0;a<5;a++ ));do
        start
        done
        exit 0

 

############################################
#
#
#!/bin/sh
while getopts qwe option
do
        if [ "$option" = q ] ;then
                echo -e "you type -q/n"
        elif [ "$option" = w ] ;then
                echo -e "you type -w/n"
        elif [ "$option" = e ] ;then
                echo -e "you type -e/n"
        elif [ "$option" = /? ] ;then
                echo -e "you do not type option/n" 1>&2
        fi
done

#-----------------------------------------------------
#script:getopts.sh
#purpose:using getopts function
#!/bin/sh
while getopts abc:f opt    #colon dose not take effect here
do
    case "$opt" in
        a) echo -e "option a input/n";;
        b)
        echo -e "option b input/n"
        ;;
        c)
        echo -e "option c input/n"
        ;;
        f)
        echo -e "option f input/n";;
        *)
        echo -e "error: invalid option/n"
        ;;

    esac
done

抱歉!评论已关闭.