Shell字符串相关操作方法是什么
发表于:2025-02-23 作者:千家信息网编辑
千家信息网最后更新 2025年02月23日,本篇内容介绍了"Shell字符串相关操作方法是什么"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!Sh
千家信息网最后更新 2025年02月23日Shell字符串相关操作方法是什么
本篇内容介绍了"Shell字符串相关操作方法是什么"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
Shell中针对字符串的操作有很多。结合代码示例会比较容易理解
通过单引号拼接字符串
################### 使用单引号拼接字符串 ###################name1='white'str1='hello, '${name1}''str2='hello, ${name1}'echo ${str1}_${str2}# Output:# hello, white_hello, ${name1}name3='green'str5='hello,'${name3}''str6='hello, ${name3}'echo ${str5}_${str6}
运行结果如下:
hello, white_hello, ${name1}hello,green_hello, ${name3}
通过双引号拼接字符串
################### 使用双引号拼接字符串 ###################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}
运行结果如下:
hello, black_hello, blackhello, pink_hello, pink
获取字符串长度信息
################### 获取字符串长度 ###################text="12345"echo "${text} length is: ${#text}"# Output:# 12345 length is: 5text1="qwert"echo "${text1} length is: ${#text1}"# 获取子字符串text="12345"echo ${text:2:2}# Output:# 34text="asdfghjkl"echo ${text:3:4}#fghj
运行结果如下:
12345 length is: 5qwert length is: 534fghj
查找字符串中对应的子串的索引,索引值从1开始计数。
################### 查找子字符串对应的索引 ###################text="hello"echo `expr index "${text}" ll`# Output:# 3# 索引从 1 开始记录string_test="linux world"echo `expr index "${string_test}" w`# 7
运行结果如下:
37
逻辑判断字符串中是否包含子字符串
################### 判断字符串中是否包含子字符串 ###################str="new_feature/"result=$(echo "${str}" | grep "feature/")if [[ "$result" != "" ]]; then echo "feature/ 是 ${str} 的子字符串"else echo "feature/ 不是 ${str} 的子字符串"fi
运行结果如下:
feature/ 是 new_feature/ 的子字符串
shell获取字符串特定模式匹配右边的内容
################### 截取关键字右边内容 ###################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}"
运行结果如下:
branch is 1.0.0right half of aaa_bbb is bbb
shell获取字符串特定模式匹配左边的内容
################### 截取关键字左边内容 ###################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}"
运行结果如下:
version is 0.0.1left_address is california
将字符串分割成数组
################### 字符串分割成数组 ###################str="0.0.0.1"OLD_IFS="$IFS"IFS="."array=( ${str} )IFS="$OLD_IFS"size=${#array[*]}lastIndex=`expr ${size} - 1`echo "数组长度:${size}"echo "最后一个数组元素:${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`firstIndex=`expr ${ip_size} - 4`echo "IP地址对应的数组长度:${ip_size}"echo "IP地址的第一段对应是:${array[${firstIndex}]}"for element in ${array[@]}do echo ${element}done
运行结果如下:
数组长度:4最后一个数组元素:10001IP地址对应的数组长度:4IP地址的第一段对应是:19219216811
逻辑判断字符串是否为空
################### 判断字符串是否为空 ####################-n 判断长度是否非零#-z 判断长度是否为零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 string testing is not emptyThe string is empty==The string is empty==
字符串比较逻辑
################### 字符串比较 ###################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
运行结果如下:
str equals hellostr2 not equals hellostr3 equals linux
"Shell字符串相关操作方法是什么"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
字符
字符串
结果
运行
数组
长度
内容
地址
引号
索引
逻辑
方法
元素
关键
关键字
右边
更多
模式
知识
实用
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
服务器属性管理
光遇服务器异常导致登录失败
遨游网络安全儿童画
数据库厂房照片
oracleplm软件开发
mysql连接数据库
软件开发标价表
粤版网络技术应用教案全套
软件开发项目经理的职责与权限
大数据与会计与数据库的关系
软件开发研究生大学
互联网的科技含义是什么
万达医疗系统软件开发
图书馆人文科学数据库
哪个服务器搭建发卡平台好
河南可视化人口管理系统软件开发
计算机网络技术与基础试题
安卓项目建立数据库连接
音乐节拍器软件开发者
网络安全+网络摄像头
事业单位软件开发试题
青少年网络安全文章
数据库功能模块结构
软件开发中数据总线的演进
猪价数据库
互联网企业排行商汤科技
普陀区提供软件开发服务要多少钱
信息和网络技术中心风险点
OPAC书目型数据库吗
怎么传递手机数据库