10、深入理解Shell脚本基础与核心特性

深入理解Shell脚本基础与核心特性

1. 高级文件描述符的使用

在Shell脚本中,对于文件描述符3及更高编号的使用并没有统一标准,这也暴露出Shell的一个弱点:难以方便地跟踪这些描述符。不过,可以通过变量来存储特定函数使用的值,一定程度上缓解这个问题,示例代码如下:

exec 3>/tmp/log.txt
logfd=3
log() { echo "$@" >&$logfd; }
log "Hello, world!"
log "All done."

此脚本会将两行内容输出到 /tmp/log.txt 文件中。但这种方法并不完美,一方面,无法保证其他Shell代码不会重定向描述符3;另一方面,必须确保在两行代码中使用相同的描述符编号。若尝试先设置变量:

logfd=3
exec $logfd>/tmp/log.txt

这样会失败,因为重定向是Shell语法,重定向操作符(如 3> )不能通过参数扩展得到。不过可以使用 eval 解决:

eval "exec $logfd>/tmp/log.txt"

如果觉得跟踪这些比较麻烦, m4sh 实用工具(GNU autoconf的一部分)提供了一种自动跟踪描述符并避免冲突的方

相关推荐

11、深入理解Shell核心特性

本博客深入探讨了Shell的核心特性,涵盖了Shell关键字与命令列表、灵活的语法结构、短路操作、显式列表的使用、引用机制、参数替换与扩展、字段拆分以及路径名扩展等内容。通过详细的示例和解析,帮助读者全面理解Shell脚本的工作原理和实际应用,从而编写出更高效、可靠的Shell脚本。

year5的博客 148

12、深入理解Shell核心特性与进程管理

本文深入探讨了Shell编程的核心特性与进程管理机制,包括字段分割(通过IFS变量控制)、参数替换(提供多种替换规则)、波浪号扩展(用户目录映射)、通配符匹配(文件路径匹配)等核心概念,并详细介绍了UNIX进程模型和子Shell的使用。文章还总结了Shell编程的最佳实践,如避免特殊字符、合理使用引号、明确变量作用域等,旨在帮助开发者编写高效、健壮且可移植的Shell脚本。

year5的博客 55

深入理解Shell脚本中的符号与命令

本篇博客深入探讨了Shell脚本编程中常用符号与命令的用法与功能。从基本的通配符、引号使用到复杂的模式匹配、参数扩展,再到控制结构如条件判断和循环,文章详细解析了各种语法元素的细节及其在脚本编写中的重要性。此外,还涵盖了文件操作、命令替换以及后台执行等高级技巧,旨在为读者提供一份全面的Shell脚本参考指南。

weixin_33298352的博客 715

1、深入理解Shell脚本编程:从基础到应用

本文深入探讨了Shell脚本编程的基础与应用,涵盖从基本概念到高级特性的内容。文章介绍了Shell作为UNIX系统核心的双重角色——用户界面与编程语言,并强调了编写可移植脚本的重要性。通过具体示例,讲解了条件判断、循环、函数、错误处理和调试等编程结构的使用方法,展示了Shell在数据处理、文件管理和程序协调方面的强大能力。最后通过一个备份脚本的综合实例,体现了Shell脚本在实际任务自动化中的高效与灵活。

l6m7n8的博客 40

Linux与Shell脚本全面学习资料包

Linux系统是全球最大的开源操作系统项目之一,它是由成千上万的开发者共同维护的。作为一个强大的类Unix操作系统,Linux系统被广泛应用于服务器、嵌入式系统、移动设备以及超级计算机领域。在Shell脚本中定义变量非常简单,通常不需要特定的声明语句。定义变量时,只需给出一个名字和一个赋值。上面的例子中定义了一个名为VARIABLE的变量,并赋值为value,然后通过echo命令打印变量的值。Shell脚本中的变量可以在脚本执行期间的任何地方访问,但它们通常在被赋值后才能使用。

weixin_26854475的博客 1115

Learn-shell:掌握Linux Shell脚本编写教程

Bash脚本的头通常包含一个shebang行,它告诉系统使用哪个解释器来执行脚本。例如,以/bin/bash开头的脚本意味着该脚本将由Bash解释器执行。shebang行是必需的,因为它指示了系统如何正确地调用脚本。/bin/bash# 这是一个注释行在这个例子中,/bin/bash是脚本头,它指定了脚本的解释器。注释行使用符号,不会被解释器执行,但对理解脚本内容有帮助。在 Bash 脚本中,变量是存储数据的简单容器。根据脚本或函数的作用域,变量可以是全局的或局部的。

weixin_32242475的博客 551

全面Shell教学资料:深入理解与实战演练

本文还有配套的精品资源,点击获取 简介:Shell作为Linux/Unix操作系统中的命令解释器,是用户与系统交互的接口,拥有执行命令、脚本编程、文件管理等核心功能。本教学资料详细介绍了Shell的基础知识、Bash Shell的使用、Shell命令和脚本编写技巧,并涵盖输入/输出重定向、正则表达式、错误处理以及一些实用的实战案例。通过本资料,学习者可以系统性地学习并掌握S...

weixin_42452483的博客 1652

Linux Shell脚本编程实战指南

本文还有配套的精品资源,点击获取 简介:本书专为Linux初学者设计,旨在帮助他们深入理解并掌握Shell脚本编程。Shell脚本是Linux系统中用于自动化任务和系统管理的强大工具,其基础和高级概念包括bash语法、变量、条件语句、循环、函数、文件系统操作、输入/输出处理、正则表达式、脚本结构设计、调试和应用实例。通过学习本书,读者将能够编写高效的自动化脚本,加强Linu...

weixin_30789053的博客 1051

Linux系统与Shell脚本编程实战资料

Linux是一个强大的、多用户的操作系统,由芬兰学生林纳斯·托瓦兹在1991年首次发布。它基于UNIX理念,是一个完全开放源代码的系统,这使得它在全球范围内拥有庞大的开发和用户社区。Linux操作系统以其稳定、高效和安全性广受赞誉,被广泛应用于服务器、桌面、嵌入式系统和超级计算机领域。Linux的内核是操作系统的核心部分,它负责管理系统的硬件资源,提供程序运行环境,并且实现了各种文件系统、网络协议栈和设备驱动程序。围绕Linux内核的是一系列基础软件和实用工具,它们共同组成了完整的Linux系统。

weixin_32921023的博客 758

U-Boot 适配新型号 SPI Nor Flash 实战解析

本文详细解析了U-Boot适配新型号SPI Nor Flash的全过程,从JEDEC ID识别错误入手,深入源码分析Flash信息数据库,结合实际手册参数添加新设备支持。重点介绍了设备树同步和验证调试的关键步骤,并总结了常见问题与解决方案,为嵌入式开发者提供实用参考。

weixin_29170327的博客 211

OCR开发实战:如何用PaddleOCR C++版实现本地文字识别(附模型文件处理技巧)

本文详细介绍了如何使用PaddleOCR C++版实现本地文字识别,包括环境搭建、工程编译、运行时优化等全流程。重点讲解了在VS2019/VS2022环境下配置PaddleOCR的CPU推理性能优化技巧,以及模型文件处理的实际应用方法,帮助开发者高效完成OCR本地化部署。

devops8pract的博客 342

【CODESYS】-软件Bug与处理方法(20240710更)

【CODESYS】-软件Bug与处理方法(不定期更新)

qq_35316470的博客 5198

用COMSOL计算复合材料板频散曲线:从建模到绘图全流程

导波在板结构中传播时,不同频率成分的速度不同,即频散现象。理解频散曲线是识别导波模态、确定相速度与群速度的基础,也是无损检测和结构健康监测中信号解读的关键。对于碳纤维增强复合材料等各向异性板,频散关系更加复杂,传统解析方法难以求解,数值仿真成为主流手段。通过COMSOL结合周期性边界条件与特征频率扫描,可以高效计算出复合材料板的频散曲线,进而绘制相速度和群速度随频率变化的图谱,为实验模态识别、换能器激励频率选择提供依据。本文以一个2毫米单向碳纤维板为例,详细介绍几何建模、材料参数设置、网格收敛验证、参数扫描

weixin_31412781的博客 244

brother的PT-9800PCN标签打印机二次开发

本来想通过esc/p指令打印标签,但是无奈能找到的资料太少,而且打电话给客服,也没有sample可以提供参考,后来客服说使用template功能的话,打印很方便。于是下载手册学习了一下, 先下载P-touch Editor软件,制作标签模板。 制作的模板就是普通的标签,不需要连接数据库之类的,模板里有3个object,分别是文本1,文本2,条形码3。这个很关键,后面的代码传送数据,就是按这...

3571

Python机器学习库之orange3使用详解

数据科学和机器学习是当今科技领域的重要组成部分,而数据分析和建模通常是其中的关键步骤。Python Orange3 是一个功能强大且易于使用的开源工具,它结合了数据可视化、数据分析和机器学习,为数据科学家和分析师提供了一个全面的平台。本文将深入介绍 Python Orange3,包括其基本概念、安装方法、示例代码以及一些高级用法,以帮助大家掌握这一强大的数据科学工具。

Rocky006的博客 4036

CAPL诊断功能实现

UDS(Unified Diagnostic Services)是一种用于汽车电子系统诊断的标准协议。它定义了一系列诊断服务,用于读取和写入车辆电子控制单元(ECU)中的数据,以及处理诊断故障码(DTC)。UDS协议通常运行在CAN总线上,使用ISO 15765-2(ISO-TP)协议进行数据传输。通过本文的学习,读者应该对CAPL诊断功能实现有了初步的了解,并能够编写复杂的CAPL脚本。在实际项目中,CAPL的应用非常广泛,希望读者能够通过不断的学习和实践,掌握更多的CAPL技巧,提高自己的开发能力。

闲人编程的博客 1524

fatal: unable to access ‘https://github.com/‘: Recv failure: Connection was reset 解决方案

本文主要介绍了fatal: unable to access ‘https://github.com/’: Recv failure: Connection was reset 解决方案,希望能对使用github的同学们有所帮助。 文章目录 1. 问题描述 2. 解决方案

weixin_43178406的博客 2万+

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

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

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

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

上一篇: 50、Samba 故障排除全攻略
下一篇: 31、分组密码及其操作模式详解
year5
博客等级 码龄1天 0粉丝 1554原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值