shell字符串操作

Shell脚本计算字符串长度和判断字符串为空小技巧 一些需要注意的脚本问题 计算字符串长度可用的三种方法: 代码如下: echo “$str”|awk ‘{print length($0)}’ expr length “$str” echo “$str”|wc -c 但是第三种得出的值会多1,可能是把结束符也计算在内了 判断字符串为空的方法有三种: 代码如下: if [ “$str” =  “” ] if [ x”$str” = x ] if [ -z “$str” ] 注意:都要代双引号,否则有些命令会报错。 您可能感兴趣的文章:用Shell判断字符串包含关系的方法小结Shel 立即下载

在做shell批处理程序时候,经常会涉及到字符串相关操作。有很多命令语句,如:awk,sed都可以做字符串各种操作。 其实shell内置一系列操作符号,可以达到类似效果,大家知道,使用内部操作符会省略启动外部程序等时间,因此速度会非常的快。

 

一、判断读取字符串值

表达式含义
${var}变量var的值, 与$var相同
  
${var-DEFAULT}如果var没有被声明, 那么就以$DEFAULT作为其值 *
${var:-DEFAULT}如果var没有被声明, 或者其值为空, 那么就以$DEFAULT作为其值 *
  
${var=DEFAULT}如果var没有被声明, 那么就以$DEFAULT作为其值 *
${var:=DEFAULT}如果var没有被声明, 或者其值为空, 那么就以$DEFAULT作为其值 *
  
${var+OTHER}如果var声明了, 那么其值就是$OTHER, 否则就为null字符串
${var:+OTHER}如果var被设置了, 那么其值就是$OTHER, 否则就为null字符串
  
${var?ERR_MSG}如果var没被声明, 那么就打印$ERR_MSG *
${var:?ERR_MSG}如果var没被设置, 那么就打印$ERR_MSG *
  
${!varprefix*}匹配之前所有以varprefix开头进行声明的变量
${!varprefix@}匹配之前所有以varprefix开头进行声明的变量

加入了“*”  不是意思是: 当然, 如果变量var已经被设置的话, 那么其值就是$var.

[chengmo@localhost ~]$ echo ${abc-'ok'}
ok
[chengmo@localhost ~]$ echo $abc

[chengmo@localhost ~]$ echo ${abc='ok'}
ok
[chengmo@localhost ~]$ echo $abc
ok

 

如果abc 没有声明“=" 还会给abc赋值。

[chengmo@localhost ~]$ var1=11;var2=12;var3=
[chengmo@localhost ~]$ echo ${!v@}           
var1 var2 var3
[chengmo@localhost ~]$ echo ${!v*}
var1 var2 var3

 

${!varprefix*}与${!varprefix@}相似,可以通过变量名前缀字符,搜索已经定义的变量,无论是否为空值。

 

二、字符串操作(长度,读取,替换)

表达式含义
${#string}$string的长度
  
${string:position}在$string中, 从位置$position开始提取子串
${string:position:length}在$string中, 从位置$position开始提取长度为$length的子串
  
${string#substring}从变量$string的开头, 删除最短匹配$substring的子串
${string##substring}从变量$string的开头, 删除最长匹配$substring的子串
${string%substring}从变量$string的结尾, 删除最短匹配$substring的子串
${string%%substring}从变量$string的结尾, 删除最长匹配$substring的子串
  
${string/substring/replacement}使用$replacement, 来代替第一个匹配的$substring
${string//substring/replacement}使用$replacement, 代替所有 匹配的$substring
${string/#substring/replacement}如果$string的前缀 匹配$substring, 那么就用$replacement来代替匹配到的$substring
${string/%substring/replacement}如果$string的后缀 匹配$substring, 那么就用$replacement来代替匹配到的$substring
  

说明:"* $substring”可以是一个正则表达式 .

 

1.长度

[web97@salewell97 ~]$ test='I love china'
[web97@salewell97 ~]$ echo ${#test}
12

${#变量名}得到字符串长度

 

2.截取字串

[chengmo@localhost ~]$ test='I love china'
[chengmo@localhost ~]$ echo ${test:5}    
e china
[chengmo@localhost ~]$ echo ${test:5:10}
e china

${变量名:起始:长度}得到子字符串

 

3.字符串删除

[chengmo@localhost ~]$ test='c:/windows/boot.ini'
[chengmo@localhost ~]$ echo ${test#/}
c:/windows/boot.ini
[chengmo@localhost ~]$ echo ${test#*/}
windows/boot.ini
[chengmo@localhost ~]$ echo ${test##*/}
boot.ini

[chengmo@localhost ~]$ echo ${test%/*}
c:/windows
[chengmo@localhost ~]$ echo ${test%%/*}

${变量名#substring正则表达式}从字符串开头开始配备substring,删除匹配上的表达式。

${变量名%substring正则表达式}从字符串结尾开始配备substring,删除匹配上的表达式。

注意:${test##*/},${test%/*} 分别是得到文件名,或者目录地址最简单方法。

4.字符串替换

[chengmo@localhost ~]$ test='c:/windows/boot.ini'
[chengmo@localhost ~]$ echo ${test//////}
c:/windows/boot.ini
[chengmo@localhost ~]$ echo ${test///////}
c:/windows/boot.ini

 

${变量/查找/替换值} 一个“/”表示替换第一个,”//”表示替换所有,当查找中出现了:”/”请加转义符”//”表示。

三、性能比较

在shell中,通过awk,sed,expr 等都可以实现,字符串上述操作。下面我们进行性能比较。

[chengmo@localhost ~]$ test='c:/windows/boot.ini'                      
[chengmo@localhost ~]$ time for i in $(seq 10000);do a=${#test};done;           

real    0m0.173s
user    0m0.139s
sys     0m0.004s

[chengmo@localhost ~]$ time for i in $(seq 10000);do a=$(expr length $test);done;      

real    0m9.734s
user    0m1.628s

 

速度相差上百倍,调用外部命令处理,与内置操作符性能相差非常大。在shell编程中,尽量用内置操作符或者函数完成。使用awk,sed类似会出现这样结果。


转自 http://www.cnblogs.com/chengmo/archive/2010/10/02/1841355.html

 

 

用Arduino和MAX30102做个心率血氧仪,从硬件连线到算法调试全流程避坑 本文详细介绍了如何使用Arduino和MAX30102传感器构建高精度心率血氧仪,涵盖硬件连线、软件配置、信号处理及算法优化全流程。通过实战案例和避坑指南,帮助开发者快速实现临床级精度的健康监测设备,适用于可穿戴设备和医疗健康领域。 阅读详情

相关推荐

详解一典型的反激式开关电源方案

如图,在某系统方案上看到下图所示的单端反激式开关电源方案。

alala120的博客 1万+

SHELL判断字符串长度

ID=12345 if [ ${#ID} -eq 5 ];then     echo "5" fi

moyuanbomo的博客 1万+

手把手教你搭建三端口DC-DC变换器(含Boost+Buck电路实战)

本文详细介绍了如何从零开始搭建三端口DC-DC变换器,涵盖Boost和Buck电路的硬件设计、元器件选型、电路板布局、焊接技巧、控制系统设计与调试等关键步骤。通过实战案例和优化建议,帮助工程师避开常见陷阱,提升系统效率和稳定性,适用于光伏、储能等电力电子应用场景。

x8y9z0的博客 944

shell 字符串为空和长度的判断

计算字符串长度可用的4种方法:  (1)echo “$str”|awk '{print length($0)}'  (2)expr length “$str”  (3)echo “$str”|wc -c  但是第三种得出的值会多1,可能是把结束符也计算在内了    array 为数组 length

chengxuyuanyonghu的专栏 1848

Ubuntu22.04如何安装cuDNN8.9.7

本文详细介绍了在Ubuntu 22.04系统上为CUDA 12.2安装适配cuDNN 8.9.7的完整流程,特别针对RTX A6000显卡。主要内容包括:1)选择与CUDA 12.2兼容的cuDNN版本;2)使用deb包方式的安装步骤(添加仓库、导入密钥、安装组件);3)通过MNIST示例验证安装;4)版本锁定、卸载旧版等补充说明;5)性能测试方法。文中强调了版本匹配的重要性,并提供了多卡配置建议,为搭建深度学习开发环境提供完整指导。

2501_93651177的博客 1848

Vue3 - [兼容PC和手机H5] 详细监听浏览器刷新 / 关闭 / 前进后退事件,用户点击关闭和刷新页面前 “拦截“ 操作并弹出提示框(实时监听用户关闭或刷新网页,触发时文字提醒并执行自定义操作)

vue3监听页面关闭和刷新,vue3中如何监听页面刷新和关闭事件,vue3中监听浏览器窗口关闭事件,并在窗口关闭进行操作,vue3怎么只在某个页面监听,其他页面不监听,vue3监听刷新和关闭怎么离开页面时注销释放,vue3怎么阻止浏览器前进和后退并提示,页面跳转离开后依旧可以监听到刷新和关闭,vue3怎么知道用户关闭了网页,vue3如何监听刷新网页操作并截断,vue3移动端手机h5网页手机网站监听浏览器刷新关闭,vue3当页面关闭或刷新时怎么提示用户离开不保存数据,vue3项目监听浏览器窗口或标签页关闭,并

🏆 前端领域优质创作者 1万+

shell判断字符串长度是否大于0

shell判断字符串长度是否大于0: 这个程序是一个简单的ssh下载程序: if [ -n “$1” ] //判断字符串$1长度是否大于0, then scp wyz@192.168.2.105:/home/wyz/sshdir/$1 . else echo “Usage: $0 filename” fi -n判断字符串长度是否大于0,是的话返回真,注意变量要加"",例如 “$1” ...

qq_33472414的博客 8447

Unity零基础到进阶 ☀️| Unity中的 GC及优化 超级全面解析 ☆(ゝω・)v 建议收藏!

介绍: 在游戏运行的时候,数据主要存储在内存中,当游戏的数据在不需要的时候,存储当前数据的内存就可以被回收以再次使用。内存垃圾是指当前废弃数据所占用的内存,垃圾回收(GC)是指将废弃的内存重新回收再次使用的过程。 Unity中将垃圾回收当作内存管理的一部分,如果游戏中废弃数据占用内存较大,则游戏的性能会受到极大影响,此时垃圾回收会成为游戏性能的一大障碍点。 本文我们主要学习垃圾回收的机制,垃圾回收如何被触发以及如何提GC收效率来提高游戏的性能。 Unity内存管理机制简介 要想了解垃圾回收如何工作以及何时被

努力前行,总会成为自己心中的那道光 6万+

Linux中的shell(二)

(一)shell算数运算1.expr:是一个表达式处理命令,可以执行简单的整数运算,有‘+,-,*,、,%’#a=5;b=6#a='expr $a + 1'#echo $a62.let:不需要在前面加“$”,但必须将单个或带有空格的表达式用双引号引起来。#x=100#let x=x+1#echo x1013.浮点算数运算使用bc,awk命令下例为使用bc命令计算193/2的结果#n='echo "...

薛定谔的博客 497

4、Vitis_HLS、Vivado 和 Vitis IDE 工具的设置与使用

本文详细介绍了使用Vitis_HLS、Vivado和Vitis IDE进行FPGA开发的完整流程,涵盖开发板选择、软件安装、IP设计与模拟、综合、导出以及在FPGA上的实现与验证。以Xilinx Artix-7和Zynq系列开发板为例,指导用户从环境搭建到最终硬件验证,完成一个32位加法器IP的开发,适用于嵌入式系统和硬件加速项目的学习与实践。

fff88的博客 184

【C语言】字符常量详解

字符常量是C语言中处理字符数据的重要工具。通过单引号括起一个字符,我们可以方便地使用字符常量进行字符判断、字符运算和字符串处理等操作。理解字符常量的表示方法、使用场景和ASCII码对应关系,对于编写高效的C语言程序至关重要。

LuckiBit 5028

Shell脚本中替换字符串等操作

一、判断读取字符串值 表达式 含义 ${var} 变量var的值, 与$var相同     ${var-DEFAULT} 如果var没有被声明, 那么就以$DEFAULT作为其值 * ${var:-DEFAULT} 如果va...

心飞路漫的博客 2万+

STM32入门教程:智能储物柜控制

在这个例子中,我们将使用一个4x4的储物柜,共有16个储物格。我们了解了硬件准备、储物柜电路设计、STM32的基本配置、储物柜控制代码的编写以及测试和调试等方面的知识。根据我们的电路设计,我们需要将一些引脚配置为输出,并将其他引脚配置为输入。在本教程中,我们将使用STM32微控制器来实现一个基本的智能储物柜控制系统。在主函数中,我们将不断循环读取传感器数据,并根据数据的变化来控制储物柜的开关和显示相关信息。这个函数将读取人体感应传感器、温湿度传感器和光照传感器的数据,并将数据存储到相应的变量中。

m0_75244442的博客 668

shell 字符串操作

用shell自带的分割数组功能(IFS),将字符串分割成数组将字符串”one,two,three,four”,以逗号为分割符分割为数组。#!/bin/bash a="one,two,three,four"#变量$IFS存储着分隔符,将默认的分隔符备份到OLD_IFS OLD_IFS="${IFS}"#将分隔符设置为逗号“,” IFS=","#将字符串a分割为数组arr,圆括号() arr=(${a}

Elena专栏 781

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

3.shell字符串操作

1.shell字符串的使用 字符串是shell编程中最常用最有用的数据类型。字符串可以用单引号,也可以用双引号,也可以不用引号 #!/bin/bash #单引号 str='Cat' echo ${str} //输出:Cat str1='${str}' echo ${str1} //输出:${str} #双引号 str2="Cat" echo ${str2} /...

扛麻袋的少年的博客 2088

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

Shell脚本之字符串操作

文章目录前言一、字符串获取二、字符串截取1. #号截取(自左向右)2. ##号截取(自左向右)3. %号截取(自右向左)4. %%号截取(自右向左)三、字符串终端打印1.直接打印内容2. 打印string参考链接 前言 本文主要介绍下Shell脚本中有关字符串的一些操作。 一、字符串获取 示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。 二、字符串截取 假设string = http://baidu.com/563.htm 1. #号截取(自左向右) echo .

xinfeismile123的博客 3458

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

linux shell 字符串操作详解

在做shell批处理程序时候,经常会涉及到字符串相关操作。有很多命令语句,如:awk,sed都可以做字符串各种操作。 其实shell内置一系列操作符号,可以达到类似效果,大家知道,使用内部操作符会省略启动外部程序等时间,因此速度会非常的快。 一、判断读取字符串值 表达式 含义 ${var} 变量var的值, 与$var相同 ${var-DEFAULT} 如果var没有被声明, 那么就以$DEFAULT作为其值 * ${var:-DEFAULT} 如果var没有被声明, 或者其值为空,

时有限 969

Shell_Shell 脚本中字符串的相关操作

在我们的shell 编程中,一个必不可少的操作就是针对于字符串的操作,重要有字符串替换,计算字符串长度 等等。。。原文地址:https://blog.csdn.net/github_33736971/article/details/53980123下面对这些操作进行一个总结。${#string} 返回$string的长度${string:position} 在$string中,从$position...

迎难而上 1万+

linux shell 字符串操作…

一、判断读取字符串值http://justcoding.iteye.com/blog/1963463 表达式 含义 ${var} 变量var的值, 与$var相同     ${var-DEFAULT} 如果var没有被声明, 那么就以$DEFAULT作为其值 * ${var:-DEFAULT} 如果var没有被声明, 或者其值为空, 那么就以$DEFAULT作为其

编程之路 410

Shell编程—【01】shell中常用的字符串操作

计算字符串长度 ${#string} expr length “$string” v1=="i love you" len=`expr length "$v1"` echo len 获取子串中字符在字符串中的位置 expr index “string&quot;&quot;string&quot; &quot;string""subString” v1="...

苡仁 294

Shell 字符串操作

Shell字符串操作小结 1. 取长度 str="abcd" expr length $str # 4 echo ${#str} # 4 expr "$str" : ".*" # 4 好像一般使用第二种 2. 查找子串的位置 str="abc" expr index $str "a" # 1 expr index $str "b" # 2 expr index $str "x" # 0 expr index $str "" #...

mary0712的博客 251

Shell字符串操作

拼接、截取、长度

荣耀之路 1636

shell对参数操作 字符串操作

<br />#cat test.sh #!/bin/sh echo '"$@" output.....' for i in "$@" do echo $i done echo '"$*" output ....' for i in "$*" do echo $i done<br /> 输出结果<br />#sh test.sh a b c d "$@" output..... a b c d "$

hqbobo的专栏 870

shell下字符串的相关操作

计算字符串长度: [shentao@vps ~]$ string=abcABC123ABCabc [shentao@vps ~]$ echo ${#string} 15 [shentao@vps ~]$ echo `expr length $string` 15 [shentao@vps ~]$ echo `expr "$string" : '.*'` 15感觉最后一种是很奇怪的写法。

sailershen的专栏 680

Shell: 字符串操作

转载:http://www.cnblogs.com/dwdxdy/archive/2012/07/25/2608257.html,谢谢! Shell内置的一系列字符串操作符号.具体的相关操作符号介绍如下: 表达式 含义 ${#string} $string的长度     ${string

加油,码农! 970
上一篇: 博客收藏
下一篇: shell的一些命令
wind19
博客等级 码龄23年 477粉丝 170原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值