': not a valid identifier [duplicate] - bash

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 1 year ago.
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

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.

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".

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

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.

Related

Syntax error in conditional expression and near `then' in Ubuntu terminal

I'm just learning terminal commands, I'm trying to create my own command, but I have the following problem
./myFile: line 10: syntax error in conditional expression
./myFile: line 11: syntax error near `then'
./myFile: line 11: ` then'
there is my code
#!/bin/bash
echo "please enter name of the folder"
read NAME1
if [[ $NAME1 ]]
then
echo "enter first number"
read NUM1
if [[ $NUM1 ]] && [[ $NUM1 -ge 0]]
then
echo "enter last number"
read NUM2
if [[ $NUM2 ]] && [[ $NUM2 -gt $NUM ]]
then
mkdir $NAME1{$NUM1..$NUM2}
else
echo"please enter last number to continue"
fi
else
echo "please enter first number to continue"
fi
else
echo "please enter name of the folder to continue"
fi
Firstly, the expression [[ $NAME1 ]] is not valid, or at least, does not do what you think it does. I believe you are trying to determine if the user actually typed in a string. To do this, you should either test for a non-empty string ([[ -n ${NAME1} ]]) or test for a string length greater than zero ([[ ${#NAME1} -gt 0 ]]). The same applies when you are testing $NUM1 and $NUM2.
Secondly, when dealing with user input, you should take care to avoid testing empty strings. This is best achieved by quoting your variables. For example: [[ "${NUM1}" -gt 0 ]].
Thirdly, spaces are important in tests. always leave a space after the [[ and before the ]].
In addition, $NUM (line 15) is not declared, so will evaluate to the empty string. This should be set somewhere earlier in the script.
There are many other areas in which this script could be improved (e.g. checking for numerical input, checking for valid folder names, etc. But the above changes should get you past the immediate errors.

Unexpected end of file for bash

read answer #this will read what the user is typing in and makes a string for it
if [[ -z {answer} ]]
then
echo "Sorry that is not a valid choice, please select from the above list provided" #this will help pick up if the user just enters a blank option
if [[ $1{answer} ]]
then
echo "this has worked" #no clue if this will work going to try this and see if it works
if [[ $2{answer} ]]
then
echo "this is answer 2"
if [[ $3{answer} ]]
then
echo "this is answer 3"
if [[ $4{answer} ]]
then
echo "This is answer 4"
if [[ $Q{answer} ]]
then
echo "this is the quit button"
else
echo "no valid choice has been made"
fi
fi
fi
fi
fi
fi
so i am trying to run this code, i have closing arguments for each if statement, but every time i try and run it it shows "line 51: syntax error: unexpected end of file" the code goes up to line 50, i tried putting in ;; but it ran a syntax error on them, i tried converting with the dos2unix command but nothing, is there something i am missing?
Add the $ to this line, like so
if [[ -z ${answer} ]]

Why does this if statement give me an error

Can someone explain why this simple bash script:
#!/bin/bash
myvar="Hello"
if [[ -z "$myvar" ]]; then
# echo "It's an unfilled string"
else
echo "It's a filled string!"
fi
gives me the error
./testscript: line 7: syntax error near unexpected token `else'
./testscript: line 7: `else'
However, if I remove the comment on the echo line, the script runs fine. Obviously, there is an issue with having commented lines within empty if statements. This this in mind, how do I fix it so I can have an empty if statement with comments?
There are no statements between then and else, so this is a syntax error. If you really want to do nothing in the if branch, then you can use a : (or true) as a placeholder:
#!/bin/bash
myvar="Hello"
if [[ -z "$myvar" ]]; then
# echo "It's an unfilled string"
:
else
echo "It's a filled string!"
fi
Better yet, reverse your logic:
#!/bin/bash
myvar="Hello"
if [[ -n "$myvar" ]]; then
echo "It's a filled string!"
fi
This is the way not to use if else statement.
#!/bin/bash
myvar="Hello"
[[ -n "$myvar" ]] && echo "It's a filled string!"
Also you can use this.
#!/bin/bash
myvar="Hello"
[[ -z "$myvar" ]] || echo "It's a filled string!"

if ["$i" -gt "$count"]; IS GIVING AN ERROR

I'm trying to put the 'f-$count'(f-1,f-2) name into the array.
Below is the code,
echo "Enter the count"
read count
echo $count
#arr=()
i=1
while true;
do
if ["$i" -gt "$count"]; then
exit 0
else
arr[$i]=f-$i
i=$((i+1))
fi
done
echo ${arr[#]}
I'm getting the error as 'script.sh: line 11: [3570: command not found
' continuously.
In shell programming, the brackets in the if MUST be delimited by spaces:
if ["$i" -gt "$count"]; then
MUST be:
if [ "$i" -gt "$count" ]; then
[EDIT] The left bracket ([) is actually a built-in shell command and so requires the space afterwards to delimit it from its parameters, as with any command.

Check input consists only of numeric characters in shell script

I need to check that the input consists only of numeric characters. I have the code below, but it didn't work properly.
if [[ $1 =~ [0-9] ]]; then
echo "Invalid input"
fi
It should give true only for 678686 not for yy66666.
How about this:-
re='^[0-9]+$'
if ! [[ $Number =~ $re ]] ; then
echo "error: Invalid input" >&2; exit 1
fi
or
case $Number in
''|*[!0-9]*) echo nonnumeric;;
*) echo numeric;;
esac
Try using start/end anchors with your pattern. If you don't, the match succeeds with a part of a test string. Don't forget that you have to use a pattern matching the complete test string if you follow this suggestion.
if [[ $1 =~ ^[0-9]+$ ]]; then
echo "Invalid input"
fi
Check out this SO post for more details.

Resources