I use sed
to change text in existing files and Nano to create new files.
I change text with sed
this way, for example:
sudo sed -i 's/TESTING === "1"/TESTING === "0"/g' /etc/csf/csf.conf
Is there a way to create files with it, without going inside Nano or VI and pasting text, giving permission and then execute?
Usually I do cd ~ && nano script.sh && chmod +x script.sh && ./script.sh && rm -rf script.sh
and then I paste all the script content inside, then save and execute.
This time however, I want to automate even that, and just paste everything as one input and run it in place.
In other words, I look for either a way to run the script from paste, or to paste it into a temporary file and run it "in-place" the moment I close the file and by that, to run it with the least number of clicks and pastings.
Why I need this solution:
The process can be repetitive when working with several server environments and I want to save time when doing this task time and again.
The discussion about the legitimacy of such action is surly a discussion by itself...
Here is an example --- I paste the following coomand to prompt (the first one, that includes ampersands), go into Nano to create the script and right then, I paste the following script syntax, save, and it it is being executed. I wish to automate this process as to do all of this, in one pasting / one action.
cd /home/testo && sudo nano script.sh && sudo chmod +x scripts.sh && sudo ./script.sh
#!/bin/bash -x
# Basic update and upgrade:
sudo apt-get update -y
sudo apt-get upgrade -y
# Setup CSF-LFD:
sudo rm -f csf.tgz
sudo wget https://download.configserver.com/csf.tgz
sudo tar -xzf csf.tgz
sudo sh install.sh
sudo perl /etc/csf/csftest.pl
sudo sed -i 's/TESTING === "1"/TESTING === "0"/g' /etc/csf/csf.conf
sudo csf -r
sed
but why would you want to usesed
? Have you ever heard ofheredoc
s ? – don_crissti Nov 01 '16 at 14:24sed 's/old/new/' paste_source > new_file
. Apart form that: If you usesudo script.sh
all thesudo
s in the script are negligible. – FelixJN Nov 01 '16 at 16:06nano
? As I said, all you need is a heredoc. And please edit the title here, your question is not how to create files with sed. – don_crissti Nov 10 '16 at 23:36sudo
here. Particularly runningsudo wget
made me cringe a bit. Also, if you're running the script withsudo ./ses.sh
, none of thesudo
in the script actually do anything... – Chris Nov 15 '16 at 21:52