Bash를 사용하여 텍스트 파일에 데이터를 열고 쓰시겠습니까?
Linux에서 셸 스크립트를 사용하여 텍스트 파일에 자동으로 데이터를 쓰려면 어떻게해야합니까?
파일을 열 수있었습니다. 그러나 데이터를 쓰는 방법을 모르겠습니다.
echo "some data for the file" >> fileName
#!/bin/sh
FILE="/path/to/file"
/bin/cat <<EOM >$FILE
text1
text2 # This comment will be inside of the file.
The keyword EOM can be any text, but it must start the line and be alone.
EOM # This will be also inside of the file, see the space in front of EOM.
EOM # No comments and spaces around here, or it will not work.
text4
EOM
명령 출력을 파일로 리디렉션 할 수 있습니다.
$ cat file > copy_file
또는 그것에 추가
$ cat file >> copy_file
직접 쓰려면 명령은 echo 'text'
$ echo 'Hello World' > file
#!/bin/bash
cat > FILE.txt <<EOF
info code info
info code info
info code info
EOF
나는 이것이 지독한 오래된 질문이라는 것을 알고 있지만 OP는 스크립팅에 관한 것이므로 Google이 나를 여기로 데려 갔다는 사실 때문에 동시에 읽고 쓰기 위해 파일 설명자를 여는 것도 언급해야합니다.
#!/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>&-
그런 다음 cat
터미널의 파일
$ 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 :)
Anyhow, fd's can be used in a lot of interesting ways.
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
Like this:
cat >> FILE.txt <<EOF
info code info
...
EOF
Moving my comment as an answer, as requested by @lycono
If you need to do this with root privileges, do it this way:
sudo sh -c 'echo "some data for the file" >> fileName'
For environments where here documents are unavailable (Makefile
, Dockerfile
, etc) you can often use printf
for a reasonably legible and efficient solution.
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
Can also use here document and vi, the below script generates a FILE.txt with 3 lines and variable interpolation
VAR=Test
vi FILE.txt <<EOFXX
i
#This is my var in text file
var = $VAR
#Thats end of text file
^[
ZZ
EOFXX
Then file will have 3 lines as below. "i" is to start vi insert mode and similarly to close the file with Esc and ZZ.
#This is my var in text file
var = Test
#Thats end of text file
참고URL : https://stackoverflow.com/questions/11162406/open-and-write-data-to-text-file-using-bash
'IT story' 카테고리의 다른 글
루비 : 해시를 HTTP 매개 변수로 바꾸는 방법? (0) | 2020.05.09 |
---|---|
Composer를 실행하면“입력 파일을 열 수 없습니다 : composer.phar” (0) | 2020.05.09 |
에 집중할 수 있습니까 (0) | 2020.05.09 |
오른쪽에 4 개의 탐색 모음 항목 부트 스트랩 (0) | 2020.05.09 |
SQL에서 월 번호를 월 이름 함수로 변환 (0) | 2020.05.09 |