I currently have a c++ Linux program that reads a parameter “P” from a file and loads it in RAM for further operations. The file has the following line :
P = 123
I would like the program to take P from shell input instead of the file. I am open to all options, as long as I can manually enter P while connected in SSH.
What I have in mind is something like an input prompt :
sudo myprogram start
enter P value : (I would manually enter "123" here)
Or maybe an argument :
sudo myprogram start 123
It must be simple to do but I do not know how, so any help is greatly appreciated !
If this is the only data that the file has then the file operation is needless.
Simply pass 123 (or whatever) to your C++ program and convert the string into integer.
Assuming you pass the integer as the second argument then:
A better option is to use strtol:
If you can’t make changes to the C++ code then simply do:
If your file has more content and you can’t simply do echo then, replace the existing line with new value: