Is it possible to have a file with variable content? (It's probably not possible but worth asking as other might know a solution.)
For example the content of file mydate.txt should always be what this bash (it can be any scripting language, used bash just for simplicity) snippet produce:
#!/bin/bash
date
So when I try to see the content of the mydate.txt file it should actually display current date.
For example
cat mydate.txt
should display
Fri Dec 23 09:54:05 AM EET 2022
CodePudding user response:
The given example and description looks (to me) like a (simple) wrapper function to execute the provided file ... if the file has execute permissions enabled:
mycat() { for fname in "$@"; do ./"${fname}"; done; }
Taking for a test drive:
$ mycat mydate.txt
Fri Dec 23 11:02:20 CST 2022
$ mycat mydate.txt mydate.txt mydate.txt
Fri Dec 23 11:21:07 CST 2022
Fri Dec 23 11:21:07 CST 2022
Fri Dec 23 11:21:07 CST 2022
NOTES:
- if the file has not been marked as executable it gets (quite) a bit more complicated (eg, sourcing a file bypasses any shebang that may be needed for the contents of the script to 'run' correctly; do you have permission to (re)set the 'execute' bit?)
- while this 'works' for the (relatively) simple example, I'm guessing it probably won't work in all of OP's scenarios in which case we'd need a more detailed problem description (and more examples)
CodePudding user response:
As suggested, the best answer is to use a FIFO (First In, First Out) file, maybe like this:
#!/bin/bash
[ -f "catme.txt" ] && rm catme.txt # cleanup if needed
mkfifo catme.txt
trap 'rm catme.txt ; exit 1' SIGINT. # try to clean up the nice way
while true
do
date > catme.txt
printf "data provided on %s\n" "`date`"
done
run the command and use control-c to exit it. Then in another terminal you would call cat catme.txt
and you will see our current date. This may not be what you want, but you have to attach a process to something dynamic in unix, else you can try running the command from your other script.