I have some inputs values (32-bit unsigned integer) defined as follow, with their associated identifier, in a file :
var=0x000000001
var1=0x000000002
var2=0x000000004
var3=0x000000008
toto=0x000000010
titi=0x000000020
tata=0x000000040
toto1=0x000000080
toto2=0x000000100
toto3=0x000000200
titi2=0x000000400
titi3=0x000000800
tito0=0x000001000
tito1=0x000002000
tito2=0x000004000
tito3=0x000008000
tito4=0x000010000
I would like to implement a second function name GetConfig() which must return an uint32 value. Based on this value, I would to quote the associated variable. If I enter 300 I should display toto2 toto3.
function Config()
{
vartest=$1
if [ -f $file ]; then
while read lines
do
value=${lines##*=}
mask=$(($vartest & $value))
echo $mask
done < $file
else
exitError 101
fi
}
If I enter ./script Config 3840 I obtain :
0
0
0
0
0
0
0
0
256
512
1024
2048
0
0
0
0
0
I can easily display toto2 toto3 titi5 titii, but if I enter ./script Config 4812, I obtain :
0
0
4
8
0
0
64
128
0
512
0
0
4096
0
0
0
0
This result can not allows me to display var1 titi titii test1
I am not sure to be clear but I am really currently blocked on this mask issue.
Thank in advance
Your problem statement isn't very clear but I hope this snippet can help put you back on track. The real meat is in using ${lines%=*} to obtain the key corresponding to the value in this lines.
Config() {
vartest=$1
if [ -f $file ]; then
while read lines
do
value=${lines##*=}
test $(($vartest & $value)) = 0 && continue
echo "${lines%=*}"
done < $file
else
exitError 101
fi
}
file=/dev/stdin
Config 3840 <<':'
var=0x000000001
var1=0x000000002
var2=0x000000004
var3=0x000000008
toto=0x000000010
titi=0x000000020
tata=0x000000040
toto1=0x000000080
toto2=0x000000100
toto3=0x000000200
titi2=0x000000400
titi3=0x000000800
tito0=0x000001000
tito1=0x000002000
tito2=0x000004000
tito3=0x000008000
tito4=0x000010000
:
Test run:
toto2
toto3
titi2
titi3
I would expect a more-useful function to not require the values in a file; this incidentally also illustrates how to put them in a here document (I did some weird stuff to not have to change those parts of your function).
Related
I need to know if Bash has some solution for my case. I need after some conditions to do a "double return". I mean, to perform a return of a function and also return the parent function to skip the rest of the code of that parent function.
I know that I can do a conditional using function return values to achieve this. But I'd like to know if in Bash exist something like "break 2" for functions. I don't want if possible to modify the code of the parent function because as you can imagine, in my real script there are dozens of functions and I don't want to modify all of them.
Example:
#!/bin/bash
function sublevelone() {
echo "sublevelone"
# Return 2, or break 2 or something :D
}
function main() {
sublevelone
echo "This is the part of the code to being avoid executed"
}
main
I don't know what the bash experts will think, but this works at least for simple cases:
multireturn(){
[ -n "$1" ] && poplevel="$1"
if [ "$poplevel" -ge 0 ]; then
trap multireturn DEBUG
shopt -s extdebug
(( poplevel-- ))
return 2
else
shopt -u extdebug
trap - DEBUG
return 0
fi
}
This makes use of the DEBUG trap and the extdebug flag:
extdebug
If set at shell invocation, arrange to execute the
debugger profile before the shell starts, identical to
the --debugger option. If set after invocation, behav-
ior intended for use by debuggers is enabled:
1. The -F option to the declare builtin displays the
source file name and line number corresponding to
each function name supplied as an argument.
2. If the command run by the DEBUG trap returns a
non-zero value, the next command is skipped and
not executed.
3. If the command run by the DEBUG trap returns a
value of 2, and the shell is executing in a sub-
routine (a shell function or a shell script exe-
cuted by the . or source builtins), the shell
simulates a call to return.
4. BASH_ARGC and BASH_ARGV are updated as described
in their descriptions above.
5. Function tracing is enabled: command substitu-
tion, shell functions, and subshells invoked with
( command ) inherit the DEBUG and RETURN traps.
6. Error tracing is enabled: command substitution,
shell functions, and subshells invoked with (
command ) inherit the ERR trap.
Example usage:
#!/bin/bash
multireturn(){
[ -n "$1" ] && poplevel="$1"
if [ "$poplevel" -ge 0 ]; then
trap multireturn DEBUG
shopt -s extdebug
(( poplevel-- ))
return 2
else
shopt -u extdebug
trap - DEBUG
return 0
fi
}
# define 8 levels of function calls
# (level N prints output, calls level N+1, then prints more output)
for i in $(seq 1 8); do
eval \
'level'$i'(){
echo -n " '$i'"
level'$((i+1))'
echo -n "('$i')"
}'
done
# final level calls multireturn
level9(){
echo -n " 9"
multireturn $n
echo -n "(9)"
}
# test various skip amounts
for i in $(seq 0 10); do
echo -n "$i:"
n=$i
level1
echo .
done
echo
echo done
Result:
0: 1 2 3 4 5 6 7 8 9(9)(8)(7)(6)(5)(4)(3)(2)(1).
1: 1 2 3 4 5 6 7 8 9(8)(7)(6)(5)(4)(3)(2)(1).
2: 1 2 3 4 5 6 7 8 9(7)(6)(5)(4)(3)(2)(1).
3: 1 2 3 4 5 6 7 8 9(6)(5)(4)(3)(2)(1).
4: 1 2 3 4 5 6 7 8 9(5)(4)(3)(2)(1).
5: 1 2 3 4 5 6 7 8 9(4)(3)(2)(1).
6: 1 2 3 4 5 6 7 8 9(3)(2)(1).
7: 1 2 3 4 5 6 7 8 9(2)(1).
8: 1 2 3 4 5 6 7 8 9(1).
9: 1 2 3 4 5 6 7 8 9.
10: 1 2 3 4 5 6 7 8 9
done
This is kinda whacky but if you use parentheses to define levelone, it will execute the function in a subshell and then you can exit out of that shell from an inner function. That said, I think it's more appropriate to use return to send back value that you check for in the parent function.
#!/bin/bash
function leveltwo() {
echo "two"
exit
}
function levelone() (
echo "one"
leveltwo
echo "three"
)
levelone
echo "four"
Will print:
one
two
four
Why don't you return an exit status from the function and add an if-statement to main like you normally would in any other programming/scripting language?
#!/bin/bash
function sublevelone() {
echo "sublevelone"
[ 0 -eq 1 ]
return $? # Returns 1 in this case because 0 != 1
}
function main() {
sublevelone
[ $? -eq 0 ] || return 1 # Return in case of error
echo "This is the part of the code to being avoid executed"
}
main
exit 0
Just a short version of #jhnc answer for exactly 2-level return from a function:
trap 'trap "shopt -u extdebug; trap - DEBUG; return 0" DEBUG; return 2' DEBUG
shopt -s extdebug
return
How can I turn the output of zpool status -v into something usable, with data that match by row in a data.oriented format, instead of the silly "visual" output it uses, so that it's something scriptable, using standard unix-like utilities? I had a python script that did something acceptable, but python 3 completely breaks it, and I'm not fixing it just to have some new version of python break it again. (after screwing around getting the script to run with no errors, it returns nothing :)
bascially this space-bar alinged mess:
pool: data
state: ONLINE
status: Some supported features are not enabled on the pool. The pool can
still be used, but some features are unavailable.
action: Enable all features using 'zpool upgrade'. Once this is done,
the pool may no longer be accessible by software that does not support
the features. See zpool-features(7) for details.
scan: scrub repaired 0 in 4h52m with 0 errors on Fri Aug 18 04:52:47 2017
config:
NAME STATE READ WRITE CKSUM
data ONLINE 0 0 0
mirror-0 ONLINE 0 0 0
gptid/6dfb7dbe-68c5-11e6-982d-00e04c68f511 ONLINE 0 0 0
gptid/27f40ebe-8f1b-11e4-94f8-3085a9405b85 ONLINE 0 0 0
gptid/9244318f-c1b4-11e6-a31d-0cc47ae2abe8 ONLINE 0 0 0
mirror-1 ONLINE 0 0 0
gptid/1993f2d7-8f1b-11e4-94f8-3085a9405b85 ONLINE 0 0 0
gptid/529e2c88-f1d1-11e6-89c3-0cc47ae2abe8 ONLINE 0 0 0
gptid/53a09a3e-f1d1-11e6-89c3-0cc47ae2abe8 ONLINE 0 0 0
mirror-2 ONLINE 0 0 0
gptid/51f3b377-6a20-11e6-be8c-00e04c68f511 ONLINE 0 0 0
gptid/9fb54bde-1e2d-11e7-a83e-0cc47ae2abe8 ONLINE 0 0 0
gptid/9eebde32-1e2d-11e7-a83e-0cc47ae2abe8 ONLINE 0 0 0
cache
gptid/63db5172-20bd-11e7-b561-0cc47ae2abe8 ONLINE 0 0 0
errors: No known data errors
to something with actual columnns like this:
NAME STATE READ WRITE CKSUM
data ONLINE 0 0 0
data mirror-0 ONLINE 0 0 0
data mirror-0 gptid/6dfb7dbe-68c5-11e6-982d-00e04c68f511 ONLINE 0 0 0
data mirror-0 gptid/27f40ebe-8f1b-11e4-94f8-3085a9405b85 ONLINE 0 0 0
data mirror-0 gptid/9244318f-c1b4-11e6-a31d-0cc47ae2abe8 ONLINE 0 0 0
data mirror-1 ONLINE 0 0 0
data mirror-1 gptid/1993f2d7-8f1b-11e4-94f8-3085a9405b85 ONLINE 0 0 0
data mirror-1 gptid/529e2c88-f1d1-11e6-89c3-0cc47ae2abe8 ONLINE 0 0 0
data mirror-1 gptid/53a09a3e-f1d1-11e6-89c3-0cc47ae2abe8 ONLINE 0 0 0
data mirror-2 ONLINE 0 0 0
data mirror-2 gptid/51f3b377-6a20-11e6-be8c-00e04c68f511 ONLINE 0 0 0
data mirror-2 gptid/9fb54bde-1e2d-11e7-a83e-0cc47ae2abe8 ONLINE 0 0 0
data mirror-2 gptid/9eebde32-1e2d-11e7-a83e-0cc47ae2abe8 ONLINE 0 0 0
data cache
data cache gptid/63db5172-20bd-11e7-b561-0cc47ae2abe8 ONLINE 0 0 0
I can use perl to remove and rearrange, but I can't work out how to match the rows dynamically, in a way that would work with mirror/raidz123/stripe/cache.
datadata ONLINE 0 0 0
data mirror-0 ONLINE 0 0 0
data gptid/6dfb7dbe-68c5-11e6-982d-00e04c68f511 ONLINE 0 0 0
data gptid/27f40ebe-8f1b-11e4-94f8-3085a9405b85 ONLINE 0 0 0
data gptid/9244318f-c1b4-11e6-a31d-0cc47ae2abe8 ONLINE 0 0 0
data mirror-1 ONLINE 0 0 0
data gptid/1993f2d7-8f1b-11e4-94f8-3085a9405b85 ONLINE 0 0 0
data gptid/529e2c88-f1d1-11e6-89c3-0cc47ae2abe8 ONLINE 0 0 0
data gptid/53a09a3e-f1d1-11e6-89c3-0cc47ae2abe8 ONLINE 0 0 0
data mirror-2 ONLINE 0 0 0
data gptid/51f3b377-6a20-11e6-be8c-00e04c68f511 ONLINE 0 0 0
data gptid/9fb54bde-1e2d-11e7-a83e-0cc47ae2abe8 ONLINE 0 0 0
data gptid/9eebde32-1e2d-11e7-a83e-0cc47ae2abe8 ONLINE 0 0 0
datacache
data gptid/63db5172-20bd-11e7-b561-0cc47ae2abe8 ONLINE 0 0 0
This is the code that generates the above.
zpool status -v data | sed '/ data/, $!d' | grep -v errors: > /tmp/diskslistzpoolstatusdata
perl -pi -e 's/^\n$//' /tmp/diskslistzpoolstatusdata #remove blank lines
perl -pi -e 's/\t$//' /tmp/diskslistzpoolstatusdata
perl -p -i -e 's/\t//g' /tmp/diskslistzpoolstatusdata
perl -pi -e 's/^/data/' /tmp/diskslistzpoolstatusdata
extra:
include the scrub summary and error lines per gptid
NAME STATE READ WRITE CKSUM
misc ONLINE 0 0 0
misc mirror-0 ONLINE 0 0 0
misc mirror-0 gptid/aefbaf6e-e004-11e6-8f42-0cc47ae2abe8 ONLINE 0 0 0 0err/4h52m/0err/Fri Aug 18 04:52:47 2017 No known data errors
misc mirror-0 gptid/affc3cac-e004-11e6-8f42-0cc47ae2abe8 ONLINE 0 0 0 0err/4h52m/0err/Fri Aug 18 04:52:47 2017 No known data errors
misc cache gptid/3139819b-20bd-11e7-b561-0cc47ae2abe8 ONLINE 0 0 0 0err/4h52m/0err/Fri Aug 18 04:52:47 2017 No known data errors
Unfortunately there is no integrated solution available. You have two options:
Parse it yourself in a language of your choice. You already extracted the essential information. The layout is relatively static, as vdevs and pools cannot be nested (pools contain vdevs, never pools themselves), the order is respected (no devices from vdev A come after vdev B), the keywords are few and fixed (mirror-N, raidzX-N, etc), and the output is quite small (less than hundreds of lines usually). This means you just have to go through each row, read the info you need, store it in nested objects or simply arrays and go to the next line.
Directly call the appropriate C functions to get the status in non-readable form and convert the output. To do this, have a look at status_callback(zpool_handle_t *zhp, void *data), where all printf-output is generated from the pool data. You could mirror this function to convert the output into a format you like instead of the indented format, and then call your mini-application from your script to give you your data.
If you are familiar with C, option 2 would be faster I think. Performance-wise it does not matter much, as the data is small (even on big systems) and the calls will most likely be very infrequent (as pool layouts do not change often).
This is my script:
#!/bin/bash
JOB_NUM=4
function checkJobNumber() {
if (( $JOB_NUM < 1 || $JOB_NUM > 16 )); then
echo "pass"
fi
}
...
checkJobNumber
...
When I try to launch the script I get the message:
./script.sh line 49: ((: < 1 || > 16 : syntax error: operand expected (error token is "< 1 || > 16 ")
(Please notice the spaces in the error message)
I really don't understand what the problem is. If I do the evaluation manually at the command line, it works.
Also, I tried different evaluations like if [[ "$JOB_NUM" -lt 1 -o "$JOB_NUM" gt 16 ]];... still no success.
UPDATE: As suggested I made few more attempts in the rest of the code outside the function call, and I found the problem.
My variables declaration was actually indented this way:
JOB_NUM= 4
THREAD_NUM= 8
.....
VERY_LONG_VAR_NAME= 3
and apparently this hoses the evaulation. BAH! It always worked for me before, so why doesn’t it now?
If I delete the white spaces, the evaluation works:
JOB_NUM=4
THREAD_NUM=8
....
VERY_LONG_VAR_NAME=3
OK I officially hate bash . . . sigh :(
this will work fine
#!/bin/bash
JOB_NUM=7
function checkJobNumber() {
if [ $JOB_NUM -lt 0 ] || [ $JOB_NUM -gt 16 ] ; then
echo "true"
else
echo "false"
fi
}
checkJobNumber
And if you want to check variable during tests you can write in your bash :
set -u
this will generate a message like "JOB_NUM: unbound variable" if variable is not well set
In my shell, I need check if a string is a valid IPv6 address.
I find two ways, neither of them is ideal enough to me.
One is http://twobit.us/2011/07/validating-ip-addresses/, while I wonder if it must be such complex for such a common requirement.
The other is expand ipv6 address in shell script, this is simple, but for major distribution of Linux, sipcalc isn't a common default utility.
So my question, is there a simple way or utility to validate a IPv6 address with shell?
Thanks in advance.
The code in the first link isn't particularly elegant, but modulo stylistic fixes, I don't think you can simplify much beyond that (and as indicated in a comment, it may already be too simple). The spec is complex and mandates a number of optional features, which is nice for the end user, but cumbersome for the implementor.
You could probably find a library for a common scripting language which properly encapsulates this logic in a library. My thoughts would go to Python, where indeed Python 3.3 includes a standard module called ipaddress; for older versions, try something like
#!/usr/bin/env python
import socket
import sys
try:
socket.inet_pton(socket.AF_INET6, sys.argv[1])
result=0
except socket.error:
result=1
sys.exit(result)
See also Checking for IP addresses
Here is a solution in POSIX compatible shell script that handles IPv4 and IPv6 addresses with an optional subnet mask. To test an IP that should not have a subnet mask just pass it a dummy one when performing the test. It seems like a lot of code but it should be significantly faster than using external programs like grep or scripts that are likely to fork.
Single IPv6 zero groups compressed to :: will be treated as invalid. The use of such representation is strongly discouraged, but technically correct. There is a note in the code explaining how to alter this behaivour if you wish to allow such addresses.
#!/bin/sh
set -e
# return nonzero unless $1 contains only digits, leading zeroes not allowed
is_numeric() {
case "$1" in
"" | *[![:digit:]]* | 0[[:digit:]]* ) return 1;;
esac
}
# return nonzero unless $1 contains only hexadecimal digits
is_hex() {
case "$1" in
"" | *[![:xdigit:]]* ) return 1;;
esac
}
# return nonzero unless $1 is a valid IPv4 address with optional trailing subnet mask in the format /<bits>
is_ip4() {
# fail if $1 is not set, move it into a variable so we can mangle it
[ -n "$1" ] || return
IP4_ADDR="$1"
# handle subnet mask for any address containing a /
case "$IP4_ADDR" in
*"/"* ) # set $IP4_GROUP to the number of bits (the characters after the last /)
IP4_GROUP="${IP4_ADDR##*"/"}"
# return failure unless $IP4_GROUP is a positive integer less than or equal to 32
is_numeric "$IP4_GROUP" && [ "$IP4_GROUP" -le 32 ] || return
# remove the subnet mask from the address
IP4_ADDR="${IP4_ADDR%"/$IP4_GROUP"}";;
esac
# backup current $IFS, set $IFS to . as that's what separates digit groups (octets)
IP4_IFS="$IFS"; IFS="."
# initialize count
IP4_COUNT=0
# loop over digit groups
for IP4_GROUP in $IP4_ADDR ;do
# return failure if group is not numeric or if it is greater than 255
! is_numeric "$IP4_GROUP" || [ "$IP4_GROUP" -gt 255 ] && IFS="$IP4_IFS" && return 1
# increment count
IP4_COUNT=$(( IP4_COUNT + 1 ))
# the following line will prevent the loop continuing to run for invalid addresses with many occurrences of .
# this makes no difference to the result, but may improve performance when validating many such invalid strings
[ "$IP4_COUNT" -le 4 ] || break
done
# restore $IFS
IFS="$IP4_IFS"
# return success if there are 4 digit groups, otherwise return failure
[ "$IP4_COUNT" -eq 4 ]
}
# return nonzero unless $1 is a valid IPv6 address with optional trailing subnet mask in the format /<bits>
is_ip6() {
# fail if $1 is not set, move it into a variable so we can mangle it
[ -n "$1" ] || return
IP6_ADDR="$1"
# handle subnet mask for any address containing a /
case "$IP6_ADDR" in
*"/"* ) # set $IP6_GROUP to the number of bits (the characters after the last /)
IP6_GROUP="${IP6_ADDR##*"/"}"
# return failure unless $IP6_GROUP is a positive integer less than or equal to 128
is_numeric "$IP6_GROUP" && [ "$IP6_GROUP" -le 128 ] || return
# remove the subnet mask from the address
IP6_ADDR="${IP6_ADDR%"/$IP6_GROUP"}";;
esac
# perform some preliminary tests and check for the presence of ::
case "$IP6_ADDR" in
# failure cases
# *"::"*"::"* matches multiple occurrences of ::
# *":::"* matches three or more consecutive occurrences of :
# *[^:]":" matches trailing single :
# *"."*":"* matches : after .
*"::"*"::"* | *":::"* | *[^:]":" | *"."*":"* ) return 1;;
*"::"* ) # set flag $IP6_EXPANDED to true, to allow for a variable number of digit groups
IP6_EXPANDED=0
# because :: should not be used for remove a single zero group we start the group count at 1 when :: exists
# NOTE This is a strict interpretation of the standard, applications should not generate such IP addresses but (I think)
# they are in fact technically valid. To allow addresses with single zero groups replaced by :: set $IP6_COUNT to
# zero after this case statement instead
IP6_COUNT=1;;
* ) # set flag $IP6_EXPANDED to false, to forbid a variable number of digit groups
IP6_EXPANDED=""
# initialize count
IP6_COUNT=0;;
esac
# backup current $IFS, set $IFS to : to delimit digit groups
IP6_IFS="$IFS"; IFS=":"
# loop over digit groups
for IP6_GROUP in $IP6_ADDR ;do
# if this is an empty group then increment count and process next group
[ -z "$IP6_GROUP" ] && IP6_COUNT=$(( IP6_COUNT + 1 )) && continue
# handle dotted quad notation groups
case "$IP6_GROUP" in
*"."* ) # return failure if group is not a valid IPv4 address
# NOTE a subnet mask is added to the group to ensure we are matching addresses only, not ranges
! is_ip4 "$IP6_GROUP/1" && IFS="$IP6_IFS" && return 1
# a dotted quad refers to 32 bits, the same as two 16 bit digit groups, so we increment the count by 2
IP6_COUNT=$(( IP6_COUNT + 2 ))
# we can stop processing groups now as we can be certain this is the last group, : after . was caught as a failure case earlier
break;;
esac
# if there are more than 4 characters or any character is not a hex digit then return failure
[ "${#IP6_GROUP}" -gt 4 ] || ! is_hex "$IP6_GROUP" && IFS="$IP6_IFS" && return 1
# increment count
IP6_COUNT=$(( IP6_COUNT + 1 ))
# the following line will prevent the loop continuing to run for invalid addresses with many occurrences of a single :
# this makes no difference to the result, but may improve performance when validating many such invalid strings
[ "$IP6_COUNT" -le 8 ] || break
done
# restore $IFS
IFS="$IP6_IFS"
# if this address contained a :: and it has less than or equal to 8 groups then return success
[ "$IP6_EXPANDED" = "0" ] && [ "$IP6_COUNT" -le 8 ] && return
# if this address contained exactly 8 groups then return success, otherwise return failure
[ "$IP6_COUNT" -eq 8 ]
}
Here are some tests.
# tests
TEST_PASSES=0
TEST_FAILURES=0
for TEST_IP in 0.0.0.0 255.255.255.255 1.2.3.4/1 1.2.3.4/32 12.12.12.12 123.123.123.123 101.201.201.109 ;do
! is_ip4 "$TEST_IP" && printf "IP4 test failed, test case '%s' returned invalid\n" "$TEST_IP" && TEST_FAILURES=$(( TEST_FAILURES + 1 )) || TEST_PASSES=$(( TEST_PASSES + 1 ))
done
for TEST_IP in ::1 ::1/128 ::1/0 ::1234 ::bad ::12 1:2:3:4:5:6:7:8 1234:5678:90ab:cdef:1234:5678:90ab:cdef \
1234:5678:90ab:cdef:1234:5678:90ab:cdef/127 1234:5678:90ab::5678:90ab:cdef/64 f:1234:c:ba:240::1 \
1:2:3:4:5:6:1.2.3.4 ::1.2.3.4 ::1.2.3.4/0 ::ffff:1.2.3.4 ;do
! is_ip6 "$TEST_IP" && printf "IP6 test failed, test case '%s' returned invalid\n" "$TEST_IP" && TEST_FAILURES=$(( TEST_FAILURES + 1 )) || TEST_PASSES=$(( TEST_PASSES + 1 ))
done
for TEST_IP in junk . / 0 -1.0.0.0 1.2.c.0 a.0.0.0 " 1.2.3.4" "1.2.3.4 " " " 01.0.0.0 09.0.0.0 0.0.0.01 \
0.0.0.09 0.09.0.0.0 0.01.0.0 0.0.01.0 0.0.0.a 0.0.0 .0.0.0.0 256.0.0.0 0.0.0.256 "" 0 1 12 \
123 1.2.3.4/s 1.2.3.4/33 1.2.3.4/1/1 ;do
is_ip4 "$TEST_IP" && printf "IP4 test failed, test case '%s' returned valid\n" "$TEST_IP" && TEST_FAILURES=$(( TEST_FAILURES + 1 )) || TEST_PASSES=$(( TEST_PASSES + 1 ))
done
for TEST_IP in junk "" : / :1 ::1/ ::1/1/1 :::1 ::1/129 ::12345 ::bog ::1234:345.234.0.0 ::sdf.d ::1g2 \
1:2:3:44444:5:6:7:8 1:2:3:4:5:6:7 1:2:3:4:5:6:7:8/1c1 1234:5678:90ab:cdef:1234:5678:90ab:cdef:1234/64 \
1234:5678:90ab:cdef:1234:5678::cdef/64 ::1.2.3.4:1 1.2.3.4:: ::1.2.3.4j ::1.2.3.4/ ::1.2.3.4:junk ::1.2.3.4.junk ;do
is_ip6 "$TEST_IP" && printf "IP6 test failed, test case '%s' returned valid\n" "$TEST_IP" && TEST_FAILURES=$(( TEST_FAILURES + 1 )) || TEST_PASSES=$(( TEST_PASSES + 1 ))
done
printf "test complete, %s passes and %s failures\n" "$TEST_PASSES" "$TEST_FAILURES"
Most distros come with package iproute2 (name may vary) preinstalled. So you can rely on the command ip for querying the routing table:
ip -6 route get <probe_addr>/128 >/dev/null 2>&1
Even on a machine without appropriate route this delivers rc=0 when the probe is in valid v6-syntax.
valid_ip(){
ip -6 route get "$1"/128 >/dev/null 2>&1
case "$?" in
0|2) return 0
1) return 1
esac
}
i took sgundlach's answer, but needed it on a machine which did not have ip6 connectivity so through testing and reading the manpage I found that I can trust exit code 1 to mean the syntax is invalid, meanwhile 0 is success and 2 is valid syntax but kernel error reported.
from the man page:
Exit status is 0 if command was successful, and 1 if there is a syntax error. If an error was reported by the kernel exit status is 2.
I have this non-functioning script, before I can attempt to fix it I need to know what kind of shell this was supposed to run in.
#
set pwrs = ( 0 0 0 0 0 0 0 0 )
# pwrs[1]=1
# next=2
while ( $next < 9 )
# last = $next - 1
# pwrs[$next] = $pwrs[$last] * 2
# next = $next + 1
end
#count = 1
while ( $count <= 8 )
echo $pwrs[$count]
# count = $count + 1
end
I was able to get this to produce appropriate output using csh. I would expect tcsh to work as well.
Turns out I do have a csh available. I pasted the code into the cmdline.
There was one error. All math calculations that begin with the # char must be separated from their following variable name, hence
#count = 1
Needed to be fixed as
# count = 1
$ set pwrs = ( 0 0 0 0 0 0 0 0 )
$ # pwrs[1]=1
$ # next=2
$ while ( $next < 9 )
while? # last = $next - 1
while? # pwrs[$next] = $pwrs[$last] * 2
while? # next = $next + 1
while? end
$ # count = 1
$ while ( $count <= 8 )
while? echo $pwrs[$count]
while? # count = $count + 1
while? end
output
1
2
4
8
16
32
64
128
[oracle#localhost ~]$
See Grymoire Unix - Csh for explanation of the csh #, and $pwrs[$next] (array notation). Your specific case is not addressed, but you should be able to work it out. If you have other questions after you build small test cases that don't work, post them with sample inputs, expected outputs, and your current code and output.
Also, don't spend any more time on csh, while not as bad as some portray it, you will ultimately discover that there are problems you can't solve in csh. All of it's nice cmd-line features are included in bash, so you might want to consider using it.Finally, see Grymoire csh top 10 for detailed reasons why you want to convert this code to a newer shell.
IHTH