文章目录
- linux_文件输入输出重定向/shell写入多行文本到文件中/cat 操作文件
- references
- sheet
- Redirection Commands
- Note that the file descriptor:
- Summary
- example
- 重定向多行文本输入(文本块)
- 覆盖性写入多行内容
- 追加多行
- >> <<重定向法
- `引号`换行重定向法(echo >)
- 方法特点
- cat 和重定向
- 利用cat 创建一个多行文件
- cat 合并文件并作为全新创建的文件的内容(concatenate files)
- 利用tee来重定向
- 利用tee创建多行文件
- 类似于cat 的用法
- tee也可以配合<<符使用
- 在脚本文件中一次性打印多行
- 单行内容写入到文件
- 将字符串传递给命令行(<<<)
- powershell 多行输入重定向
- reference
- @string(here-string)方式
- 直接使用`' '`
- @'content'@ pair method
linux_文件输入输出重定向/shell写入多行文本到文件中/cat 操作文件
references
- Unix / Linux - Shell Input/Output Redirections (tutorialspoint.com)
- bash - What does <<< mean? - Unix & Linux Stack Exchange
- What is Redirection?
- Output Redirection
- Input redirection
- File Descriptors (FD)
- Error Redirection
- Why Error Redirection?
sheet
Redirection Commands
Following is a complete list of commands which you can use for redirection −
Sr.No. | Command & Description |
1 | pgm > file Output of pgm is redirected to file |
2 | pgm < file (pgm从文件读取输入)Program pgm reads its input from file |
3 | pgm >> file Output of pgm is appended to file |
4 | n> file Output from stream with descriptor n redirected to file |
5 | n>> file Output from stream with descriptor n appended to file |
6 | n>& m Merges output from stream n with stream m |
7 | n<& m Merges input from stream n with stream m |
8 | << tag (读取标准输入内容块)Standard input comes from here through next tag at the start of line |
9 | $\textbf{ |
Note that the file descriptor:
- 0 is normally standard input (STDIN),
- 1 is standard output (STDOUT),
- 2 is standard error output (STDERR).
Summary
- Each file in Linux has a corresponding File Descriptor associated with it
- The keyboard is the standard input device while your screen is the standard output device
- “>” is the output redirection operator. “>>” appends output to an existing file
- “<” is the input redirection operator
- “>&”re-directs output of one file to another.
- You can re-direct error using its corresponding File Descriptor 2.
example
- 普通标准重定向
#环境:install_pip文件是存在的,而file_test文件时不存在的,以下命令会产生两种性质的输出
# cxxu @ cxxuAli in ~ [18:27:34]
$ ll install_pip file_test
ls: cannot access 'file_test': No such file or directory
-rw-rw-r-- 1 cxxu cxxu 290 May 18 23:22 install_pip
# cxxu @ cxxuAli in ~ [18:28:05] C:2
$ ll install_pip file_test > log
ls: cannot access 'file_test': No such file or directory
- 可见,被正确处理的部分可以被
>
重定向到指定文件中 - 但是未被正确处理的错误内容依然会被输出到终端
- 所以为了将错误信息一同保留,需要使用
2>&
- 比如指定
2>&1
,这样,错误信息会跟随标准输出 输出到同一个文件中.
- 流n 冲定向到流m:
# cxxu @ cxxuAli in ~ [18:28:11] C:2
$ ll install_pip file_test > log 2>&1
# cxxu @ cxxuAli in ~ [18:28:23] C:2
$ cat log
ls: cannot access 'file_test': No such file or directory
-rw-rw-r-- 1 cxxu cxxu 290 May 18 23:22 install_pip
- 将所有输出(包括错误输出重定向到一个文件中log.txt)
- 使用
nohup...&
将任务挂在到后台执行
$ nohup webhook -hooks hooks.json -verbose >log.txt 2>&1 &
[1] 29968
重定向多行文本输入(文本块)
下面提到的 token 是自定义的结束符(delimiter),一般使用 eof
覆盖性写入多行内容
-
> fileName << token
- 键入多行内容(content lines…)
- 在单独的一行键入token,结束操作
# 创建多行文件
cxxu_kali➜~» > file << eof [13:58:25]
heredoc> line cover
heredoc> line cover
heredoc> eof
# 查看文件
cxxu_kali➜~» nl file [13:59:07]
1 line cover
2 line cover
cxxu_kali➜~»
追加多行
>> <<重定向法
>> fileName << token
- 键入多行内容(content lines…)
- 在单独的一行键入token,结束操作
- 大概过程是这样的:
<<
将多行输入输送给前面的 >>
; -
>>
又将内容输送到文件 fileName
中
cxxu_kali➜~» >> file << eof
heredoc> append line
heredoc> eof
# 查看结果
cxxu_kali➜~» cat file [14:05:51]
line cover
line cover
append line
引号
换行重定向法(echo >)
方法特点
- 简单易用
- 适用范围广(嵌入脚本内来修改/创建文件方便)
- 但是有些时候需要转义字符,会造成不便!
- 另外在脚本文件中,可以通过字符串加法/模板字符串等操作得到插值后的字符串!
无论是单行还是多行,都可以轻松做到
cxxu_kali➜Users/cxxu/Desktop» ec "line1 [11:12:19]
line2
line3" > ww
cxxu_kali➜Users/cxxu/Desktop» nl ww [11:12:25]
1 line1
2 line2
3 line3
cxxu_kali➜Users/cxxu/Desktop» ec "line5 [11:12:28]
dquote> line6
dquote> line7">>ww
cxxu_kali➜Users/cxxu/Desktop» nl ww [11:15:59]
1 line1
2 line2
3 line3
4 line5
5 line6
6 line7
cat 和重定向
利用cat 创建一个多行文件
方法特点:整齐
cxxu_kali➜~» cat > fileByCat [13:09:08]
line1
line2
#结束输入:可以通过键入ctrl+D完成文件的创建,不过此前需要确保有一个回车(也就是说ctrl+D在单独的行键入才有效);
# 检查文件创建
cxxu_kali➜~» nl fileByCat [13:15:19]
1 line1
2 line2
# 文件追加也没问题
cxxu_kali➜~» cat >> fileByCat [13:15:29]
line by "cat >>"
cxxu_kali➜~» [13:18:10]
cxxu_kali➜~» nl fileByCat [13:18:11]
1 line1
2 line2
3 line by "cat >>"
cxxu_kali➜~»
cat 合并文件并作为全新创建的文件的内容(concatenate files)
# 生成演示文件
cxxu_kali➜~» cat > file1 [13:30:53]
file1 content:
lin1
lin2
cxxu_kali➜~» cat > file2 [13:31:37]
file2 content:
lin3
lin4
# 执行合并
cxxu_kali➜~» cat file1 file2 > fileConcatenated [13:36:14]
cxxu_kali➜~» nl fileConcatenated [13:36:33]
1 file1 content:
2 lin1
3 lin2
4 file2 content:
5 lin3
6 lin4
cxxu_kali➜~»
# 将file1的内容接到file2中去(末尾)
cxxu_kali➜~» cat file1 >> file2
# 查看合并结果
cxxu_kali➜~» cat file2 [13:50:51]
file2 content:
lin3
lin4
file1 content:
lin1
lin2
利用tee来重定向
事实上,我最喜欢使用tee来创建多行内容,特别是在tee与sudo的配合比较方便写入到需要特殊权限的地方
利用tee创建多行文件
类似于cat 的用法
- 从键盘上输入多行内容
- 使用
ctrl+D
完成输入 - 适用于手动交互输入多行内容(不适合脚本中使用)
下面是一个保存清华源到一个文件的过程;在单独的空行键入ctrl+D
完成文件的创建
$ sudo tee -a ./KaliAptSourceQingHua
deb http://mirrors.tuna.tsinghua.edu.cn/kali kali-rolling main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/kali kali-rolling main contrib non-free
对应的cat语句:
sudo cat >> ./KaliAptSourceQingHua
tee也可以配合<<符使用
使用<< token 来结束输入
# cxxu_kali @ cxxuWin11 in ~ [9:16:39]
$ sudo tee -a ./ustcAptSourceQingHua <<eof
heredoc> deb http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib
deb-src http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib
heredoc> eof
# 此时文件已经创建完成
#以下是tee的回显
deb http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib
deb-src http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib
## 再次检查确认
# cxxu_kali @ cxxuWin11 in ~ [9:25:55]
$ nl ustcAptSourceQingHua
1 deb http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib
2 deb-src http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib
在脚本文件中一次性打印多行
通过键盘输入内容来模拟文件输入,将数据传给命令
#!/bin/sh
cat << EOF
This is a simple lookup program
for good (and bad) restaurants
in Cape Town.
EOF
单行内容写入到文件
这比较简单
cxxu_kali➜~» echo "write to file" > file0 [13:08:41]
cxxu_kali➜~» nl file0 [13:08:56]
1 write to file
将字符串传递给命令行(<<<)
- 使用管道符,意味着管道符后面的任务是在subshell中执行的
- 参数可以传递到subshell中,这没问题,但是,当我们要在current shell 中拿到subshell中的处理结果,则需要小心
- 要么再追加一个管道符,将需要取值的命令接在后面,要么避免使用管道符,采取变通的办法.
- 使用
<<<
将某个字符串传递给命令行,可以不开辟subshell,从而可以避免出现意外结果
bash - What does <<< mean? - Unix & Linux Stack Exchange
- 利用cat来查看某些空白字符
┌──(cxxu㉿CxxuWin11)-[~]
└─$ echo "$IFS" |cat -A
:$
┌──(cxxu㉿CxxuWin11)-[~]
└─$ cat -A <<<"$IFS"
:$
powershell 多行输入重定向
- powershell也支持
>
号的方法进行重定任意行内容 - 此外powershell可以不用前头的echo;
reference
- about Quoting Rules - PowerShell | Microsoft Docs
- Everything you wanted to know about variable substitution in strings - PowerShell | Microsoft Docs
- 2Variable substitution
- 2Command substitution
- 3Command execution
- 2Format string
- 3Format values as arrays
- 2Advanced formatting
- 2Joining strings
- 2Join-Path
- 2Strings are arrays
- 3StringBuilder
- 2Delineation with braces
- 2Find and replace tokens
- 3Replace multiple tokens
- 3ExecutionContext ExpandString
- 2Whatever works the best for you
@string(here-string)方式
- 使用
" "
可以直接创建多行文本,但是如果需要阻止shell解释内部的一些特殊符号和可能引起shell解释的字符,则使用' '
来创建 -
' '
本身就是不解释插值变量的
cxxu ~ ﲍ 59 09:47:02
🚀 @"
>> line1
>> line`n@$a
>> "@
line1
line
@
cxxu ~ ﲍ 60 09:47:26
🚀 @'
>> line1
>> line`n@$a
>> '@
line1
line`n@$a
直接使用' '
无法直接保留包含多个'
的字符串(这时候尝试@'
字符串
🚀 '
>> line1@{a}
>> line`n$a
>> '
line1@{a}
line`n$a
@‘content’@ pair method
- 对比
@'
&' '
🚀 'don''t'
don't
cxxu ~ ﲍ 79 10:04:52
🚀 @'
>> 'don''t'
>> '@
'don''t'