qcd_install文件解析

来源:互联网 发布:淘宝交易有限额怎么办 编辑:程序博客网 时间:2024/05/09 22:04

#!/bin/sh
#qcd install 
#written by xiewei 2004

setup_content=/etc/qcd
history_dir=$setup_content/history_dir
bin_content=/usr/local/sbin
prof_content=/etc/profile.d
#上面是定义的一些程序中要使用到的变量。

setup()  #安装程序函数
{
    #check 检测文件是否存在
    if [ -r $history_dir ]  #判断文件是否可读。
    then
        echo -n "You have installed qcd , overwrite it(y\Y or q\Q or n\N)? "
 #上一行echo后面的-n表示打印后面的字符串后不换行。
        while read choice  #读取用户按下的键值。
        do
            if [ "$choice" = "y"  -o  "$choice" = "Y" ]  
            then
                break
            fi  
#在用户选择了y/Y后,程序就会退出while循环,执行后面的程序代码。
            if [ "$choice" = "q"  -o  "$choice" = "Q" ]  
            then
                echo "Nothing to do!"
                exit 1
            fi
               
            if [ "$choice" = "n"  -o  "$choice" = "N" ]  
            then
                cp -f ./qcd $bin_content/
                cp -f ./qcd.sh $prof_content/
                echo "install qcd OK, but do not overwrite it!"
                echo "version is 1.00"
                exit 1
            fi
#注意:在用户选择n/N时,该安装程序只是将qcd和qcd.sh的文件覆盖掉了原来的文件
#而没有覆盖掉用户使用该程序时的设置目录(即没有执行 cp -f ./history_dir $setu
#p_content/代码),然后退出该函数。
            echo -n "You have installed qcd, overwrite it(y\Y or q\Q or n\N)? "
#上面一行回显代码的作用是:当进入while循环后没有按下指定的按键时,
#让程序继续显示提示内容提示操作者。 
        done
    fi
    
    if [ -r $setup_content ] #判断这个安装目录是否存在,不存在就创建。 
    then
        :
    else
        mkdir $setup_content
    fi
#下面的代码是在用户按下y/Y时,执行的代码。其中将用户的历史设置
#用空文件(history_dir)取代了。
    cp -f ./qcd $bin_content/
    cp -f ./history_dir $setup_content/
    cp -f ./qcd.sh $prof_content/
}

delete()  #卸载程序函数
{
    [ -r $history_dir ] || \
    [ -r $bin_content/qcd ] || \
    [ -r $prof_content/qcd.sh ] || \
! echo "Your computer has not qcd!" || ! echo "Nothing to do!" || exit 1
#上面代码首先检测history_dir文件是否存在。如果history_dir文件不存在,
#然后才判断qcd目录是否存在。如果qcd目录不存在,然后才判断qcd.sh文件是否存在,
#如果qcd.sh文件都不存在,说明该程序没有安装而且没有残余项,所以就不用执行
#卸载程序了,直接退出这个卸载程序即可。
#在echo之前的!是表明对两个||之间的命令进行取非(为假)那么||后的命令才能得到执行。
#注意不可以改写成下面这样:
#echo "Your computer has not qcd!" && echo "Nothing to do" && exit 1
    echo -n "Are you sure to delete qcd(y\Y or q\Q)? "
    while read choice
    do
        if [ "$choice" = "y"  -o  "$choice" = "Y" ]  
        then
            break
        fi

        if [ "$choice" = "q"  -o  "$choice" = "Q" ]  
        then
            echo "Nothing to do!"
            exit 1
        fi

        echo -n "Are you sure to delete qcd(y\Y or q\Q)? "
    done

    rm -rf $setup_content
    rm -f  $bin_content/qcd
    rm -f  $prof_content/qcd.sh
}

usage()   #用途函数用于提示操作者正确的输入方式。
{
    echo "<install> install qcd on your computer."
    echo "<install del> delete qcd from your computer."
    exit 1
}

echo "Qcd Install Software"
echo "Written By XieWei 2004"

if [ $# -eq 0 ] #特殊环境变量:$#表示命令行参数的总数,为0就是没有参数,那么就
then            #执行安装程序函数。
    setup       #调用安装函数
    echo "install qcd OK!"
    echo "version is 1.00"
    exit 1
fi   

if [ $# -gt 1 ] #参数个数大于1不符合规则,那么就执行用途函数。
then
    usage       #调用用途函数
fi   

case $1 in   #对第一命令行参数进行判断,如果为del,那么就执行卸载程序的函数。
del)         #不然就继续执行用途函数提示操作者正确的输入命令方式。
    delete   #调用卸载函数
    echo "have delete qcd OK!"
    ;;
*)
    echo "Input command incorrect!"
    echo "Please follow:"
    usage    #调用用途函数
    ;;
esac


很久前的东西,转移到csdn上,上面注释有些不对,抱歉。

0 0
原创粉丝点击