Linux - NewbieThis 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
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.
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.
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.
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.
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. :-(
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.
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.
#!/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.
#!/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?
$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".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.