對於使用者輸入日期的合法性檢驗,是個很重要的問題,這個例子是簡單得取得使用者輸入的日期,並轉換為相應的格式,但不完美,原因請看後文。#!/bin/sh# normdate -- Normalizes month field in date specification# to three letters, first letter capitalized. A helper# function for Script #7, valid-date. Exits w/ zero if no
今天無意中發現一本挺有意思的shell編程的書,是e文的,內容是101個shell案例,堅持明天看一個,寫點心得。下面是例子001:#!/bin/sh# inpath - Verifies that a specified program is either valid as is,# or that it can be found in the PATH directory list.in_path(){ # Given a command and the PATH, try to
今天這個例子是 用來驗證使用者輸入的參數的合法性的,程式並不複雜,如下所示:#!/bin/sh# validAlphaNum - Ensures that input consists only of alphabetical# and numeric characters.validAlphaNum(){ # Validate arg: returns 0 if all upper+lower+digits, 1 otherwise # Remove all unacceptable
今天的案例是將 對使用者輸入的判斷的#!/bin/sh# validint -- Validates integer input, allowing negative ints too.function validint{ # Validate first field. Then test against min value $2 and/or # max value $3 if they are supplied. If they are not supplied, skip these
Given a number, shows it in comma-separated form.Expects DD and TD to be instantiated. Instantiates nicenum. or, if a second arg is specified, the output is echoed to stdout.廢話不多說,首先是#!/bin/sh# nicenumber -- Given a number, shows it in
這個shell是來判斷輸入的數字是否為合理的浮點數實現代碼如下:#!/bin/sh# validfloat -- Tests whether a number is a valid floating-point value.# Note that this script cannot accept scientific (1.304e5) notation.# To test whether an entered value is a valid floating-point number,
這次的shell案例比較簡單,但有其特點。#!/bin/sh# scriptbc - Wrapper for 'bc' that returns the result of a calculation.if [ $1 = "-p" ] ; then precision=$2 shift 2else precision=2 # defaultfibc -q << EOFscale=$precision$*quitEOFexit 0指令碼特點:1)
實現代碼:#!/bin/sh# ANSI Color -- Use these variables to make output in different colors# and formats. Color names that end with 'f' are foreground (text) colors,# and those ending with 'b' are background colors.initializeANSI(){ esc="\033" # if this
To turn the functions in this chapter into a library for use in other scripts, extract all the functions and concatenate them into one big file. If we call this file library.sh, a test script that accesses all of the functions might look like this:#!
原本書上這個例子是教調試指令碼的,它故意給出的指令碼中有幾個錯誤,教我們認識系統提示的錯誤,並修改運行。但比較難寫出來,所以把修改好了的指令碼放上來,這個指令碼比較有趣味的#!/bin/sh # hilow -- A simple number-guessing game biggest=100 # maximum number possible guess=0 # guessed by player guesses=0
很久沒更新了,很久沒看指令碼了想想應該繼續看指令碼了#14 Formatting Long Lines#!/bin/sh# A version of fmt, using nroff. Adds two useful flags: -w X for line width# and -h to enable hyphenation for better fills.while getopts "hw:" opt; do case $opt in h) hyph=1 ;; w)