I can't deal with the problem. I run the script (I'll attach it below), after the first question I get the error "line 37 syntax error near unexpected token `fi' If fi is removed, nothing changes. Help me to understand... I tried many solutions, and dos2unix configuration, and double-checked all the indents
#!/bin/bash
function pause (){
read -p "Press any key to continue ... "
}
timedatectl set-timezone Atlantic/Reykjavik
a=`date "%Y-%m-%d %H:%M:%S"`
echo -n -e "Current Date/Time is "
date
sleep 2
read -p "is it correct (Yes/No) ? " CC
if [ "$CC" == "N" -o "$CC" == "n" ];then
echo "Exit testing ..."
exit
fi
else
read -p "Do you want to change (Yes/No) ? " CC
if [ "$CC" == "Y" -o "$CC" == "y" ];then
read -p "Please input new date, format: YYYYMMDD " D
NEWD="`echo $D | cut -b 1-4`"-"`echo $D | cut -b 5-6`"-"`echo $D | cut -b 7-8`"
read -p "Please input new time, format: HHMM " T
NEWT="`echo $T | cut -b 1-2`":"`echo $T | cut -b 3-4`":"00"
echo -n "date -s '" > d.sh
echo -n "$NEWD $NEWT" >> d.sh
echo "'" >> d.sh
chmod 777 d.sh
./d.sh
echo
sleep 1
clock -w
sleep 1
a=`date "%Y-%m-%d %H:%M:%S"`
echo -n -e "NEW Date/Time is""\e[0;32m $a\e[0m"", "
read -p "Press ENTER to continue..."
fi
UPD: enter image description here Display after deletion "else"
UPD2 This is second similar code that gives the same error. Plus, doesn't request the entry of information ("Please input the First MAC address:)
echo "************************"
echo " Serial number setting "
echo "************************"
SLEN=0
while [ "$SLEN" != "13" ]
do
read -p "Please input Serial Number:" sn
SLEN="${#sn}"
done
echo "*************************"
echo " MAC-address setting "
echo "*************************"
PLEN=0
until [ "$PLEN" == "12" ] && [ "$VID" == "0015B2" ]
do
read -p "Please input the First MAC address: " MACIN
PLEN="${#MACIN}"
if [ $PLEN != 12 ];then
echo
echo MAC address LENTH error !!
fi
MAC=`echo $MACIN | tr a-z A-Z`
VID=`echo $MAC | cut -b 1-6`
if [ "$VID" != "0015B2" ];then
echo
echo -n "The first 6 digit MUST be"
echo -n -e "\e[0;37;42m 0015B2 \e[0m", but you input
echo -e "\e[0;37;41m $VID \e[0m""!!"
fi
done
CodePudding user response:
Remove "else" after the first if block and your code should work
CodePudding user response:
The most likely explanation for your problem is an 'exotic' (non-ASCII) Unicode character within your script. Like most scripting and programming languages, Bash cannot stand the presence of non-ASCII characters within or next to a token.
For example, there is a space following then
in this statement:
if [ "$CC" == "N" -o "$CC" == "n" ];then
This is fine, as long as that space is the ASCII space character (U 0020). If that space character would happen to be a non-breaking space (U 00A0), then you will get exactly the error that you witnessed:
Current Date/Time is Thu Jul 21 16:39:09 WEDT 2022
is it correct (Yes/No) ? y
./test.sh: line 14: syntax error near unexpected token `fi'
./test.sh: line 14: ` fi '
That was just an example. There are many more Unicode characters that can cause this problem.
- Zero-width space (U 200B) next to or even within a token. As the name suggests, this character is invisible in most fonts.
- Non-ASCII letters; I remember a similar problem caused by a Cyrillic Es taking the place of a roman C in an identifier. In many fonts, the two are indistinguishable.
I strongly advice you to take a hex viewer and very carefully inspect your script for the presence of Unicode characters.