How to write Linux script that adds custom lines to files and executes a command?

OpenSource Ghost

Limp Gawd
Feb 14, 2022
I have a router that is mostly controlled by API, but it allows SSH access to make changes. Some custom parameters I add to hostapd (WiFi) files do not survive reboot. There is already a tool that runs any selected .SH scripts on boot, but the only scripts I know how to use are basic execution of commands for common network tools such as IPTables, which simply requires creating a file that has desired "sudo iptables..." lines and "#!/bin/sh" at the top of the file.

I need a script that adds lines, such as "dtim_period=2", to "/etc/hostapd/radio0.cfg" file and then restarts hostapd process via "killall -9 hostapd" command to apply new parameters added to "radio0.cfg". That is what I have to do manually after every reboot. There may not be a simply way to do this and may require understanding of how to write cron jobs, but that's beyond my level...
echo dtim_period=2 >> /etc/hostapd/radio0.cfg
killall -9 hostadp
That works, but it removes all other lines from the file. It makes the file contain the one and only "echo" line I specify. I need to add needed lines to the ones already present and generated by router API.
That works, but it removes all other lines from the file. It makes the file contain the one and only "echo" line I specify. I need to add needed lines to the ones already present and generated by router API.
>> should append to the end of the file, > will replace the contents (note two vs one). You could also look up what program is modifying that file and change it's configuration instead -- with that you wouldn't need to run your own script each boot.
How do I write an SH script that deletes 2 files from a directory? Router API re-generates weak SSH key pair after each reboot and stores both public and private keys on the router. That's not normal. I want my router to have only the custom public key I generated and me having the private key. I already have a script that stores my self-generated public key on the router after reboot, but it doesn't delete the router-generated pair.
I tried the following, but it didn't work:
rm -f "root\mnt\data\ssh\id_rsa"
rm -f "root\mnt\data\ssh\"
If it's a linux shell, those should be forward slashes (/), and if you aren't mounting the drives locally but instead using ssh or some other remote shell, then the "root" directory should be "/".
Oh, and "/root" is the "root" user's home directory. It's usually empty, except for some config files. Certainly doesn't usually have a "mnt" directory, anyway.

Might help to use `ls -lh` to list the files and directories in / and /root

Edit: just occurred to me, `ls` might not be on the router. In that case, I guess use `dir`.
Last edited:
You should probably also learn the difference between
in shell scripts.
Thanks all for your input! With your help I managed to automate on-boot process for my router to compensate for some of its shortcomings!

There is one more on-boot script I need to not have to perform any manual work via SSH after every router reboot. I need to have several directories (with files) from my PC copied to respective router root directories and overwrite router files. I can save those directories and files in router directory where they can survive reboot, but I need a script to have them copied from that survivable directory to router root directory. How can I do that? It would also be nice if I could package those files into archive and have script extract them from that file to router root directory, but this router does not accept "dpkg" command in normal shell.

Also, there is OS shell container, which changes its "/root/proc" directory PID after every reboot. In order to navigate to it after a reboot, I have to run "top" command, find the process name PID, and only then I can navigate to it through "/root/proc/PID". I don't have to write files to it, but it would be nice to know how to get to that directory without having to run "top" command. I use WinSCP and it can create shortcuts, but how do I create a shortcut to a directory that changes after every reboot?

Finally, based on questions I ask, what are some basic Linux CLI courses/lessons should I take? I don't want to keep this thread going forever :) .
Directory that survives reboot is "/etc/boot/" and directory where files need to be copied is "/etc/local/". I need a command that copies all files from "/etc/boot/" to "/etc/local/" and overrides any files with identical name. The only reason I need to copy those files is because vanilla files in "/etc/local" are restored from restricted firmware directories upon reboot. I think the script is as simple as
"cp –R /root/etc/boot /root/etc/local" .