#!/bin/sh while getopts dm name do case $name in d)dopt=1;; m)mopt=1;; *)echo "Invalid arg!";; esac done DT=`date '+%d %b'` if [[ ! -z $dopt ]]; then echo "Date is: " ${DT/ */} fi if [[ ! -z $mopt ]]; then echo "Month is: " ${DT/* /} fi shift $(($OPTIND - 1))
结果:
1 2 3 4 5 6 7
# ./disp.sh -d Date is: 19 # ./disp.sh -d -m Date is: 19 Month is: 5月 # ./disp.sh -m Month is: 5月