I want to create a script and want to use cat > file name but i don't know how to use ctrl+c to exit cat redirection. How to write shell script using cat redirection.
我想创建一个脚本,并希望使用cat>文件名,但我不知道如何使用ctrl + c退出cat重定向。如何使用cat重定向编写shell脚本。
Thanks.
2 个解决方案
#1
I guess you meant Ctrl + Z and end-of-file. You can use it like this in a shell script:
我猜你的意思是Ctrl + Z和文件结尾。您可以在shell脚本中使用它:
cat > ./myfile << EOF
hello world
EOF
#2
It's not very clear what you are redirecting from cat.
你从猫那里重定向是什么并不是很清楚。
If you are redirecting from standard input (i.e., what you type in):
如果您从标准输入重定向(即您键入的内容):
cat - > filename
cat - > filename
Then to stop capturing the keyboard you have to enter Ctrl+D to signal the end of the stream. Then the next line in your script will execute.
然后要停止捕获键盘,您必须输入Ctrl + D来表示流的结束。然后脚本中的下一行将执行。
If you are redirecting from one or more files like this:
如果您从一个或多个文件重定向,如下所示:
cat file1 file2 file3 > allfiles
cat file1 file2 file3> allfiles
Then there is nothing you have to do to stop the redirection. cat will concatenate (hence the name cat) all files sequentially into "allfiles". If they are big it will take a while but that's not cat's fault, it's the time it takes the OS to do the I/O.
然后,您无需停止重定向。 cat会将所有文件连续地(因此名称为cat)连接成“allfiles”。如果它们很大,则需要一段时间,但这不是猫的错,而是操作系统执行I / O所需的时间。
#1
I guess you meant Ctrl + Z and end-of-file. You can use it like this in a shell script:
我猜你的意思是Ctrl + Z和文件结尾。您可以在shell脚本中使用它:
cat > ./myfile << EOF
hello world
EOF
#2
It's not very clear what you are redirecting from cat.
你从猫那里重定向是什么并不是很清楚。
If you are redirecting from standard input (i.e., what you type in):
如果您从标准输入重定向(即您键入的内容):
cat - > filename
cat - > filename
Then to stop capturing the keyboard you have to enter Ctrl+D to signal the end of the stream. Then the next line in your script will execute.
然后要停止捕获键盘,您必须输入Ctrl + D来表示流的结束。然后脚本中的下一行将执行。
If you are redirecting from one or more files like this:
如果您从一个或多个文件重定向,如下所示:
cat file1 file2 file3 > allfiles
cat file1 file2 file3> allfiles
Then there is nothing you have to do to stop the redirection. cat will concatenate (hence the name cat) all files sequentially into "allfiles". If they are big it will take a while but that's not cat's fault, it's the time it takes the OS to do the I/O.
然后,您无需停止重定向。 cat会将所有文件连续地(因此名称为cat)连接成“allfiles”。如果它们很大,则需要一段时间,但这不是猫的错,而是操作系统执行I / O所需的时间。