Reputation: 122
I have used this simple bash function to check if ip is valid and it works fine, when I use source checkip.sh && isvalidip 127.0.0.1 && echo $?
in bash env. but the same thing does not work when I am in zsh environment and always returns not valid ($? == 1).
Any idea how can make it work in zsh? (I want to use it as a function in shell env)
#!/bin/bash
isvalidip(){
case $1 in
"" | *[!0-9.]* | *[!0-9]) return 1 ;;
esac
local IFS=.
set -- $1
[ "$#" -eq 4 ] && [ ${1:-666} -le 256 ] && [ ${2:-666} -le 256 ] \
&& [ ${3:-666} -le 256 ] && [ ${4:-666} -le 256 ]
}
Upvotes: 2
Views: 727
Reputation: 5339
We need the ${=specs}
syntax around there in zsh, something like below:
local IFS=.
if [[ -n ${ZSH_VERSION-} ]]; then
set -- ${=1}
else
set -- ${1}
fi
It seems that zsh does not expand parameters here as with bash according to the zsh manual:
${=spec}
[...snip...]
This forces parameter expansions to be split into separate words before substitution, using IFS as a delimiter. This is done by default in most other shells.
Upvotes: 1