Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
28
rated 0 times [  28] [ 0]  / answers: 1 / hits: 156541  / 1 Year ago, wed, november 23, 2022, 12:45:14

I'm a little new to Shell Scripting and I want to create a new file inside the script and want to add content and then close it. It should not take the arguments from the user. Everything from the path and content is predefined. How can I do it?


More From » bash

 Answers
5

Just use output redirection. E.g.



#!/bin/bash

echo "some file content" > /path/to/outputfile


The > will write all stdin provided by the stdout of echo to the file outputfile here.



Alternatively, you could also use tee and a pipe for this. E.g.



echo "some file content" | tee outputfile


Be aware that any of the examples will overwrite an existing outputfile.



If you need to append to a currently existing file, use >> instead of > or tee -a.



If you don't accept user input in this line, no user input can change the behaviour here.


[#30328] Thursday, November 24, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
motivengry

Total Points: 459
Total Questions: 112
Total Answers: 108

Location: Bonaire
Member since Sat, May 1, 2021
3 Years ago
motivengry questions
Tue, Oct 4, 22, 10:02, 2 Years ago
Wed, May 31, 23, 14:33, 12 Months ago
;