2

I am trying to catch typeset error by the below shell script test.sh:

Shell Script

typeset -i int
int=2
echo $int
int=asd || echo "type mismatch"

But i am getting output as:

Output

./test.sh
2
./test.sh[4]: asd: bad number

Required Result

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

2
  • Which shell are you using? Commented Jul 19, 2013 at 13:21
  • @glennjackman It's in the question. Bash 3.2.51(1) on sparc-sun-solaris2.10. Commented Jul 19, 2013 at 13:29

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.