鳥哥Linux私房菜之shell script习题

  • 請建立一支 script ,當你執行該 script 的時候,該 script 可以顯示: 1. 你目前的身份 (用 whoami ) 2. 你目前所在的目錄 (用 pwd)
    #!/bin/bash
    #输出 当前用户名 & 当前所在目录
    #date:  2011-03-10
    #author:       nodot [鳥哥的Linux私房菜-基础学习篇]
    who=`whoami`
    echo "当前用户的身份是:$who"
    pd=`pwd`
    echo "您所在的目录:$pd"
  • 請自行建立一支程式,該程式可以用來計算『你還有幾天可以過生日』啊?
    #!/bin/bash
    read -p "Pleas input your birthday (MMDD, ex> 0709): " bir
    now=`date +%m%d`
    if [ "$bir" == "$now" ]; then
    echo "Happy Birthday to you!!!"
    elif [ "$bir" -gt "$now" ]; then
    year=`date +%Y`
    total_d=$(($((`date --date="$year$bir" +%s`-`date +%s`))/60/60/24))
    echo "Your birthday will be $total_d later"
    else
    year=$((`date +%Y`+1))
    total_d=$(($((`date --date="$year$bir" +%s`-`date +%s`))/60/60/24))
    echo "Your birthday will be $total_d later"
    fi
  • 讓使用者輸入一個數字,程式可以由 1+2+3... 一直累加到使用者輸入的數字為止。
    #!/bin/bash
    #输入一个值,输出从1累加至该值的结果
    #date:          2011-03-10
    #author:        nodot
    echo "Please input an integer  number"
    read num
    declare -i i
    declare -i s
    for (( i=0; i<="$num"; i=i+1 ))
    do
            s=s+i
    done
    echo "从1加到 $num 的总和为: $s"

  • 我們知道 /etc/passwd 裡面以 : 來分隔,第一欄為帳號名稱。請寫一隻程式,可以將 /etc/passwd 的第一欄取出,而且每一欄都以一行字串『The 1 account is "root" 』來顯示,那個 1 表示行數。
    #!/bin/bash
    #Using for and loop to read the account of this linux server
    #notod@2011/03/10
    account=`cut -d ":" -f1 /etc/passwd|sort`
    echo "the follow is your linux server's account"

    for account  in $account
    do
            declare -i i=$i+1
            echo "The $i  account is \"$account"\"
    done
posted @ 2011-03-10 10:57  nodot  阅读(702)  评论(0)    收藏  举报