How to write String to a text File - bash

My question is about file handling in bash.
I want to write a string to text file in Bash script.
I have already tried
echo string>>filename.txt

your code is correct.you can write to a file using that.But you have to check the file access permission of the file you want to write the data in to.
Check following for more details.
Create text file and fill it using bash
Open and write data to text file using bash/shell scripting

That is a valid way of doing it. I would however consider this:
echo "string" >> file.txt
The quotes make it possible to handle more than one word (in the case of echo, that works anyway, but it's good practice to always use it). You should also distinguish between > and >>
> truncates the file (erases it) and replaces its contents with your string
>> on the other hand appends your string to the bottom of your file

Related

What does the redirection operator "<" do in shell scripts?

I came across the below shell command:
$prog.sh < file_name.json
I know it reads from a file, but how and where does prog.sh load the file?
Every program has three open file handles at startup, one of which is standard input. Normally, the file handles are inherited from the parent process. The < operator tells the shell that, instead of passing its standard input to prog.sh, to open file_name.json instead and give that file handle to prog.sh as its standard input.
$prog.sh < file_name.json
As you rightly guess. The < is meant for redirecting the input from a file so that your script will read from the file which will be the (temporary) stdin(fd0).
it read from a file, but how and where prog.sh will load the file
It depends on how you plan to go about it. Any command in the script that expects an input from the stdin will now read from the file. The new line character in the text file (usually) stands for the ↵ in the stdin.

Shell script which will create a text file

I need to write a shell script that will do the following: will allow to create a text file from the keyboard and would find out the file type.
Could you help me? Thank you
to create a text file from keyboard input, you can use
cat > yourfile
which will send everything you type to yourfile, until you type ctrl+D on an empty line. or else, you can use any text editor. In all cases, the user will need to know how to terminate the input.
Then, if you want to identify the content of this file, you might suppose that the first line is a shebang (typically :
#!/bin/bash
and analyse it.

Piping input from a file to a command in windows cmd

My understanding is that the redirection operator, <, should allow me to take text from a file and give it as input to another file as if I had written out the contents of that file. Here is what I am trying to do:
python code.py < input.txt
I expect this to act as though I had typed the contents of input.txt after python code.py, but instead it acts as if I passed no input.
If I use cat, I get the contents of the file:
> cat input.txt
['2015-1-1','2015-5-1','2015-9-1','2015-10-1','2015-12-1','2016-1-1','2016-2-1','2016-4-1','2016-5-1'] [65,50,30,45,55,39,45,30,20]
And if I just copy and paste the contents of the file, I get the correct behavior.
I know this must be a really simple misunderstanding on my part, but I can't figure it out.
It's called Redirection, not piping, but you are correct that the < operator will push the file to the command. You can see this in action by using Sort instead of echo.
sort < input.txt
This will display the text file as a list, sorted alphabetically. Echo does not work with text files, so sending a text file to Echo simply runs "Echo".
If you just want to send a file to the command window, you can use Type instead, and not use the redirector.
type input.txt

In bash or shell, how does one input a line of text as a file without making a temp file?

Problem 1:
I'm using a self-made command, that for reasons, cannot be changed. The command requires a filename that it will read from like so: read-cmd "testtext.txt" I know it's possible to use files as a stream of input for some commands using input redirection, e.g. some-cmd < "text.txt", but I'm wondering if the opposite is true, whether I can use a line of text and make bash believe it's a file, so that I'd be able to do read-cmd "contents of what should be in a text file"
the only thing I've been able to do is
Example 1:
echo "contents of what should be in a text file" > tmptextfile
read-cmd "tmptextfile"
rm tmptextfile
I would however, really rather not do this, and rather just stream that line in as if it were a file. Is there any possible way to do this, or would it rely entirely on how read-cmd works?
Problem 2:
A very similar issue, however, instead of the file being an input to a command, it is the input to an option of a command. So, read-cmd2 -d "testtext.txt" ...
Example 2:
echo "contents of what should be in options text file" > tmpoptfile
read-cmd2 -d tmpoptfile ...
rm tmpoptfile
whether I can use a line of text and make bash believe it's a file,
Yes you can use process substitution for this:
read-cmd <(echo "contents of what should be in a text file")
Process substitution is a form of redirection where the input or output of a process (some sequence of commands) appear as a temporary file.

Append or create a file, in one command?

Very simple task: grab an output and redirect it to a file, using shell.
Now, I need to create the file if it does not exist, or if it exist, I need to append data
If I use output > file.txt I get data overwritten at each access. Not what I want.
If I use output >> file.txt I get an error, because shell is trying to access the file while, I need it to create it first (it does not exist yet).
Is there a way to say in one line to create it, if the file doesn't exist, and append data or append only if the file does exist already?
I can just do that using an if-else (if file exist use >> otherwise use >), and I would like to avoid to complicate my code, if there is an easier way.
to create or append to a file in bash, you can use >>.
echo "sample text" >> sample_file
echo "append text" >> sample_file

Resources