-4

I have removed the two if statements from before and replaced them with a case statement and tried to remove all the error from the code. I am trying to run this code but i get an error in the case statement. "': not a valid identifier
main.sh: line 5: syntax error near unexpected token $'in\r''
'ain.sh: line 5:
case "$y" in "

    #!/bin/bash
echo "1. Julian"
echo "2. Gregorian"
read y
case "$y" in
1)echo "Enter your year (1900-2050)"
read x
if [[ $x -ge 1900 && $x -le 2050 ]]
 then
a=$((x%19)) 
b=$((x/100))
c=$((x%100))
d=$((b/4))
e=$((b%4))
g=$(((8*b+13)/25))
h=$(((19*a+b-d-g+15)%30))
m=$(((a+11*h)/319))
j=$((c/4))
k=$((c%4))
l=$(((2*e+2*j-k-h+m+32)%7))
n=$(((h-m+l+90)/25))
p=$(((h-m+l+n+19)%32)) 
o=$(date +"$x-$n-$p")

echo "Gregorian Easter is on $O."

else
 echo "Invalid Input" 
 fi
;;

2) echo "Enter your year (1900-2050)"
read x
if [[ $x -ge 1900 && $x -le 2050 ]]
 then
A=$((x%4)) 
B=$((x%7))
C=$((x%19))
D=$(((19*C+15)%30))
E=$(((2*A+4*B−D+34)%7))
M=$(((D+E+114)/31))
day=$(((D+E+115)%31))
o=$(date +"$x-$M-$day")

echo "Gregorian Easter is on $o."
else 
 echo "Invalid Input"
fi
;;
0) exit ;;
esac
  • 1
    There's too many errors in your code, Try balancing the if statement first. You can't balance 3 ifs with just one fi – Jahid Apr 11 '15 at 10:21
  • Take a look at http://www.shellcheck.net/ – Cyrus Apr 11 '15 at 10:26
  • is this a way to learn the shell? not a useful demonstration in the shell. if you're doing arithmetic, take this computation-intensive problem to awk. avoid the syntactic noise the shell imposes on arithmetic. (unless manipulating file names). – Marty McGowan Apr 12 '15 at 01:23

4 Answers4

6

Addressing the issue in the title, ': not a valid identifier

This happens when read is passed a variable name that ends with a carriage return symbol.

When that symbol is printed, it sends the cursor back to the beginning of the line.

Thus, read foo<CR> tries to print:

foo<CR>': not a valid identifier

However, because <CR> sends the cursor back, what it actually prints is:

': not a valid identifier

To fix this, run dos2unix on your file; run :set fileformat=unix in vim, or otherwise transform it to have UNIX newlines instead of DOS newlines.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
2

Replace #!/bin/sh with #!/bin/bash.

Replace elif[ with elif [.

Add a fi line above the elif line.

Add a then line below the elif line.

Replace -gt with -ge.

Replace -lt with -le.

Having done this your o variable may still end up empty, but you can debug that by adding echo +"$x-$n-$p".

pts
  • 80,836
  • 20
  • 110
  • 183
0

Still, there were too many errors. Anyway, I think you want something like this:

#!/bin/bash
echo "1. Julian"
echo "2. Gregorian"
read y
case "$y" in
1)echo "Enter your year (1900-2050)"
read x
if [[ $x -ge 1900 && $x -le 2050 ]]; then
a=$((x%19)) 
b=$((x/100))
c=$((x%100))
d=$((b/4))
e=$((b%4))
g=$(((8*b+13)/25))
h=$(((19*a+b-d-g+15)%30))
m=$(((a+11*h)/319))
j=$((c/4))
k=$((c%4))
l=$(((2*e+2*j-k-h+m+32)%7))
n=$(((h-m+l+90)/25))
p=$(((h-m+l+n+19)%32)) 
o="$date $x-$n-$p"

echo "Julian Easter is on $o."

else
 echo "Invalid Input" 
 fi
;;

2) echo "Enter your year (1900-2050)"
read x
if [[ $x -ge 1900 && $x -le 2050 ]]; then
A=$((x%4)) 
B=$((x%7))
C=$((x%19))
D=$(((19*C+15)%30))
E=$(((2*A+4*B-D+34)%7))
M=$(((D+E+114)/31))
day=$(((D+E+115)%31))
o="$date $x-$M-$day"

echo "Gregorian Easter is on $o."
else 
 echo "Invalid Input"
fi
;;
0) exit
;;
esac
Jahid
  • 21,542
  • 10
  • 90
  • 108
0

One input here- I was facing same issue for one of my scripts. I found one blunder I have done. Always make sure function names should not have - and instead _ should be used.

rajdeepbs29
  • 1,211
  • 12
  • 9