Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
09-16-2006, 05:04 AM
|
#1
|
Member
Registered: May 2004
Posts: 75
Rep:
|
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
|
|
|
09-16-2006, 05:19 AM
|
#2
|
Senior Member
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141
Rep: 
|
Do you mean something like this?
Code:
echo -n "Type in a number: "
read ANSWER
echo $ANSWER >> afile
|
|
|
09-17-2006, 04:04 AM
|
#3
|
Senior Member
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802
Rep:
|
Quote:
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?
|
No there is a write command to send a message to another user. To write to a file there are many options:
Code:
# Write a number to a file. Will destroy any existing file.
$NUM=1000;echo $NUM >num;cat num
1000
# Same thing but will NOT destroy existing file instead will append to the end.
$NUM=2000;echo $NUM >>num;cat num
1000
2000
# Edit an existing file using sed, also you can make a backup using -i.bak.
$sed -i 's/$/.00/g' num;cat num
1000.00
2000.00
# Open a file for R/W access. Close file when done.
$exec 3<> num;read -n 1 <&3;echo -n 9 >&3;read -n 7 <&3;echo -n 3 >&3;exec 3>&-;cat num
1900.00
3000.00
|
|
|
09-17-2006, 09:22 AM
|
#4
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,794
|
Quote:
Originally Posted by yhus
Hi,
I can use a "read" command to read a file in bash script
|
Not exactly, read is meant to read one ore more variables from the shell standard input.
The standard input can be a file with redirections or a pipe though.
The "command" to read a file with the shell is
This command is mostly useless, as nothing is done with the file.
Another way to read a file with the shell can be:
Code:
content=$(<somefile)
Then, you'll have everything the file contains in the "content" variable.
Quote:
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?
|
There are plenty of ways, here is one:
Code:
value=12.3456
printf "%.2f\n" $value >> /tmp/somefile
|
|
|
09-17-2006, 10:18 PM
|
#5
|
Member
Registered: Sep 2003
Location: Melbourne, Australia
Distribution: NetBSD 3.0.1, Slackware 10.1
Posts: 394
Rep:
|
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
|
|
|
09-18-2006, 02:40 AM
|
#6
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,430
|
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.
|
|
|
09-18-2006, 12:23 PM
|
#7
|
Senior Member
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802
Rep:
|
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 03:20 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|