1

I am just wondering if it is good programming practice to use shell scripts in systems programming.

I am new to systems programming and am trying to figure out the best way to discover printers on a given system and display the results.

The easiest way I can think of is to run an exec command to run a shell script that will list the printers using

lpstat -a > printers.txt

and then parse the file printers.txt, display the information and then remove the file when done.

1
  • 1
    Using shell scripts is fine, but make sure you use vanilla sh and do not rely on bashisms! Commented Jun 13, 2013 at 22:30

2 Answers 2

3

You're running the shell script from within code in another language? In that case using a shell script is almost certainly unnecessary.

Virtually all languages allow you to run an arbitrary command and capture the output. For example, C and friends have popen() and Perl, Ruby and such have backticks (`lpstat -a` returns the output of that command as a string). This kind of approach makes it unnecessary to have an extra shell script or a temporary file.

Sign up to request clarification or add additional context in comments.

3 Comments

One more point here, you would also want to be careful that if you rely on external scripts - you make sure there is a contingency plan if the script isn't available. For example, you have sudo to execute commands as another user, but what happens if sudo is not installed on the system?
My main reason for trying to do this was that I was having trouble getting exec to work with multiple arguments. That's why I was trying to use a script to make a file I could parse to display the results in a GUI. I am not really sure what the best practices are.
2d char arrays for argv (and envp) should be sufficient for most exec functions, or use popen on posix compliant systems. (see man pages for each exec variant) if you are going to call a shell script, you might as well write the whole thing in shell.
1

It is better to do it with a script OR with a compiled language. You can implement this task with Bash, or Python, or Perl. Mixing languages is also OK, but that complicates the code unnecessarily so you need a good justification.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.