Error : Field $() is not correct

Hi All,

I'm new to shell scripting. Trying to extract substring using awk script as shown below :

Flag=$1
Length=`echo ${#Flag}`
NewLen=$Length-2
NewFlag=`echo $Flag|awk '{print substr($Flag,0,$NewLen)}'`
echo "New string is : $NewFlag"
exit

When I execute this script the following error message appears :
awk: 0602-562 Field $() is not correct.
The input line number is 1.
The source line number is 1.

Could you please guide me to resolve this error.

Thanks in advance.

I think you are trying to extract a string by omitting the last two characters.

If so, you can do

${Flag%??}

In awk:

echo "New string is : $(echo|awk -v Flag=$1 '{print substr(Flag,0,length(Flag)-2)}')"

Thanks everyone for their prompt help!!!!