LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-14-2009, 12:14 PM   #1
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 897

Rep: Reputation: 70
printing a character to file


Hi all,

Any script kiddies out there?

what's the best way to send a single text character (append it) to a text file. I've tried:

$char >> textfile.txt

but Bash doesn't understand it. I then tried:

write $char >> textfile.txt

But still no go. Similarly:

putch$char >> textfile.txt

and no joy either.

How should it be done properly?

thanks, cc.
 
Old 08-14-2009, 12:16 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
echo $char >>textfile.txt

All this assumes you've previously defined the variable $char (e.g. char=b).
 
Old 08-14-2009, 12:24 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
man write
write - send a message to another user

$ man putch
No manual entry for putch
$ putch
bash: putch: command not found

???
 
Old 08-14-2009, 12:53 PM   #4
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 897

Original Poster
Rep: Reputation: 70
Quote:
Originally Posted by jlightner View Post
echo $char >> textfile.txt

All this assumes you've previously defined the variable $char (e.g. char=b).
Thanks. It doesn't seem to work if the character in question is a space, however. Spaces get stripped from the output file. Any way of fixing that?
 
Old 08-14-2009, 12:54 PM   #5
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 897

Original Poster
Rep: Reputation: 70
Quote:
Originally Posted by catkin View Post
man write
write - send a message to another user

$ man putch
No manual entry for putch
$ putch
bash: putch: command not found

???
Good point. I forgot about "printf" :-)
 
Old 08-14-2009, 12:58 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Code:
echo "$char" >> textfile.txt
Generally squeaking it's good practice to always double quote variables to avoid such gotchas.

And I forgot printf, too
 
Old 08-14-2009, 02:26 PM   #7
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 897

Original Poster
Rep: Reputation: 70
Quote:
Originally Posted by catkin View Post
Code:
echo "$char" >> textfile.txt
Generally squeaking it's good practice to always double quote variables to avoid such gotchas.

And I forgot printf, too
I did try double and single quotes before making that post. I know they can have a big impact on formatting. However, still no joy. Everything comes out space-expanded. I mean letters between individual words become single-spaced; words are separated by double spaces! You can't win!

I reckon C is far more suited to this particular task, so I'm going to switch at this point. Thanks for all the ideas anyway, guys.
 
Old 08-14-2009, 02:40 PM   #8
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 897

Original Poster
Rep: Reputation: 70
One more thing I thought I should add. Here is the source code:

Code:
#!/bin/bash
#process a text file character-by-character and send the output to
#a separate file. Input filename is textfile; output filename is
#outputfile. No character translations required at this stage.
# the variable '$char' is the individual file character under test

touch outputfile ; chmod 666 outputfile


while read -n1 char
do

  echo -n "$char" >> outputfile


done < textfile.txt
It should just produce a copy of a text file in the same directory. In practice however, the 'copy file' is MUCH larger than the original, appears to have been processed several times in different ways, and contains a lot of gobbldegook that looks astonishly like the output of ps -e processes listing!!

I'll post the input and output files if anyone's interested in finding out why this happens.
 
Old 08-14-2009, 02:58 PM   #9
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 897

Original Poster
Rep: Reputation: 70
Ah! I wonder if the problem could be due to me using 'touch' to create an empty file to accept the output? Maybe on subsequent re-runs of the program, it won't overwrite a file of the same name? Not much info in the man page. :-(
 
Old 08-14-2009, 03:34 PM   #10
nuwen52
Member
 
Registered: Feb 2009
Distribution: Debian, CentOS 5, Gentoo, FreeBSD, Fedora, Mint, Slackware64
Posts: 208

Rep: Reputation: 46
Quote:
Originally Posted by Completely Clueless View Post
Ah! I wonder if the problem could be due to me using 'touch' to create an empty file to accept the output? Maybe on subsequent re-runs of the program, it won't overwrite a file of the same name? Not much info in the man page. :-(
I don't think that's a problem. The only thing touch will do on a pre-existing file is to update it's access/modification times. Touch will not delete everything in the file if it exists. A second/third/etc. run would just create an ever growing output file.

I ran this program, and it created a copy of a file, except for all white-space characters were removed.
 
Old 08-14-2009, 04:18 PM   #11
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 897

Original Poster
Rep: Reputation: 70
Quote:
Originally Posted by nuwen52 View Post
I don't think that's a problem. The only thing touch will do on a pre-existing file is to update it's access/modification times. Touch will not delete everything in the file if it exists. A second/third/etc. run would just create an ever growing output file.

I ran this program, and it created a copy of a file, except for all white-space characters were removed.
Yeah, but that's what i'm getting at. Try changing the program in some small way so it will produce a different output and see if the output file is the same. in my experience it will be. Touch has not overwritten it, nor given any warning it hasn't.
 
Old 08-14-2009, 04:27 PM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
What's the problem?
Code:
#!/bin/bash

echo -n 'abcdef' > input.txt
cat /dev/null > output.txt
while read -n1
do
	echo -n "$REPLY" >> output.txt
done < input.txt

# The following command produced no output if the files are identical
diff input.txt output.txt
The only problem is if there is a newline at the end of input.txt; it is not copied.
 
Old 08-14-2009, 04:36 PM   #13
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 897

Original Poster
Rep: Reputation: 70
Quote:
Originally Posted by catkin View Post
What's the problem?
Code:
#!/bin/bash

echo -n 'abcdef' > input.txt
cat /dev/null > output.txt
while read -n1
do
	echo -n "$REPLY" >> output.txt
done < input.txt

# The following command produced no output if the files are identical
diff input.txt output.txt
The only problem is if there is a newline at the end of input.txt; it is not copied.
Sorry, but where is $REPLY defined/assigned/declared?
 
Old 08-14-2009, 04:52 PM   #14
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
$REPLY is the default variable set by the read command.

I can't find it in the Gnu bash Reference or in the POSIX Shell Reference but it's in the Advanced Bash-Scripting Guide and the bash man page: "If no names are supplied, the line read is assigned to the variable REPLY".
 
Old 08-15-2009, 05:02 AM   #15
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 897

Original Poster
Rep: Reputation: 70
Okay, thanks for the clarification, Charles. However, as I stated earlier in this thread......

"I reckon C is far more suited to this particular task, so I'm going to switch at this point. Thanks for all the ideas anyway, guys."
 
  


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
Bash scripting: parsing a text file character-by-character Completely Clueless Programming 13 08-12-2009 09:07 AM
CUPS not printing character JMJ_coder Linux - Software 6 03-23-2007 02:36 PM
Printing the octal representation of a character. smoothdogg00 Programming 1 03-02-2006 10:44 PM
ncurses printing character array prob. slzckboy Programming 3 07-31-2005 08:51 AM
Problem with reading and printing a character hubabuba Programming 3 02-28-2005 02:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:28 AM.

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