在shell中求长度的几种常见方式

Linux shell编程之 - 获得一个变量的长度 按你脚本的逻辑,第一次进入循环的是非法串长,在循环体内又没有一条语句来修改这个非法串长就成了死循环,所以还是要将read name。哦,是的,当时代码没有没有写完整,而我自己用的是完整的,所以就想当然以为这段也没错了。按照你的脚本的逻辑,如果首次输入的长度不是6是会死循环的呀。前面的朋友说得没错,会死循环,变量$length应该在循环里面重新赋值!这样name变量的长度就读到asd变量了,但是会比实际的长度多1。不会啊,循环里面是交互的啊,只要符合条件就可以退出循环的。其中`号为“1”键左边的键。 阅读详情

<1> 使用$#直接求解
echo ${#变量名}

[root@localhost SHELL]# TE=muyingshiguang
[root@localhost SHELL]# echo ${#TE}
14
[root@localhost SHELL]# 

<2>使用管道符与wc的组合求解
echo $变量名 | wc -L

[root@localhost SHELL]# echo $TE | wc -L
14
[root@localhost SHELL]# 

<3>使用expr命令求解
expr length “$TE”

[root@localhost SHELL]# expr length "$TE"
14
[root@localhost SHELL]#

<4>使用awk求解
echo $TE | awk ‘{print length($0)}’

[root@localhost SHELL]# echo $TE | awk '{print length($0)}'
14
[root@localhost SHELL]# 
一步一步搭建S32K14x的AutoSar项目----MCAL工程创建及说明 一步一步搭建S32K14X的Autosar项目--MCAL工程创建及说明 阅读详情

相关推荐

周立功CAN盒二次开发实录:用QT5.1.0解析汽车CAN报文(附数据可视化技巧)

本文详细介绍了基于QT5.1.0和周立功USBCAN-E-U设备进行汽车CAN总线二次开发的实战过程。核心内容包括利用CANdb++数据库解析原始报文并转换为物理值,以及通过QCustomPlot库实现高性能实时数据可视化,为车载诊断与车联网开发提供了一套完整的工程实践方案。

sugar的博客 1057

shell变量计算长度及加减运算方法总结

linux shell 变量定义,以及变量的输出,在shell编写中常常需要计算变量的长度,以及数字变量直接进行加减操作变量定义 shell变量没有整形,字符串,浮点型等其余编程语言定义的数据类型[root@mytest ~]# str=stringstr是一个变量 变量输出[root@mytest ~]# echo $str string 或: [root@mytest ~]# echo ${

comprel的博客 1万+

Mac和VScode配置fortran

macOS系统 vscode fortran gfortran 终端

weixin_43047707的博客 2409

Shell中计算字符串长度的5种方法

一、使用 wc -L 命令 wc -L(一定是大写L)可以获取到当前行的长度,因此对于单独行的字符串可以用这个简单的方法获取;另外 wc -l(一定是小写的l)则是获取当前字符串内容的行数 echo "abc" | wc -L #结果为 3 表示长度 echo "abc" | wc -l #结果为1 表示行数 二、使用 expr length string 命令 expr length "abc" #结果为 3 三、通过 awk+length 的方式获取字符串长度 echo "abc" | awk '{

sinat_34241861的博客 8993

从零到一:Firefly-RK3399上的Ubuntu镜像构建与GPU加速实战

本文详细介绍了在Firefly-RK3399开发板上从零构建深度优化的Ubuntu系统镜像的全过程,包括交叉编译环境配置、内核定制、GPU加速环境部署(Mesa Panfrost驱动、OpenCL/Vulkan支持)以及深度学习推理优化(TFLite + LinuxNN)。通过实战案例展示了如何充分发挥Mali-T860 GPU的硬件潜力,提升边缘AI应用的性能和能效。

pz8901234的博客 1019

指纹对抗指南:如何绕过Cloudflare与Akamai的5s盾与浏览器指纹检测

现在的CDN反爬技术虽然越来越强,但是道高一尺魔高一丈,只要掌握了指纹伪造、流量伪装、行为模拟这些核心技术,就可以绕过绝大多数的防护措施。对抗过程中要注意效率和成本的平衡,小规模爬取用Playwright+stealth最方便,大规模爬取用curl_cffi+代理池性价比最高,遇到验证码用打码平台或者自动识别服务,不需要死磕破解。本文介绍的方案已经在实战中验证过,可以绕过95%以上的Cloudflare/Akamai防护,成功率非常高。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 295

Shell中获取字符串长度的七种方法

求字符串操作在shell脚本中很常用,下面归纳、汇总了求字符串的几种可能方法: 【方法一】:利用${#str}来获取字符串的长度 【方法二】:利用awk的length方法 备注: 1) 最好用{}来放置变量 2) 也可以用length($0)来统计文件中每行的长度 【方法三】:利用awk的NF项来获取字符串长度 备注: -F为分隔符,NF为域的个数,即单行字符串的长度 【方法四】:利用wc的-L参数来获取字符串的长度 备注: -L参数 1) 对多行文件来说,表示打印最长行的长

chenpuo的博客 1万+

【瑞芯微px30】【RTL8363】【一】驱动移植-网卡驱动概述

linux rtl8363驱动移植

weixin_34799243的博客 1179

linux--shell---判断参数大小

写一个脚本,传入两个数字,对数字大小进行比对。要求对参数个数进行校验。 #定义变量 num1=$1 num2=$2 #判断是否输入两个参数,是的话将两个参数传递给下一条指令 #若不是两个参数,则打印输出内容输出并且退出exit脚本不执行下一个指令。 if [ $# -ne 2 ] ;then echo '请输入两位数字' exit fi if [ $num1 -gt $num2 ] ; ...

weixin_44812034的博客 4571

Opencv计算机视觉PPT-算法篇

01 02 03 04 05 06 07 08 09 010 011 012 013 014 015 016 017 018

Qt历险记,专注Qt技术,三年医疗设备研发经验,1年机器视觉研发经验,半年柔性传感研发经验。 203

Android开发工程师面试问题与参考答案

这是一份Android开发工程师职位面试指南,主要针对自研OS开发岗位。岗位要求3年以上独立开发经验,熟悉Android平台UI设计、四大组件和Java基础。面试问题覆盖五大核心领域:基础知识、开发经验、技术深度、问题解决和性能优化。文章提供了典型面试问题及专业参考答案,帮助应聘者准备关键技术点,如多线程处理、HashMap与ConcurrentHashMap区别、Handler机制等。同时建议应聘者结合项目经验展示优化能力,并注意公司提供完善的薪酬福利和团队文化。

郑伟强dev的专栏 892

Linux拓展之Shell中求字符串的长度

在 shell 脚本中,有时候需要去求字符串的长度。如下展示了常见求字符串长度的方法。

二木成林 3879

土壤数据库辅助工具SPAW计算土壤导水率

首先点击打开根据之前的1比100土壤数据查表得到各个组分含量其中Field Capacity是田间持水量Matric Bulk Density是基质粒密度。

所有专家都曾始于新手 3443

Shell脚本中的几种计算方式

Shell脚本中的几种计算方式expr使用方括号let使用双括号bash内建计算器 expr  这是我常常用的一个命令,它不仅可以处理数值,还可以处理表达式或者字符串。 使用expr命令的过程中,需要注意一些特殊的字符,例如*、/、()等等,1 使用方括号 let 内建命令 使用双括号 内建命令 bash内建计算器 这些符号在shell中有特殊的意思,需要在其前面加上\对其转义。 ↩︎ ...

fuluoyide312的博客 6064

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

深度学习工具包 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")

shell 数组长度

Shell  数组操作方式 数组元素个数  ${#array[@]}  数组的所有元素  ${array[*]}  字符串长度      ${#str} 1、获取数组元素的个数:         array=(bill   chen  bai   hu);         num=${#array[@]}                          //获取数组元素的个数。

7948

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

Shell编程——字符串的相关操作(定义、拼接、截取、求长度)

以下内容源于C语言中文网的学习与整理,非原创,如有侵权请告知删除。

主要记录嵌入式学习与开发过程。 2万+

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Shell编程——数组的相关操作(定义、拼接、求长度等)

以下内容源于C语言中文网的学习与整理,非原创,如有侵权请告知删除。

主要记录嵌入式学习与开发过程。 2283

shell 截取字符串的几种方式

Shell 截取字符串通常有两种方式:从指定位置开始截取和从指定字符(子字符串)开始截取。 从指定位置开始截取 这种方式需要两个参数:除了指定起始位置,还需要截取长度,才能最终确定要截取的字符串。 既然需要指定起始位置,那么就涉及到计数方向的问题,到底是从字符串左边开始计数,还是从字符串右边开始计数。答案是 Shell 同时支持两种计数方式。 1) 从字符串左边开始计数 如果想从字符串的左边开始计数,那么截取字符串的具体格式如下: ${string: start :length} 其中,.

勇敢前行,一路向前 2069

shell中取字符串子串的几种方式

<br />(1)awk中函数substr<br /> substr(源字符串,开始索引,长度)   开始索引以0开始<br /> 示例:<br /> awk '{$a=substr($0,0,2);print $a;}' filename<br /> 假设文件中为只有一行为abcdefg,则返回结果为ab<br /><br /> (2)expr substr <br /> expr substr 字符串 开始索引 长度   开始索引以1开始<br /> 示例:<br />expr subst

星星的技术专栏 4万+

shell 脚本中求一个字符串的长度

shell脚本中 求字符串的长度.

阿常呓语的专栏 7652

linux shell 字符长度,Shell中获取字符串长度方法总结

求字符串操作在shell脚本中很常用,下面归纳、汇总了求字符串的几种可能方法:【方法一】:利用${#str}来获取字符串的长度[mkx@localhost testdir]$ stringZ=abcABC123ABCabc[mkx@localhost testdir]$ echo ${#stringZ}15【方法二】:利用awk的length方法[mkx@localhost testdir]$ e...

weixin_32073733的博客 1440

linux shell字符串及字符串长度获取方式

linux shell字符串及字符串长度获取方式

学亮编程手记 567

linux shell 显示字符串长度,Shell获取字符串长度的多种方法总结

前言我们在日常工作中,对于求字符串操作在shell脚本中很常用,实现的方法有很多种,下面就来给大家归纳、汇总了求字符串的几种可能方法,话不多说了,来一起看看详细的介绍吧。方法如下:【方法一】:利用${#str}来获取字符串的长度【方法二】:利用awk的length方法备注:1) 最好用{}来放置变量2) 也可以用length($0)来统计文件中每行的长度【方法三】:利用awk的nf项来获取字符串长...

weixin_35930255的博客 2651

Shell脚本中计算字符串长度的几种方法

1:wc -L wc -l (小写)是获取当前字符串内容的行数,wc -L (大写)可以获取到当前行的长度,因此对于单独行的字符串可以用这个简单的方法获取。 $ str="123.?+ abc" $ echo "$str" |wc -L 10 2: expr length $string expr length $string 可以获取字符串的长度 $ str="123.?+ abc"...

vayneX的博客 1541
上一篇: shell变量的子串的删除/替换
下一篇: linux-awk中NF与$NF的区别
lm_123_456
博客等级 码龄11年 35粉丝 86原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值