Thursday 15 September 2011

linux - How to check the status code of a function in sh? -


I am converting my script to bash ... and there is a problem getting the return code of the function. #! / Bin / sh check_ip () {local netbit = `resonance" $ 1 "| Awk -F \ / '{print $ 1}' 'local netmask =' resonant "$ 1" | Awk -F \ / '{Print $ 2}' `If the case is" $ netbit "* in *! [0-9] * | *. *. *. *. * | * .. * | [! 0- 9] * | * [! 0- 9]) Wrong ;; * 25 [6-9] * | * 2 [6-9] [0-9] * | * [3- 9] [0- 9] [0-9] * | * [0- 9] [0-9] [0- 9] [0- 9] *) false ;; *.*.*.* ) truth ;; * ) false ;; Esac; So if [ ! -J "$ netmask"]; So if ["$ netmask" -ge 1] & amp; Amp; ["$ Netmask" -32]; Then come back 0 and return 1fi return 0f and return 1fi} # it is working without work [check_ip "$ 1"; Then okay, echo is okay, echo nofi

saw a similar script from another machine, and it does not have [] because @Bermer has suggested. All are working now thanks a lot.

if chooses or < Code> Other based on the status of the command to exit the command. You only use [ if you want to test defined in the test command; If you want to test only a special order, you can use it directly.

  If check_ip "$ ip" then echo the good IP and echo the bad ip file   

It is also unlikely that $! will be the appropriate parameter for your function. $! The last background is the PID of the process, which was not started IP.

No comments:

Post a Comment