简体   繁体   中英

Modifying text files and executing programs with command line parameters in c# or c++ on Linux

I have a need to create a utility in Suze Linux. The utility will make modifications to some text files, and then use the information in those text files to program a device in the computer using a different executable which accepts command line parameters.

I am fluent in c#, but have never worked with Linux. Should I take the time to learn Gnu C++ to do this, or install Mono? How would I execute the programming utility and pass it command line parameters?

In C and C++ there's system("command arguments") which you can use to execute a command using the system's interpreter (ie you can use it as though you had typed in the command in the shell). The command string can be constructed at runtime. I'm not very familiar with C#, but if I recall correctly you can use Process and ProcessStartInfo classes to run system commands.

Based on the complexity of your program I'd recommend using a scripting language like Perl. It's always a good idea to have a scripting language in your toolbox.

Is there a reason you want to restrict yourself to only C++ or C#? There are many options you could consider, for example:

For very simple tasks:

  • Bash : In some cases a simple Bash script will be able to solve the task. Piping information from one process to another is a breeze, and you have the power of sed, awk, etc. at your fingertips. Another major advantage is that it is installed almost everywhere.

For slightly more involved tasks you could try a scripting language:

  • Python : Easy to learn, pleasant to read. Very useful for putting together small applications quickly. You can use subprocess to communicate with other processes.
  • Ruby : Similar comments to Python - a good scripting language with a clean syntax.
  • Perl : Perl is very good at processing text, although personally I dislike the syntax.

Other options:

  • Java : Your C# experience will allow you to learn Java quickly as it is a very similar language. Java is officially supported on Linux.
  • C# : A lot of Linux users are wary of C# because it isn't free enough, but obviously that isn't a worry for you. It has worked fine the few times I've tried it. Note that Mono is not 100% compatible with Microsoft's version.
  • C++ : For what you're planning to do I personally wouldn't recommend C++. It will solve the task though, and if you already know some C++ then I guess it is worth considering.

Obviously there are many other suitable options too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM