1

Google didn't help me.

#!/bin/sh

j1=`expr "$1"`
j2=`expr "$2"`

while [ $j1 -le $j2 ]; do
    date=$(ncal -e $j1)
    month=$($date | cut -f1 -d' ')
    if [ $month=="April" ]; then
        echo $date
    fi
        j1=`expr $j1 + 1`
done

I want to print the date of easter only for the years where it is in the month april (in the range between the two years i passed as arguments). But somehow I get the output:

...

April 5 2015
./script.sh: 8: March: not found
March 27 2016
./script.sh: 8: April: not found
April 16 2017

What means "month: not found"? And why does it print March although I only ask for april?

I tried it various ways and there was always some error.

I also need to add the condition that only those dates are printed that are beyond the 20th april, but I am not even able to handle this.

Strict
  • 15

1 Answers1

1

On the line

month=$($date | cut -f1 -d' ')

you execute whatever is in $date as a command and pipe its output to cut.

I think you want

month=$( printf '%s\n' "$date" | cut -f 1 -d ' ' )

here.

Also, don't mix backticks and $(...) in the same script, it's confusing. Just use $(...) instead.

Comparing strings is done with =:

if [ "$month" = "April" ]; then

Be sure to double quote your variables. See Security implications of forgetting to quote a variable in bash/POSIX shells

Integer arithmetics may be done using $(( expression )). expr is antiquated.

j1=$(( "$1" ))
j2=$(( "$2" ))

...

j1=$(( j1 + 1 ))
Kusalananda
  • 333,661