《学习bash》笔记--输入输出

本文介绍了Bash shell中关于输入输出重定向的基本概念和操作,包括管道、重定向符号的用法,如>`、`>、`>>`等,以及 noclobber 特性。此外,还讲解了字符串处理命令`echo`和`read`的使用,包括它们的选项和读取变量的方法。
摘要由CSDN通过智能技术生成

1.I/O重定向符

I/O重定向符如下:

  • cmd1 | cmd2:管道,接收cmd1的标准输出作为cmd2的标准输入。
  • >file:将标准输出定向到file
  • <file:从file接收标准输入
  • >>file:将标准输出定向到file,如果file存在则附加在后面
  • >|file:即使设置了noclobber仍然强制标准输出到file。

shell提供了一种称为noclobber的特性,该特性可防止重定向时不经意地重写了已存在的文件。通过设置变量noclobber可以

将此特 性打开。打开后若将输出重定向到某个已存在文件,则shell将报告错误消息,并且不执行重定向命令。

例如:

root@-virtual-machine:~# cat a.txt
123
root@virtual-machine:~# set -o | grep "noclobber"
noclobber       off
root@virtual-machine:~# echo "test1" > a.txt
root@virtual-machine:~# cat a.txt
test1
root@virtual-machine:~# set -o noclobber
root@virtual-machine:~# set -o | grep "noclobber"
noclobber       on
root@gmdz-virtual-machine:~# echo "hello" > a.txt
-bash: a.txt: 无法覆盖已存在的文件

root@virtual-machine:~# echo "hello" >| a.txt
root@virtual-machine:~# cat a.txt
hello

  • n>|file:即使设置了noclobber仍强制从文件描述符n中输出到file。

例如:

root@-virtual-machine:~# cat a.txt
123
root@virtual-machine:~# set -o | grep "noclobber"
noclobber       off
root@virtual-machine:~# ls nosuchfile 2> a.txt
root@virtual-machine:~# cat a.txt
ls: 无法访问nosuchfile: 没有那个文件或目录
root@virtual-machine:~# echo "123" > a.txt
root@virtual-machine:~# cat a.txt
123
root@virtual-machine:~# set -o noclobber
root@virtual-machine:~# set -o | grep "noclobber"
noclobber       on
root@virtual-machine:~# ls nosuchfile 2> a.txt
-bash: a.txt: 无法覆盖已存在的文件
root@virtual-machine:~# cat a.txt
123
root@virtual-machine:~# ls nosuchfile 2>| a.txt
root@virtual-machine:~# cat a.txt
ls: 无法访问nosuchfile: 没有那个文件或目录

  • <>file:使用file同时作为输入和输出。
主要用于设备文件,也就是对应诸如终端和通信线路的硬件设备,底层系统程序员可以使用它测试设备驱动器,否则,它不是很有用。
  • n<>file,以读写方式打开file,并将n重定向到该文件。

要说明这个重定向符之前,先要说明exec命令:

shell的内建命令exec将并不启动新的shell,而是用要被执行命令替换当前的shell进程,并且将老进程的环境清理掉,而且exec命令

后的其它命令将不再执行。因此,

当使用`bash -i`命令时,`-i`选项代表交互式模式,它会启动一个新的bash shell,并且允许用户与shell进行交互。在交互式模式下,用户可以输入命令并立即得到输出。这对于需要在命令行下与shell进行交互的情况非常有用。 而当使用`bash -c`命令时,`-c`选项代表命令模式,它使得bash shell可以执行一个指定的命令而不需要启动一个新的shell。命令模式允许用户直接在命令行上执行一个或多个命令,并且不需要与shell进行交互。这对于需要一次性执行多个命令或将命令与其他程序或脚本进行组合的情况非常有用。 所以,`bash -i`用于交互式模式,而`bash -c`用于命令模式。它们的区别在于交互式模式允许用户与shell进行交互,而命令模式则是一次性执行指定的命令。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [bash源代码分析----bash --login的作用和--noprofile的作用](https://blog.csdn.net/sitelist/article/details/114583866)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [【学习笔记】反弹shell命令学习笔记,实战指南,1v1攻击测试教学,常用的反弹shell命令: bash -i >& /dev/tcp...](https://blog.csdn.net/qq_37092496/article/details/123510433)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值