read write in bash script
Hi,
I can use a "read" command to read a file in bash script, is there a "write" command I can write a value to the file? If not, how can I write a numerical value to a file? Thank you. Jim |
Do you mean something like this?
Code:
echo -n "Type in a number: " |
Quote:
Code:
# Write a number to a file. Will destroy any existing file. |
Quote:
The standard input can be a file with redirections or a pipe though. The "command" to read a file with the shell is Code:
< somefile Another way to read a file with the shell can be: Code:
content=$(<somefile) Quote:
Code:
value=12.3456 |
Is there a way I can write to certian sections of the file?
Example, just say I wanted to write a script that would write to a pf.conf file for packet filtering. Now PF reads from top to bottom, so I want to be able to order the rules how I'd like them. Is there a way to insert rules into certian sections of a file? The above is just an example...Just say I wanted to write text to a certian line in a file etc... Is this possible with bash scripting? -Kristijan |
To be honest, to do that in general you'd have to write a script to read in the file, then re-write it (to a new version when testing).
It is do-able in shell using the above techniques, although I'd prob use Perl for something like that, cetainly if you want to be able to interact with the prog eg try out different rule set orders over and over. Shell is a bit clunky for that. |
It's really not that hard to edit a file from shell. That is exactly whet sed was written to do. I'm not sure exactly what you are wanting to do but I think it's something like this:
$ cat file Rule No. 1 Rule No. 2 Rule No. 4 Rule No. 5 # Lets insert Rule No. 3 $ sed -i 's/^Rule No. 2$/Rule No. 2\nRule No. 3/' file $ cat file Rule No. 1 Rule No. 2 Rule No. 3 Rule No. 4 Rule No. 5 |
All times are GMT -5. The time now is 05:26 PM. |