In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what is the operation method of Shell string". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
There are many operations on strings in Shell. Combined with code examples, it will be easier to understand.
Concatenate strings by single quotation marks
# concatenate the strings # name1='white'str1='hello,'${name1}''str2='hello, ${name1}' echo ${str1} _ ${str2} # Output:# hello, white_hello, ${name1} name3='green'str5='hello,'$ {name3}''str6='hello in single quotes ${name3} 'echo ${str5} _ ${str6}
The running results are as follows:
Hello, white_hello, ${name1} hello,green_hello, ${name3}
Concatenate strings by double quotation marks
# concatenate the strings # name2= "black" str3= "hello," ${name2} "" str4= "hello, ${name2}" echo ${str3} _ ${str4} # Output:# hello, black_hello, blackname4='pink'str7= "hello," ${name4} "" str8= "hello, ${name4}" echo ${str7} _ ${str8}
The running results are as follows:
Hello, black_hello, blackhello, pink_hello, pink
Get string length information
# get string length # text= "12345" echo "${text} length is: ${# text}" # Output:# 12345 length is: 5text1 = "qwert" echo "${text1} length is: ${# text1}" # get substring text= "12345" echo ${text:2:2} # Output:# 34text= " Asdfghjkl "echo ${text:3:4} # fghj
The running results are as follows:
12345 length is: 5qwert length is: 534fghj
Find the index of the corresponding substring in the string, and the index value starts at 1.
# find the index corresponding to the substring # text= "hello" echo `expr index "${text}" ll` # Output:# "index starts with 1 to record string_test=" linux world "echo `expr index" ${string_test} "w` # 7
The running results are as follows:
thirty-seven
Logically determine whether a string contains substrings
# determine whether the string contains the substring # str= "new_feature/" result=$ (echo "${str}" | grep "feature/") if [["$result"! = ""]] Then echo "feature/ is a substring of ${str}" else echo "feature/ is not a substring" fi of ${str} "
The running results are as follows:
Feature/ is a substring of new_feature/
Shell gets the content to the right of a string-specific pattern match
# intercepting the content to the right of the keyword # full_branch= "feature/1.0.0" branch= `echo ${full_branch#feature/} `echo "branch is ${branch}" full_name= "aaa_bbb" right_half= `echo ${full_name#aaa_} `echo "right half of ${full_name} is ${right_half}"
The running results are as follows:
Branch is 1.0.0right half of aaa_bbb is bbb
Shell gets the content to the left of a string-specific pattern match
# intercept the content to the left of the keyword # full_version= "0.0.1-SNAPSHOT" version= `echo ${full_version%-SNAPSHOT} `echo "version is ${version}" full_address= "california-kk" left_address= `echo ${full_address%-kk} `echo "left_address is ${left_address}"
The running results are as follows:
Version is 0.0.1left_address is california
Split a string into an array
# string is divided into an array # str= "0.0.0.1" OLD_IFS= "$IFS" IFS= "." array= (${str}) IFS= "$OLD_IFS" size=$ {# array [*]} lastIndex= `expr ${size}-1`echo "array length: ${size}" echo "last array element: ${array [${lastIndex}]} "for item in ${array [@]} do echo" $item "doneip_address=" 192.168.1.1 "OLD_IFS=" $IFS "IFS=". "array= (${ip_address}) IFS=" $OLD_IFS "ip_size=$ {# array [*]} lastIndex= `expr ${ip_size}-1`fi rstIndex= `expr ${ip_size}-4`echo" IP address Array length: ${ip_size} "echo" IP address The first paragraph corresponds to: ${array [${firstIndex}]} "for element in ${array [@]} do echo ${element} done
The running results are as follows:
Array length: 4 Last array element: array length corresponding to 10001IP address: the first segment of 4IP address corresponds to: 192 1916811
Logic determines whether the string is empty
# determine whether the string is empty #-n determine whether the length is non-zero #-z determine whether the length is zero STR = testingstr2=''if [[- n "$str"]] then echo "The string $str is not empty" else echo "The string $str is empty" fiif [[- n "$str2" ] then echo "The string $str2 is not empty" else echo "The string $str2 is empty" fiif [[- z $str2]] then echo "= = The string $str2 is empty==" else echo "The string $str2 is not empty" fi# Output:# The string testing is not empty# The string is empty
The running results are as follows:
The string testing is not emptyThe string is empty==The string is empty==
String comparison logic
# string comparison # str=hellostr2=worldif [[$str= "hello"]]; then echo "str equals hello" else echo "str not equals hello" fiif [[$str2= "hello"]]; then echo "str2 equals hello" else echo "str2 not equals hello" fistr3=linuxif [[$str3= "linux"]] Then echo "str3 equals linux" else echo "str3 not equal linux" fi
The running results are as follows:
This is the end of the introduction to str equals hellostr2 not equals hellostr3 equals linux's "what is the operation method related to Shell strings". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.