Piping and Redirection,output,Reading from STDIN in script(Linux)

本文介绍在命令行环境中如何使用I/O重定向和管道来改变程序的标准输入、输出及错误信息流向,包括将输出保存到文件、从文件读取输入以及通过管道连接多个命令。

Every program we run on the command line automatically has three data streams connected to it.

  • STDIN (0) - Standard input (data fed into the program)
  • STDOUT (1) - Standard output (data printed by the program, defaults to the terminal)
  • STDERR (2) - Standard error (for error messages, also defaults to the terminal)

Redirecting to a File

  > file_name,将programs output (or whatever it sends to STDOUT)保存在file_name文件中
Exam:
<pre name="code" class="cpp">[yong.chen@entmcnode15] my_linux $ ls > myoutput
[.chen@entmcnode15] my_linux $ cat myoutput 
./
../
aa
bb
cc
myoutput
[.chen@entmcnode15] my_linux $ ls
./  ../  aa  bb  cc  myoutput
[.chen@entmcnode15] my_linux $ 

注意:1.从例中可知,在file  myoutput中是一个文件名一行,但在实际terminal中是所有输出文件名都在同一行,所以注意保存在文件中的内容与terminal上输出的相同,但是排版格式可能有所不同。
            2.从例中可知,新建的文件myoutput也保存在自身中,所以是先新建输出保存文件,然后将输出内容保存在文件中。
             3. > ,是将原文件清空,然后将输出保存其中;若要不清空原文件并保存输出,则使用 >> file_name.

Redirecting from a File

< file_name:与上面内容恰好相反,是将file_name中内容传递到program
[@entmcnode15] my_linux $ wc -l aa
5 aa
[@entmcnode15] my_linux $ wc -l <aa
5
[@entmcnode15] my_linux $ wc -l <aa >output
[@entmcnode15] my_linux $ cat output 
5
[@entmcnode15] my_linux $ 
例中wc -l file_name,是统计file_name中内容的行数。

Piping

| :feed the output from the program on the left of | as input to the program on the right.
ls
barry.txt bob example.png firstfile foo1 myoutput video.mpeg
ls | head -3
barry.txt
bob
example.png
ls | head -3 | tail -1
example.png
解释:将|左边的操作结果传递给| 右边进行操作
line 3:将ls的文件打印前三个
line 7: 选取ls的前三个文件,然后选择此三个文件中的最后一个

Summary:
>
Save output to a file.
>>
Append output to a file, don't clear previous contents
<
Read input from a file.
|
Send the output from one program as input to another program.
Streams
Every program you may run on the command line has 3 streams, STDIN, STDOUT and STDERR.

Reading from STDIN

Bash accomodates piping and redirection by way of special files. Each process gets it's own set of files (one for STDIN, STDOUT and STDERR respectively) and they are linked when piping or redirection is invoked. Each process gets the following files:
  • STDIN - /dev/stdin or /proc/self/fd/0
  • STDOUT - /dev/stdout or /proc/self/fd/1
  • STDERR - /dev/stderr or /proc/self/fd/2
所以当编写的script要处理pipe的数据,我们只需要read相应的上述三个文件即可。
[@entmcnode15] my_linux $ cat my_script.sh 
#!/bin/bash
echo Here is the output
cat /dev/stdin | head -3
[@entmcnode15] my_linux $ cat aa
llkj
da
ds
f
:`k
[@entmcnode15] my_linux $ cat aa | ./my_script.sh
Here is the output
llkj
da
ds
[@entmcnode15] my_linux $
从例中可知,在script中将STDIN 文件当作/dev/stdin 处理。

Conclusion:
read varName
Read input from the user and store it in the variable varName.
/dev/stdin
A file you can read to get the STDIN for the Bash script
跟网型逆变器小干扰稳定性分析与控制策略优化研究(Simulink仿真实现)内容概要:本文围绕跟网型逆变器的小干扰稳定性展开分析,重点研究其在电力系统中的动态响应特性及控制策略优化问题。通过构建基于Simulink的仿真模型,对逆变器在不同工况下的小信号稳定性进行建模与分析,识别系统可能存在的振荡风险,并提出相应的控制优化方法以提升系统稳定性和动态性能。研究内容涵盖数学建模、稳定性判据分析、控制器设计与参数优化,并结合仿真验证所提策略的有效性,为新能源并网系统的稳定运行提供理论支持和技术参考。; 适合人群:具备电力电子、自动控制或电力系统相关背景,熟悉Matlab/Simulink仿真工具,从事新能源并网、微电网或电力系统稳定性研究的研究生、科研人员及工程技术人员。; 使用场景及目标:① 分析跟网型逆变器在弱电网条件下的小干扰稳定性问题;② 设计并优化逆变器外环与内环控制器以提升系统阻尼特性;③ 利用Simulink搭建仿真模型验证理论分析与控制策略的有效性;④ 支持科研论文撰写、课题研究或工程项目中的稳定性评估与改进。; 阅读建议:建议读者结合文中提供的Simulink仿真模型,深入理解状态空间建模、特征值分析及控制器设计过程,重点关注控制参数变化对系统极点分布的影响,并通过动手仿真加深对小干扰稳定性机理的认识。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值