A set of Linux Shell scripting little Exercises

Source: Internet
Author: User

# 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.