Reputation: 118
I am trying to catch typeset error by the below shell script test.sh:
typeset -i int
int=2
echo $int
int=asd || echo "type mismatch"
But i am getting output as:
./test.sh
2
./test.sh[4]: asd: bad number
./test.sh
2
./test.sh[4]: asd: bad number
**type mismatch**
I am using the following machine:
bash --version
GNU bash, version 3.2.51(1)-release (sparc-sun-solaris2.10)
Copyright (C) 2007 Free Software Foundation, Inc.
Please suggest me the change i need to make in my script. I need a way to make sure that the input parameter is INT. And i have to use typeset along with Exception Handling.
Upvotes: 2
Views: 751
Reputation: 212238
Try using eval:
typeset -i int # Using the name "int" just seems like asking for trouble!
eval int="$1" || echo "type mismatch" >&2
The issue is that in the line int=asd || ...
, the first simple command will always evaluate to true
as required by the standard, since no command is given. The variable assignments take place in the current shell, but no command is given and the results are as specified in section 2.9.1 :
If there is a command name, execution shall continue as described in Command Search and Execution . If there is no command name, but the command contained a command substitution, the command shall complete with the exit status of the last command substitution performed. Otherwise, the command shall complete with a zero exit status.
Upvotes: 1