Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

For djvused command, there is an option:

-e command

Cause djvused to execute the commands specified by the option argument commands. It is advisable to surround the djvused commands by single quotes in order to prevent unwanted shell expansion.

For example, djvused myfile.djvu -e 'print-pure-txt'.

It is quite unusual to me in that a command (here djvused) can run other commands (here by -e option). I was wondering how it is possible? Is this a frequent practice in command line interface?

Is this similar to print command used in awk command?

The only way I know for a command to be used in another command is:

echo `echo hello`

Thanks and regards!

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

It is quite usual, some programs base their working exclusively on this.

Some of the more common examples that come to mind are su, sudo and xterm.

su -c 'ls -l /root'
sudo ls -l root
xterm -e 'top -d 10'

It is different from your example

echo `echo hello`

where the inverse quotes are interpreted by the shell, and the program do not execute anything itself.

Note also the difference between su and sudo. The first take a string, and could be difficult to set up such a string from the user point of view, for example to expand a variable before it is seen by the command; the second a series of string and is far more simple (there are no quotes in the sudo example).

What they use to implement their internal working? There are essentially two ways: the system library routine and the exec system call. The first will call a shell, and allow for various shell expansions, like

su -c 'ls -ld /root/.*'

while the second method do not allow such freedom.

share|improve this answer
add comment

Backticks are deprecated, because they are hardly nestable, and not easily distinguished in different fonts from apostrophs.

Instead use $(cmd), which is trivial:

echo $(echo $(echo $(echo $(echo $(echo hello)))))

And yes, it is an feature which is often wanted, even if it is rarely used.

But programs, which process commands, but don't necessarily invoke themselves again, are legion:

  • shells like bash, dash, zsh of course
  • all scriptable languages like scala, haskell, ruby, python, lua, perl, php, ...
  • more specific languages like bc, r, ...
  • sql-interfaces like psql -c "SELECT foo FROM bar" sampledb
  • remote-shells like ssh, telnet, ...
share|improve this answer
    
Thanks! In the example of djvused, that is an option followed by some sub-command quoted in single quote instead of backticks. –  Tim Jul 13 '11 at 17:41
add comment

The most excellent Vim text editor, and it's predecessor, vi, have this feature:

Type !)date when your cursor is on a blank line, and the output of the date command will get put into the blank line.

I use that little sequence when adding to files full of notes, or task lists so that I've got timestamped entries.

Again in Vim, you could put your cursor on the first line of a paragraph of text, hit V, use j to go down a line at a time to the last line of the paragraph. Hit !fmt and get your text formatted into less-than-80 column lines. Vim does highlight marking, but a variant was a common trick on older vi which just had marks: 'a,'b!fmt

As to you more technical questions, this can be done a couple of ways. The C standard library call system(3) is a common way to run an external comman. That standard library call may impose too many assumptions for what you want to do in which case you'd use the fork(2) system call to get a new process, which would change file descriptors or other things, and then call execve(2) system call to get the kernel to load a different executable file into the new process' address space.

It's a relatively common thing for a command line program to do, because it affords such great re-use of other commands.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.