LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 09-16-2006, 05:04 AM   #1
yhus
Member
 
Registered: May 2004
Posts: 75

Rep: Reputation: 15
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
 
Old 09-16-2006, 05:19 AM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
Do you mean something like this?
Code:
echo -n "Type in a number: "
read ANSWER
echo $ANSWER >> afile
 
Old 09-17-2006, 04:04 AM   #3
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
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
 
Old 09-17-2006, 09:22 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,794

Rep: Reputation: 496Reputation: 496Reputation: 496Reputation: 496Reputation: 496
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
Code:
< somefile
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
 
Old 09-17-2006, 10:18 PM   #5
Kristijan
Member
 
Registered: Sep 2003
Location: Melbourne, Australia
Distribution: NetBSD 3.0.1, Slackware 10.1
Posts: 394

Rep: Reputation: 30
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
 
Old 09-18-2006, 02:40 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,430

Rep: Reputation: 2788Reputation: 2788Reputation: 2788Reputation: 2788Reputation: 2788Reputation: 2788Reputation: 2788Reputation: 2788Reputation: 2788Reputation: 2788Reputation: 2788
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.
 
Old 09-18-2006, 12:23 PM   #7
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Should be dead simple - read/write serial port from bash ericcarlson Linux - Software 3 10-21-2006 08:35 PM
Need to write a bash script imagineers7 Linux - General 5 05-09-2006 11:17 PM
Bash: how to write a wrapper script? J_Szucs Programming 0 01-29-2005 05:50 PM
Bash: read/write the interface? J_Szucs Programming 7 06-04-2004 03:09 PM
Anoyone willing to write a BASH script for me? pilot1 Programming 4 09-16-2003 08:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration