Skip to content

Commit

Permalink
adbbc
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxwt committed Jun 20, 2022
1 parent f71c9b7 commit 1e286bb
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 1 deletion.
25 changes: 25 additions & 0 deletions website/content/ChapterFive/Chongdingxiang1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: 1.01 常规重定向
type: docs
weight: 1
---

|重定向式子|作用|
|:---:|:---:|
|1>file|重定向stdout到file|
|2>file|重定向stderr到file|
|0>file|重定向stdin到file|
|&>file|重定向stdout和stderr到file|
|2>&1|重定向stderr到stdout|
|i>&j|指向i文件的所有输出发送到j中|
|>&j|默认重定向描述符1的输出发送到j中|
|[i]<>file|分配文件描述符i给file|
|\||管道|
|n<&-|关闭输入文件标识符n|
|n>&-|关闭输出文件标识符n|


<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.linuxwt.com/linuxwtabs/ChapterFive">上一页➡️</a></p>
<p><a href="https://books.linuxwt.com/linuxwtabs/ChapterFive/Chongdingxiang2">下一页➡️</a></p>
</div>
104 changes: 104 additions & 0 deletions website/content/ChapterFive/Chongdingxiang2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
---
title: 1.02 重定向中的exec
type: docs
weight: 1
---

# exec重定向标准输入
```bash
#!/bin/bash

exec 6<&0 # 将stdin重定向到文件标识符6
exec < test.txt # 使用test.txt文件内容代替stdin,即后面的输入全部来自该文件
read a1 # 读取test.txt第一行
read a2

echo "Following lines read from file"
echo "--------"
echo $a1
echo $a2

exec 0<&6 6<&- # 从文件标识符6恢复stdin并关闭文件标识符6
echo "enter data"
read b1
echo "Input read from stdin"
echo "--------"
echo $b1

exit 0
```

# exec重定向标准输出
```bash
#!/bin/bash

exec 6>&1 # 将标准输出重定向到文件标识符6
exec > test1.txt # 输出到stdout的内容全部输出到文件test1.txt

echo "logfile"
date
echo "------------"

echo "output of \"ls -la\" command"
ls -la
echo "output of \"df\" command"
df

exec 1>&6 6>&- # 从文件标识符6恢复stdout并关闭文件标识符6

echo "stdout now restored to default"
echo "------------"
ls -la

exit 0
```

# exec同时重定向标准输入标准输出
```bash
#!/bin/bash

E_FILE_ACCESS=70
E_WRONG_ARGS=71

if [ ! -r "$1" ]
then
echo "can not read from inputfile"
echo "Usage :$0 input file"
exit $E_FILE_ACCESS
fi

if [ -z "$2" ]
then
echo "need to specify output file"
echo "Usage: $0 inputfile outputfile"
exit $E_WRONG_ARGS
fi

exec 4<&0
exec < $1

exec 6>&1
exec > $2

cat - | tr a-z A-Z # 从$1处获得输入并将其中小写字符转换成大写

exec 0<&4 4<&-
exec 1>&6 6>&-

echo "File \"$1\" written to \"$2\""
```
这个脚本需要事先创建一个文件
cat test2.txt
```bash
a
b
c
```

bash 3.sh test2.txt test3.txt


<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.linuxwt.com/linuxwtabs/ChapterFive/Chongdingxiang1">上一页➡️</a></p>
<p><a href="https://books.linuxwt.com/linuxwtabs/ChapterSix">下一章➡️</a></p>
</div>
12 changes: 12 additions & 0 deletions website/content/ChapterFive/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: 第四章 重定向
type: docs
weight: 5
---

重定向:捕捉文件、命令、程序、脚本的输出,然后将这些输出发送到另外一个文件、命令、程序、脚本中

<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.linuxwt.com/linuxwtabs/ChapterFour/Subshell3">上一页➡️</a></p>
<p><a href="https://books.linuxwt.com/linuxwtabs/ChapterFive/Chongdingxiang1">下一页➡️</a></p>
</div>
2 changes: 1 addition & 1 deletion website/content/ChapterFour/Subshell3.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ diff list123 list456

<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.linuxwt.com/linuxwtabs/ChapterFour/Subshell2">上一页➡️</a></p>
<p><a href="https://books.linuxwt.com/linuxwtabs/ChapterThree">下一章➡️</a></p>
<p><a href="https://books.linuxwt.com/linuxwtabs/ChapterFive">下一章➡️</a></p>
</div>

0 comments on commit 1e286bb

Please sign in to comment.