使用bash / shell脚本打开并将数据写入文本文件

时间:2021-12-30 13:56:54

How can I write data to a text file automatically by shell scripting in Linux?

如何通过Linux中的shell脚本自动将数据写入文本文件?

I was able to open the file. However, I don't know how to write data to it.

我能够打开文件。但是,我不知道如何向它写入数据。

8 个解决方案

#1


222  

echo "some data for the file" >> fileName

#2


101  

#!/bin/sh

FILE="/path/to/file"

/bin/cat <<EOM >$FILE
text1
text2
text3
text4
EOM

#3


40  

You can redirect the output of a command to a file:

您可以将命令的输出重定向到文件:

$ cat file > copy_file

or append to it

或附加

$ cat file >> copy_file

If you want to write directly the command is echo 'text'

如果你想直接写命令是echo'text'

$ echo 'Hello World' > file

#4


26  

#!/bin/bash

cat > FILE.txt <<EOF

info code info 
info code info
info code info

EOF 

#5


13  

I know this is a damn old question, but as the OP is about scripting, and for the fact that google brought me here, opening file descriptors for reading and writing at the same time should also be mentioned.

我知道这是一个该死的老问题,但由于OP是关于脚本的,而且谷歌把我带到这里的事实,同时也应该提到打开文件描述符进行读写。

#!/bin/bash

# Open file descriptor (fd) 3 for read/write on a text file.
exec 3<> poem.txt

    # Let's print some text to fd 3
    echo "Roses are red" >&3
    echo "Violets are blue" >&3
    echo "Poems are cute" >&3
    echo "And so are you" >&3

# Close fd 3
exec 3>&-

Then cat the file on terminal

然后在终端上抓取文件

$ cat poem.txt
Roses are red
Violets are blue
Poems are cute
And so are you

This example causes file poem.txt to be open for reading and writing on file descriptor 3. It also shows that *nix boxes know more fd's then just stdin, stdout and stderr (fd 0,1,2). It actually holds a lot. Usually the max number of file descriptors the kernel can allocate can be found in /proc/sys/file-max or /proc/sys/fs/file-max but using any fd above 9 is dangerous as it could conflict with fd's used by the shell internally. So don't bother and only use fd's 0-9. If you need more the 9 file descriptors in a bash script you should use a different language anyways :)

此示例导致文件poem.txt打开以便在文件描述符3上进行读取和写入。它还显示* nix框知道更多fd,然后只是stdin,stdout和stderr(fd 0,1,2)。它实际上拥有很多。通常,内核可以分配的文件描述符的最大数量可以在/ proc / sys / file-max或/ proc / sys / fs / file-max中找到,但使用任何大于9的fd是危险的,因为它可能与fd使用的fd冲突。 shell内部。所以不要打扰,只使用fd的0-9。如果你需要更多bash脚本中的9个文件描述符,你应该使用不同的语言:)

Anyhow, fd's can be used in a lot of interesting ways.

无论如何,fd可以用很多有趣的方式使用。

#6


9  

I like this answer:

我喜欢这个答案:

cat > FILE.txt <<EOF

info code info 
...
EOF

but would suggest cat >> FILE.txt << EOF if you want just add something to the end of the file without wiping out what is already exists

但建议cat >> FILE.txt << EOF如果你想在文件末尾添加一些内容而不删除已存在的内容

Like this:

喜欢这个:

cat >> FILE.txt <<EOF

info code info 
...
EOF

#7


5  

Moving my comment as an answer, as requested by @lycono

根据@lycono的要求,将我的评论作为答案移动

If you need to do this with root privileges, do it this way:

如果您需要以root权限执行此操作,请按以下方式执行此操作:

sudo sh -c 'echo "some data for the file" >> fileName'

#8


2  

For environments where here documents are unavailable (Makefile, Dockerfile, etc) you can often use printf for a reasonably legible and efficient solution.

对于此处文档不可用的环境(Makefile,Dockerfile等),您通常可以使用printf来获得合理清晰且高效的解决方案。

printf '%s\n' '#!/bin/sh' '# Second line' \
    '# Third line' \
    '# Conveniently mix single and double quotes, too' \
    "# Generated $(date)" \
    '# ^ the date command executes when the file is generated' \
    'for file in *; do' \
    '    echo "Found $file"' \
    'done' >outputfile

#1


222  

echo "some data for the file" >> fileName

#2


101  

#!/bin/sh

FILE="/path/to/file"

/bin/cat <<EOM >$FILE
text1
text2
text3
text4
EOM

#3


40  

You can redirect the output of a command to a file:

您可以将命令的输出重定向到文件:

$ cat file > copy_file

or append to it

或附加

$ cat file >> copy_file

If you want to write directly the command is echo 'text'

如果你想直接写命令是echo'text'

$ echo 'Hello World' > file

#4


26  

#!/bin/bash

cat > FILE.txt <<EOF

info code info 
info code info
info code info

EOF 

#5


13  

I know this is a damn old question, but as the OP is about scripting, and for the fact that google brought me here, opening file descriptors for reading and writing at the same time should also be mentioned.

我知道这是一个该死的老问题,但由于OP是关于脚本的,而且谷歌把我带到这里的事实,同时也应该提到打开文件描述符进行读写。

#!/bin/bash

# Open file descriptor (fd) 3 for read/write on a text file.
exec 3<> poem.txt

    # Let's print some text to fd 3
    echo "Roses are red" >&3
    echo "Violets are blue" >&3
    echo "Poems are cute" >&3
    echo "And so are you" >&3

# Close fd 3
exec 3>&-

Then cat the file on terminal

然后在终端上抓取文件

$ cat poem.txt
Roses are red
Violets are blue
Poems are cute
And so are you

This example causes file poem.txt to be open for reading and writing on file descriptor 3. It also shows that *nix boxes know more fd's then just stdin, stdout and stderr (fd 0,1,2). It actually holds a lot. Usually the max number of file descriptors the kernel can allocate can be found in /proc/sys/file-max or /proc/sys/fs/file-max but using any fd above 9 is dangerous as it could conflict with fd's used by the shell internally. So don't bother and only use fd's 0-9. If you need more the 9 file descriptors in a bash script you should use a different language anyways :)

此示例导致文件poem.txt打开以便在文件描述符3上进行读取和写入。它还显示* nix框知道更多fd,然后只是stdin,stdout和stderr(fd 0,1,2)。它实际上拥有很多。通常,内核可以分配的文件描述符的最大数量可以在/ proc / sys / file-max或/ proc / sys / fs / file-max中找到,但使用任何大于9的fd是危险的,因为它可能与fd使用的fd冲突。 shell内部。所以不要打扰,只使用fd的0-9。如果你需要更多bash脚本中的9个文件描述符,你应该使用不同的语言:)

Anyhow, fd's can be used in a lot of interesting ways.

无论如何,fd可以用很多有趣的方式使用。

#6


9  

I like this answer:

我喜欢这个答案:

cat > FILE.txt <<EOF

info code info 
...
EOF

but would suggest cat >> FILE.txt << EOF if you want just add something to the end of the file without wiping out what is already exists

但建议cat >> FILE.txt << EOF如果你想在文件末尾添加一些内容而不删除已存在的内容

Like this:

喜欢这个:

cat >> FILE.txt <<EOF

info code info 
...
EOF

#7


5  

Moving my comment as an answer, as requested by @lycono

根据@lycono的要求,将我的评论作为答案移动

If you need to do this with root privileges, do it this way:

如果您需要以root权限执行此操作,请按以下方式执行此操作:

sudo sh -c 'echo "some data for the file" >> fileName'

#8


2  

For environments where here documents are unavailable (Makefile, Dockerfile, etc) you can often use printf for a reasonably legible and efficient solution.

对于此处文档不可用的环境(Makefile,Dockerfile等),您通常可以使用printf来获得合理清晰且高效的解决方案。

printf '%s\n' '#!/bin/sh' '# Second line' \
    '# Third line' \
    '# Conveniently mix single and double quotes, too' \
    "# Generated $(date)" \
    '# ^ the date command executes when the file is generated' \
    'for file in *; do' \
    '    echo "Found $file"' \
    'done' >outputfile