Linux 重定向-文件描述符-here document-$相关

本文详细介绍了Linux命令行中的输出重定向,包括使用`>`、`>>`进行标准输出和错误输出的重定向,以及如何使用`<`进行标准输入重定向。此外,还讲解了`tee`命令、文件描述符、Here Document的概念和用法,以及错误输出追加到已有文件的技巧。内容深入浅出,适合Linux初学者和进阶者学习。
摘要由CSDN通过智能技术生成

1、 > 标准正确输出重定向

(与1> 一样)
linux 系统里面默认的输出方向是屏幕(标准输出方向),重定向就是重新定义输出的方向
任何命令 只要有内容输出 都可以输出重定向
> 1.txt 清空文件内容但不删除文件

[root@localhost lianxi]# cat hosts passwd  > chen.txt
[root@localhost lianxi]# echo zhangshaowei songzhiqiang huangzhiqiang  >chen.txt 
[root@localhost lianxi]# cat chen.txt  原来的文件被覆盖了
zhangshaowei songzhiqiang huangzhiqiang
[root@localhost lianxi]# cat passwd > passwd2  相当于复制
[root@localhost lianxi]# ll
总用量 16
-rw-r--r--. 1 root root 1154 1018 11:40 p

> 这里是引用

asswd
-rw-r--r--. 1 root root 1154 1018 14:56 passwd2

引申:tee命令

用于读取标准输入的数据,并将其内容输出成文件。
-a或–append  附加到既有文件的后面,而非覆盖它.

2、>> 标准正确追加输出重定向

(与1>> 一样)
任何有输出的命令,都可以追加输出重定向,在文件末尾追加输出内容

3、< 标准输入重定向

将命令中接受输入的途径由默认的键盘更改为指定的文件

[root@192 lianxi]# wc </etc/passwd   
  24   52 1195
[root@192 lianxi]# wc /etc/passwd  
  24   52 1195 /etc/passwd

while循环read
if循环

#/bin/bash
while read name sex age
do
  if (($age > 19));then  
  #分号是命令连接符号,用来在一行里面输入多个命令,then换行了的话是不用加;的
          echo "大于19岁的同学姓名是:$name,性别是:$sex, 年龄是:$age"
  else
          echo "小于19岁的同学姓名是:$name,性别是:$sex, 年龄是:$age"
  fi
done < student.txt

4、 2 > 标准错误输出重定向

[root@192 lianxi]# sdxfcsd   2> 1.txt   错误的就会向1.txt 里输出
[root@192 lianxi]# cat 1.txt 
-bash: sdxfcsd: 未找到命令
[root@192 lianxi]# echo hello 2>1.txt  正确的会向屏幕输出,并且清空1.txt里面的内容
hello
[root@192 lianxi]# cat 1.txt 
[root@192 lianxi]# echo fengdeyong 1>1.txt 2>2.txt  正确的输出到1.txt 错误的输出到2.txt
[root@192 lianxi]# > 1.txt   清空文件内容但不删除文件
[root@192 lianxi]# cat 1.txt 

5、 2 >> 标准错误追加输出重定向

6 、& >、 &>>

无论正确与否,都重定向到一个文件,文件不存在就新建,存在就 覆盖 会追加
&>> 会追加
echo feng 1>feng.txt 2>&1 错误的输出会跟着1输
echo feng &> feng.txt 效果一样

[root@192 lianxi]# echo feng 1>feng.txt 2>&1
[root@192 lianxi]# cat feng.txt
feng
[root@192 lianxi]# echo he &> feng.txt
[root@192 lianxi]# cat feng.txt
he
[root@localhost lianxi]# lslsls 2>&1|grep ls  #命令的错误输出跟着1输出给管道符号
-bash: lslsls: 未找到命令

7、 here document

生成指定内容的文档—》 用于编写shell脚本
<< 追加输入重定向
EOF 结束标志 end of file ,可以换成其他的

[root@localhost lianxi]# cat >songzhiqiang.txt <<EOF
> SONG ZHI QIANG
> 东北 吉林 长白山
> 长沙
> EOF
[root@localhost lianxi]# cat -n songzhiqiang.txt
     1  SONG ZHI QIANG
     2  东北 吉林 长白山
     3  长沙

8、文件描述符

是进程里面打开哪些文件,给文件的一个编号
系统为每一个进程维护了一个文件描述符表,此表记录了这个进程打开的所有文件
文件描述符是非符整数,默认情况下内核允许一个进程最多可以打开1024个文件
(ulimit -a可以查看 ulimit -n 2048 可以临时修改)

echo $$ 查看当前终端的进程号
通过文件描述符找到打开的文件,根据文件的信息查incode表,去磁盘里面找到文件

[root@localhost fd]# ll /dev/std*
lrwxrwxrwx. 1 root root 15 113 09:42 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx. 1 root root 15 113 09:42 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx. 1 root root 15 113 09:42 /dev/stdout -> /proc/self/fd/1

[root@localhost fd]# ll
总用量 0
lrwx------. 1 root root 64 113 15:45 0 -> /dev/pts/2
lrwx------. 1 root root 64 113 15:45 1 -> /dev/pts/2
lrwx------. 1 root root 64 113 15:45 2 -> /dev/pts/2
lrwx------. 1 root root 64 113 16:31 255 -> /dev/pts/2
lr-x------. 1 root root 64 113 15:45 3 -> /var/lib/sss/mc/passwd
lrwx------. 1 root root 64 113 15:45 4 -> 'socket:[52501]'

w 查看当前linux系统登录的信息
ctrl+alt+f1-6 可以打开1-6个tty终端
pts是伪终端

[root@localhost ~]# w  #查看当前linux系统登录的信息
 10:05:16 up 21:49,  3 users,  load average: 0.07, 0.02, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -09   33.00s  0.40s  0.40s -bash
root     pts/0    192.168.56.1     10:04    1.00s  0.02s  0.01s w
root     pts/2    192.168.56.114    2days  1.09s  1.07s python3

/dev/pts 存放的是字符设备文件,

每打开一个终端,背后会产生一 个bash文件,bash进程会有输入和输出的字符设备文件,

9、$相关

$? 上一条命令执行的返回值
0 执行成功 非0 执行失败,重定向 不影响命令返回值
127 命令不存在
1.9 命令是正确的,参数或选项不对

$$ 查看当前终端进程号

10、返回值和输出结果的区别

#!/bin/bash
#输出内容 read接收用户在终端输入的内容 -p 提示字符串的作用
read -p "请输入俩个数字:" num1  num2
sc_sum = $(($num1 + $num2))

echo "俩数之和为$sc_sum"   
#输出值是命令执行的过程中输出到屏幕上给人看的

exit 123  # 返回值,程序退出的时候返回的值,给bash的
[root@localhost lianxi]# bash sc.sh 
请输入俩个数字:10 20 
俩数之和为30    #输出结果
[root@localhost lianxi]# echo $? 123  #返回值

练习:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这个手刹不太灵儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值