Sumeet ten Doeschate
Sumeet ten Doeschate

Reputation: 27

What's wrong with the following Unix shell script?

I have entered this shell script and its showing errors when compiling

echo Enter basic Salary
read bs
if [ $bs -lt 1500 ]
then
 hra= echo ´$bs \* 10 / 100´|bc
fi
gs= echo ´$bs + $hra´|bc
echo $gs

The errors are:

(standard_in) 1: illegal character: \302
(standard_in) 1: illegal character: \264
(standard_in) 1: illegal character: \302
(standard_in) 1: illegal character: \264
(standard_in) 1: illegal character: \302
(standard_in) 1: illegal character: \264
(standard_in) 1: illegal character: \302
(standard_in) 1: illegal character: \264
(standard_in) 2: syntax error

Upvotes: 0

Views: 364

Answers (2)

Hardy
Hardy

Reputation: 129

Too many Errors:

  1. As stated by Jonathan, is using ´ in place of `.
  2. Do not use space while assigning values to variables.
  3. Create the data before assigning it to a variable.

For e.g. hra=echo $bs \* 10 / 100|bc

Also, if the input exceeds 1500, then it will give out the error. So you need to do something with it.

For e.g. echo "Enter basic Salary" read bs if [ $bs -lt 1500 ] then hra=echo $bs \* 10 / 100|bc else hra=echo $bs \* 5 / 100|bc fi gs=echo $bs + $hra|bc echo $gs

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 754910

One problem is (or, rather, 4 problems are) the use of ´ in place of ' or ".

There is another character in there also causing trouble, unless the acute accent is encoded in UTF-8 or UTF-16

Another problem is the use of spaces around assignments; these do not fly in the shell. You must not have spaces on either side of the assignment.

echo Enter basic Salary
read bs
if [ "$bs" -lt 1500 ]
then hra=$(echo "$bs * 10 / 100"|bc)
fi
gs=$(echo "$bs + $hra"|bc)
echo $gs

You don't really need the variable gs; you could write:

echo "$bs + $hra" | bc

Note that if bs is not less than 1500, you get a ill-defined value for hra (whatever happens to be left over, or blank if it is unset).

It looks like you have UTF-8 encoded data.

$ echo "´" | odx
0x0000: C2 B4 0A                                          ...
0x0003:
$ echo "´" | utf8-unicode
0xC2 0xB4 = U+00B4
0x0A = U+000A
$ bc | cat
ibase=16
obase=8
C2
302
B4
264
quit
$

U+00B4 is ACUTE ACCENT in Unicode.

Upvotes: 1

Related Questions