cal コマンドを使用してシェルスクリプトを作成する方法

cal コマンドを使用してシェルスクリプトを作成する方法

スクリプトを実行しているユーザーが月と年を入力したときに表示されるようにシェルスクリプトを作成したいのですが、そうするとこのエラーが発生します。誰でも助けることができますか?

これらの操作はすべて cal コマンドを使用して実行されます。

これは私のスクリプトです。

#!/bin/bash
cal "" ""
echo  Write month and year
read cal
echo $cal

これはエラーです

Write month and year
cal: year `' not in range 1..9999

この問題をどのように解決できますか?

ベストアンサー1

スクリプトが実際に行うことは次のとおりです。

#!/bin/bash

## Run the command 'cal' with two empty strings as arguments
cal "" ""

## print the string "Write month and year"
echo  Write month and year

## read whatever value was given into the single variable "$cal"
read cal

## print out the contents of the variable "$cal"
echo $cal

私の考えでは、あなたは次のことを意味すると思います:

#!/bin/sh

read -p "Write month and year: " month year

cal "$month" "$year"

次に、次のように実行します(例ではユーザー入力42020)。


$ foo.sh
Write month and year: 4 2020
     April 2020     
Su Mo Tu We Th Fr Sa
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30      

もちろんこれは基本コマンドに実際にcal何も追加しないのであまり意味がないようです。

$ cal 4 2020
     April 2020     
Su Mo Tu We Th Fr Sa
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30      

おすすめ記事