#每日Linux小练习#07 Shell Script知识点总结(中)

(主要内容来源于《鸟哥的Linux私房菜》)

目录

if ...then

if..elif...then

case...esac

function

 

 

【条件判断式】

if ...then

if..elif...then

if [ "$1" != "choice" -a "$1" != "detect" -a "$1" != "date" ];then
    echo "Please input 'choice' or 'detect' or 'date' ex> {$0 choice}"
fi

if [ "$1" == "choice" ];then
    read -p "Please input your choice (y / n): " yn

    if [ "$yn" == "Y" ] || [ "$yn" == "y" ];then
        echo "You input Y"
    elif [ "$yn" == "N" -o "$yn" == "n" ];then
        echo "You input N"
    else
        echo "Please input y or n"
    fi

fi

if [ "$1" == "detect" ];then

    echo "Now I will detect your Linux server's service"
    echo -e "The www,ftp,ssh,adn mail will be detect"

    testing=$( netstat -tuln | grep ":80")
    if [  "$testing" != "" ];then
        echo "WWW is running in your system"
    fi

    testing=$( netstat -tuln | grep ":22")
    if [  "$testing" != "" ];then
        echo "SSH is running in your system"
    fi

    testing=$( netstat -tuln | grep ":21")
    if [  "$testing" != "" ];then
        echo "FTP is running in your system"
    fi

    testing=$( netstat -tuln | grep ":25")
    if [  "$testing" != "" ];then
        echo "Mail is running in your system"
    fi
fi


if [ "$1" == "date" ];then
    read -p "Please input date(YYYYMMDD ex>20180410):" date2 
    date_check=$(echo $date2 | grep '[0-9]\{8\}')
    if [ "$date_check" == "" ];then
        echo "You input wrong date format"
        exit 1
    fi

    declare -i date_dem=`date --date="$date2" +%s`
    declare -i date_now=`date +%s`
    declare -i date_total=$(($date_dem -$date_now ))
    declare -i date_d=$(($date_total/60/60/24))
    if [ "$date_total" -lt "0" ];then
        echo "The day had passed before: " $((-1 * $date_d)) "days ago"
    else
        echo "another" $date_d "days left"
    fi

fi

 

1、ssh服务是什么?

传统的网络服务程序,如:ftp、pop和telnet在本质上都是不安全的,因为它们在网络上用明文传送口令和数据,别有用心的人非常容易就可以截获这些口令和数据。而且,这些服务程序的安全验证方式也是有其弱点的,就是很容易受到“中间人”(man-in-the-middle)这种方式的攻击。所谓“中间人”的攻击方式,就是“中间人”冒充真正的服务器接收你的传给服务器的数据,然后再冒充你把数据传给真正的服务器。服务器和你之间的数据传送被“中间人”一转手做了手脚之后,就会出现很严重的问题。 

SSH的英文全称是Secure SHell。
通过使用SSH,你可以把所有传输的数据进行加密,这样“中间人”这种攻击方式就不可能实现了,而且也能够防止DNSIP欺骗。
还有一个额外的好处就是传输的数据是经过压缩的,所以可以加快传输的速度。SSH有很多功能,它既可以代替telnet,又可以为ftp、pop、甚至ppp提供一个安全的“通道”。

2、declare/typeset 选项

-i 整数

   1 declare -i number
   2 # 脚本余下的部分会把"number"当作整数看待.		
   3 
   4 number=3
   5 echo "Number = $number"     # Number = 3
   6 
   7 number=three
   8 echo "Number = $number"     # Number = 0
   9 # 脚本尝试把字符串"three"作为整数来求值(译者注:当然会失败,所以出现值为0).

 

case...esac

case $1 in
    "choice")
        echo "You input 'choice'"
        ;;
    "detect")
        echo "You input 'detect'"
        ;;
    "date")
        echo "You input 'date'"
        ;;
    "")
        echo "Please input 'choice' or 'detect' or 'date' ex> {$0 choice}"
        ;;
esac    

 

function

function printit(){
    echo "You input" $1  #这个$1是指函数printit的第一个输入参数
}

case $1 in
    "choice")
        printit $1
        ;;
    "detect")
        printit $1
        ;;
    "date")
        printit $1
        ;;
    "")
        echo "Please input 'choice' or 'detect' or 'date' ex> {$0 choice}"
        ;;
esac

 

posted on 2015-08-10 11:06  依风152  阅读(307)  评论(0编辑  收藏  举报