Reputation: 2658
As I know, the bash script can create and write file to disk path
or /dev/shm
, but the file was accessed by root
or other user. How can I set the file's permission that only accessed by current bash script process? And I will rm
this file before exit
the bash script.
Upvotes: 0
Views: 598
Reputation: 52439
You can redirect a filename to a given descriptor number, and delete the file, and then access it through the descriptor:
#!/usr/bin/env bash
name=$(mktemp)
exec {fd}<>"$name"
rm -f "$name"
echo foo >&$fd
cat </dev/fd/$fd
Using a descriptor that's been opened for both reading and writing with <>
is tricky in bash
, see Bash read/write file descriptors — seek to start of file for the logic behind that cat
line at the end.
If you've never seen the {name}<>filename
style redirection before, it automatically assigns an unused descriptor to the file and stores its number in $name
.
Upvotes: 1