开发者

User defined command

开发者 https://www.devze.com 2023-01-31 09:46 出处:网络
I am having a situation for which I am looking for some suggestion. Suppose I write a program which prints the d开发者_如何转开发irectory names of the directories.

I am having a situation for which I am looking for some suggestion.

Suppose I write a program which prints the d开发者_如何转开发irectory names of the directories. Is it possible to convert this program into a command (just on my system). Not be alias but via C only.


As long as the file is executable (has the exec x access for the user starting it) and can be seen from the command interpreter (usually bash or sh), you can consider it to be a command.

There will be no difference in running your own file from your path than the ls command for instance.

Also, the C (or C++ ...) language is not a requirement. There are plenty of commands in, for instance, /usr/bin that are a script, meaning they're sh or bash (or even perl)...

  • access Ensure the file has the x access right (e.g. chmod u+x file)
  • path Ensure the file is in your PATH, or add an entry in your path (for instance) with PATH=$PATH:mypath
  • test Test it well before to put it in a path from which other users may have access


Put it in the path. On Linux, for example, you should put it in /usr/local/bin.


First, compile the program and create an executable using gcc program.c -o myexecfile. Then, an executable file named myexecfile is created in the same directory. You can run it by using ./myexecfile.

If you are on Unix(Linux etc.) and want to use it like ls or any other standard command, you need to place it in a directory that is specified in $PATH variable. For example, /usr/local/bin.

0

精彩评论

暂无评论...
验证码 换一张
取 消