LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Creating a Linux Terminal Command From Scratch (https://www.linuxquestions.org/questions/linux-general-1/creating-a-linux-terminal-command-from-scratch-840591/)

realpockets 10-26-2010 02:37 PM

Creating a Linux Terminal Command From Scratch
 
Hi everyone,

I am new to the Linux OS and I am very intrigued in learning all aspects of it. Currently I am using the Fedora 13 distro.

I am looking into creating my own terminal command from scratch. I researched online for some information on this subject was only able to view stuff related to OS X. I understand that they are both Unix based, are they both similar in creating these commands?

Basically all I would like is for someone to point me towards the right direction to start or complete this task.

Thanks for your help and support,

-rp

sycamorex 10-26-2010 02:42 PM

Hi and welcome to LQ.

You should start your script with:

#!/bin/sh

followed by other commands. You'll find the following tutorial very useful:
http://tldp.org/LDP/abs/html/

realpockets 10-26-2010 02:50 PM

Thanks for the quick reply *sycamorex.

Quick question, are linux commands such as 'mkdir', 'sed', 'rm', 'chown' ... normally first written as bash scripts and then added to the command directory so I can call them via terminal?

Ultimately I would like to create a command and not a script.

Thanks

*** this is what i mean ***

http://github.com/prtksxna/github-te...-your-commands

sycamorex 10-26-2010 03:30 PM

No, 'mkdir', etc. are not scripts. They are binary files written in C so that's what you could
start off with.

John VV 10-26-2010 04:50 PM

you might want to do some reading ?
you sound just a bit confused about things
start with DangerMouses site - for fedora
http://www.dnmouse.org/
the fedoraforum
http://www.fedoraforum.org/

The basics for ALL Linux OS's
http://rute.2038bug.com/rute.html.gz

http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/

http://www.linux.org/lessons/

cin_ 01-17-2011 07:47 PM

advent command
 
Create the script.
Save copies of your scripts to a directory of your choosing.
You can then add the directory of the scripts to your Path...
Code:

# PATH=$PATH:/dir/to/scripts
# export PATH

...then you simply call them using their filename.

Once you have a directories for your homegrown commands you can try this...

I wrote a command that takes the project I am working on and makes it a callable command.
I usually work in a different directory than the one I have appended to PATH so it has proved much easier to simply call this command instead of typing the entire path to my script directory. I like to keep the languages separate, and I bounce around a bit so I just added a second argument defining the type of script it is.

Code:

# ungaunga
bash: ungaunga: command not found
# advcomm ungaunga b
# ungaunga
mah monkeh#

/src
Code:

#!/bin/bash
case "$2" in
        "b") chmod +x $1 && cp $1 /dir/to/scripts/bash/;;
        "c") cp $1 /dir/to/scripts/c/;;
        "p") cp $1 /dir/to/scripts/perl/;;
        "py") chmod +x $1 && cp $1 /dir/to/scripts/python/;;
        "r") cp $1 /dir/to/scripts/ruby/;;
        "l") cp $1 /dir/to/scripts/lua/;;
       
        *) echo -e "\nusage: `basename $0` sets command as callable\n: b bash : c C : p perl : py python : r ruby : l lua : \n";;


jmc1987 01-17-2011 09:28 PM

Just a better understanding Mac OS derived some some Unix code but Linux did not derive from Unix. Linus Torvolds designed the kernel from scratch to resemble minux. So that makes it a unix clone.

But just another option. You can make alias's to linux commands. For example if you wanted

makedir to do mkdir you just add a alias in your bash configurations directories either in your home directory or your system wide directory.

lumak 01-17-2011 09:42 PM

Just read "advanced linux programming" it's offered as a free pdf file or you can buy the book.

http://www.advancedlinuxprogramming.com/

The question you are asking is too complicated to be asking on a 'linux general' forum.

Once you have programmed a little and need help with very specific programming question, go to the programming forum.

However, chances are, all you need is to execute a few existing commands in sequence. All you need for this is a bash script.

BTW, programming takes a lot of knowledge and understanding. I would also recommend learning at least C++ as this is a standard and generic place to start.

MTK358 01-18-2011 08:47 AM

Quote:

Originally Posted by realpockets (Post 4140108)
Quick question, are linux commands such as 'mkdir', 'sed', 'rm', 'chown' ... normally first written as bash scripts and then added to the command directory so I can call them via terminal?

Of course not! How can you write a shell script that removes a file if you don't have rm in the first place?

Quote:

Originally Posted by realpockets (Post 4140108)
Ultimately I would like to create a command and not a script.

I wanted to clear up the confusion between the terms command, script, binary, and program.

A program is a set of instructions.

A script is a program written is a scripting language (a scripting language is where there's a separate program that interprets the script every time you use it, for example Bash or Python).

A binary is a program written in a compiled language (such as C or C++) that's then been compiled. The binary runs directly on your CPU with no intermediate program.

A command is a program that's in a directory mentioned in your PATH environment variable, so that it's executed when you just type its name in a terminal.

cin_ 01-18-2011 10:15 AM

for posterities sake
 
As MTK358 explained :
Commands are globally callable programs due to their location being in the directories of your PATH.
How do they get there?
GNU creates essential programs like bash and their coreutils package.
Then each distro chooses carefully the programs that they believe are useful and want in their distro.
In this way your access to programs begins with the distro developers decisions.
Next, since GUIs are considered a standard at this point, the Desktop Environment, or the Window Manager, when loaded will carry with it software it feels is important.
Next, when you want some functionality and you find someone who has a project that fulfills this functionality and you retrieve the program, and either by your package manager or your own compilation makes it a callable command by placing the right files in the right directories.
Finally, when you want some functionality and your research for a solution returns null, then you write your own program and add it to your PATH.

Binaries are files that require you to reverse compile or find the source of to be able to read and understand its functionality. I'd suggest looking into C.
You can get the source to some of the most powerful and ubiquitous programs here... http://www.gnu.org/software/software.html

Scripts are programs written in scripting languages without compilation and so can be executed and have their source examined from the executable.
You make certain sacrifices with efficiency functionality and resource usage when working with scripting languages, but like lumak explained they are fine for executing simple command sequences, especially when those commands require modification often. I also like to prototype in a scripting language. I find it makes my C binaries come together more seamlessly.

This sounds vast and intimidating, but it really is simple.
My example of the simplicity of this process from before, shown in its entirety...
Code:

# vim ungaunga.sh  *VIM IS A COMMAND LINE TEXT EDITOR

*WITHIN TEXT EDITOR
*
* #!/bin/bash      ###CALLS AND EXECUTES A CHILD PROCESS BASH FOUND IN THE /BIN DIR
* echo "mah monkeh"  ###RUNS THE ECHO COREUTIL
*

# ls
ungaunga.sh
# chmod a+x ungaunga.sh  *ADDS(+) EXECUTE(x) PERMISSIONS TO ALL(a): OWNER, GROUP, ALL USERS
# ungaunga
bash: ungaunga: command not found
# cp ungaunga.sh /dir/appended/to/PATH/ungaunga  *REMOVED EXTENSION FOR THAT CLEAN COMMAND FEEL, CAN LEAVE IF YOU'D LIKE
# ungaunga
mah monkeh
#

Great resources...
GNU Manuals... http://www.gnu.org/manual/manual.html
GNU Source Collections... http://www.gnu.org/software/software.html NOTE: bash, coreutils


All times are GMT -5. The time now is 10:23 PM.