简体   繁体   中英

bash function does not work in zsh env

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 ]                                                    
                                                                        } 

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.

-- zshexpn(1) 14.3 Parameter Expansion

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM