制作显示欢迎信息的脚本程序
終端程序恐怕是Linux用戶(hù)使用最為頻繁的了。我的Debian系統(tǒng)啟動(dòng)后便是直接進(jìn)入的終端界面。為了在每次登錄時(shí)或者是在X視窗環(huán)境下打開(kāi)終端程序時(shí)顯示一些歡迎信息,比如當(dāng)前的日期、名人警句等,從而可以增加一些生活情趣,就可以創(chuàng)建一個(gè)腳本程序,然后在~/.bashrc文件調(diào)用它。我自己就寫(xiě)了一個(gè)welcome.sh,執(zhí)行時(shí)首先顯示當(dāng)前登錄的用戶(hù)名與主機(jī)名,然后打印出當(dāng)天的日期、當(dāng)天處于今年的第幾個(gè)星期——這一數(shù)據(jù)在項(xiàng)目匯報(bào)中常被使用。同時(shí),顯示當(dāng)天是今年的第幾天,計(jì)算出距離年末還有幾天,這樣就可以給自己警醒,“又一年即將過(guò)去,自己是否珍惜了時(shí)光?”。最后,腳本調(diào)用fortune程序,隨機(jī)顯示一些警句或有意思的話。將這些綜合起來(lái),寫(xiě)成welcome.sh腳本如下:
#!/bin/bashscript_name="welcome.sh" script_usage=$(cat <<EOF welcome.sh [OPTIONS] EOF ) script_function=$(cat <<EOF Display a welcome message. EOF ) script_doc=$(cat <<EOF -h Display this help. EOF ) script_examples=$(cat <<EOF EOF ) state_prefix="===" warning_prefix="***" error_prefix="!!!"function display_help() {if [ -n "$script_usage" ]; thenecho -e "Usage: $script_usage"fiif [ -n "$script_function" ]; thenecho -e "$script_function"fiif [ -n "$script_doc" ] ; thenecho -e "\n$script_doc"fiif [ -n "$script_examples" ]; thenecho -e "\nExamples"echo -e "$script_examples"fi }# Delete a same character from the beginning of a variable. The source variable is the global variable $src_string # $1: pattern character function del_leading_chars() {local pat_charlocal prev_src_string_lenlocal src_string_lendeclare -i prev_src_string_lendeclare -i src_string_lenpat_char=$1prev_src_string_len=0src_string_len=${#src_string}while [ $(($prev_src_string_len != $src_string_len)) = 1 ]; doprev_src_string_len=$src_string_lensrc_string=${src_string/#${pat_char}/}src_string_len=${#src_string}done }# Process command options while getopts ":h" opt; docase $opt inh ) display_helpexit 0 ;;\? ) display_helpexit 1 ;;esac done shift $(($OPTIND - 1))# Start execute the commanddeclare -i total_dates=365 year=`date +%Y`# Test leap year if [ $(($year % 100 == 0)) = 1 ]; thenif [ $(($year % 400 == 0)) = 1 ]; thentotal_dates=366fi elseif [ $(($year % 4 == 0)) = 1 ]; thentotal_dates=366fi fisrc_string=`date +%j` del_leading_chars '0' cur_day_no=$src_string src_string=`date +%V` del_leading_chars '0' cur_week_no=$src_stringecho "********************************************************" echo "Hello "`whoami`"! ""Welcome to "`hostname`"!" echo "Today is "`date "+%A, %B %d, %Y"` echo "Week $cur_week_no, day $cur_day_no, remaining days $(($total_dates - $cur_day_no))" echo "********************************************************" fortune
將welcome.sh放入~/.bashrc后,現(xiàn)在再打開(kāi)終端程序,就會(huì)顯示出現(xiàn)面的信息:
轉(zhuǎn)載于:https://www.cnblogs.com/quantumman/p/4586756.html
總結(jié)
以上是生活随笔為你收集整理的制作显示欢迎信息的脚本程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: .NET泛型解析(下)
- 下一篇: 汉服多少钱啊?