これは1行で行う必要がありますか、ユーザーは1行に2つの日付を入力しますか?
あなたが必要とするすべての入力にユーザコマンドライン上の2つの日付である場合は、これを行うことができます。そして、
read -p "Enter two dates in YY/MM/DD format: " date1 date2
を、ユーザーが2つの日付に入った後、あなたは確認するために、それらを解析することができます彼らは正しい形式になっている。日付が正しいまで、あなたの周りのループ保つことができます。
while 1
do
read -p "Enter two dates in 'DD/MM/YYY' format: date1 date2
if [ ! date1 ] -o [ ! date2 ]
then
echo "You need to enter two dates."
sleep 2
continue
if
[other tests to verify date formats...]
break # Passed all the tests
done
あなたは、あなたがセパレータとしての代わりに空白のスラッシュを使用するIFS
変数を入力日付を一度に操作することができます。
OLDIFS="$IFS" #Save the original
IFS="/"
read -p "Enter your date in MM/DD/YYYY format: " month day year
IFS="$OLDIFS" #Restore the value of IFS
これは、日付が正しく入力された検証可能性が上記の例のようにwhile
ループの内側に置くことができます。
OLDIFS="$IFS" #Save the original
IFS="/ " #Note space before quotation mark!
read -p "Enter two dates in MM/DD/YYYY format: " month1 day1 year1 month2 day2 year2
IFS="$OLDIFS" #Restore the value of IFS
echo "Month #1 = $month1 Day #1 = $day1 Year #1 = $year1"
echo "Month #2 = $month1 Day #2 = $day2 Year #2 = $year2"
をし、同じコマンドラインで両方の日付を取得:
実は、あなたはこれを行うことができます。