# Linux Shell splits strings into groups
result=$ (facter | awk '/ipaddress/&&!/ipaddress_lo/{print $ "$ $} ') array= ($result)
# Determine if a variable exists (not to determine whether it is empty)
If [-Z ${var+x}]; Then echo "Var is unset"; else echo "Var is set to ' $var '"; Fi
# Determine if a variable is empty
If ["$var x" = "x"]; Then echo "Var is empty"; else echo "Var is set to ' $var '"; fiif [-Z $var]; Then echo "Var is empty"; else echo "Var is set to ' $var '"; Fi
#系统变量用后还原
# Definition of IFS: ifs,internal Field Separator
# an Internal Field Separator (IFS) is a environment variable that stores delimiting characters.
# It's The default delimiter string used by a running shell environment.
# "$*" expands as "$1c$2c$3", where C is the first character of IFS
# When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated b Y the first character of the IFS special variable.
# that's, "$*" is equivalent to "$1c$2c ...", where C is the first char‐acter of the value of the IFS variable.
# If IFS is unset, the parameters was separated by spaces. If IFS is null, the parameters is joined without intervening separators.
oldifs= "$IFS" ifs= "" array= ($result) ifs= "$oldIFS" for I in ${array[@]}; Doecho $idone
# Use Facter to get a set of Key-value
# Facter output has a newline character, you must replace the newline character with a space
# Replace newline characters with spaces to use awk or sed
# awk-v rs= "" ' {gsub ("\ n", "");p rint} '
# ECHO-E "2 \n1" | Sed ': A; n;$!ba;s/\n//g '
result=$ (facter | awk '/ipaddress/&&!/ipaddress_lo/{print "" $ $} ' | Awk-v rs= "" ' {gsub ("\ n", "");p Rint} ') array= ($result) array_length=${#array [@]}
# Output key
for ((i = 0; i < $array _length; i=i+2)); Doecho ${array[$i]}done
# output value
for ((i = 1; i < $array _length; i=i+2)); Doecho ${array[$i]}done
# Output Key-value
for ((i = 0; i < $array _length; i=i+2)); doj= $i +1echo "${array[$i]}-${array[$j]}" done
--end--
This article is from "Communication, My Favorites" blog, please make sure to keep this source http://dgd2010.blog.51cto.com/1539422/1718284
A set of Linux Shell scripting little Exercises