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. |
echo $char >>textfile.txt
All this assumes you've previously defined the variable $char (e.g. char=b). |
man write
write - send a message to another user $ man putch No manual entry for putch $ putch bash: putch: command not found ??? |
Quote:
|
Quote:
|
Code:
echo "$char" >> textfile.txt And I forgot printf, too :( |
Quote:
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 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. :-(
|
Quote:
I ran this program, and it created a copy of a file, except for all white-space characters were removed. |
Quote:
|
What's the problem?
Code:
#!/bin/bash |
Quote:
|
$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". |
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." |
Quote:
|
Quote:
Pushing 90 degrees here again today I'm sorry to say. Now where did I put those salt tablets? |
If you like C and want to do scripting very similar to it you might want to investigate Perl.
|
Quote:
|
Quote:
|
Quote:
|
Quote:
|
Quote:
Code:
#!/bin/sh |
"^[" in the above script doesn't mean two characters: ^ and [ but one character: escape. To get it you could use vim. It's enough to go to editing mode, to press Ctrl-V for literal mode, and to press Esc for escape character.
If you're not acquainted with vim try that sequence: Open the script with the command: vim your_script_name. Go to the desired position using arrows. Press i (lowercase i) for insert mode. Press Ctrl-V (Ctrl and v) for literal mode. Press Esc for escape character. Press Esc to exit editing mode. Press ZZ (uppercase Z twice) to save changes and exit vim. |
All times are GMT -5. The time now is 07:26 AM. |