LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 10-26-2010, 03:37 PM   #1
realpockets
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Rep: Reputation: 0
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
 
Old 10-26-2010, 03:42 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,581
Blog Entries: 1

Rep: Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033
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/
 
Old 10-26-2010, 03:50 PM   #3
realpockets
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Original Poster
Rep: Reputation: 0
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

Last edited by realpockets; 10-26-2010 at 03:54 PM. Reason: forgot to post something
 
Old 10-26-2010, 04:30 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,581
Blog Entries: 1

Rep: Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033
No, 'mkdir', etc. are not scripts. They are binary files written in C so that's what you could
start off with.
 
Old 10-26-2010, 05:50 PM   #5
John VV
Guru
 
Registered: Aug 2005
Posts: 13,283

Rep: Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775
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/
 
Old 01-17-2011, 08:47 PM   #6
cin_
Member
 
Registered: Dec 2010
Posts: 266

Rep: Reputation: 23
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";;

Last edited by cin_; 07-23-2011 at 10:01 AM. Reason: gramm'err
 
Old 01-17-2011, 10:28 PM   #7
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7
Posts: 866

Rep: Reputation: 110Reputation: 110
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.
 
Old 01-17-2011, 10:42 PM   #8
lumak
Member
 
Registered: Aug 2008
Location: Phoenix
Distribution: Arch
Posts: 799
Blog Entries: 32

Rep: Reputation: 109Reputation: 109
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.

Last edited by lumak; 01-17-2011 at 10:44 PM.
 
Old 01-18-2011, 09:47 AM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
Quote:
Originally Posted by realpockets View Post
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 View Post
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.
 
Old 01-18-2011, 11:15 AM   #10
cin_
Member
 
Registered: Dec 2010
Posts: 266

Rep: Reputation: 23
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

Last edited by cin_; 07-23-2011 at 10:00 AM. Reason: gramm'err
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Make linux terminal transparent with terminal command? yooy Linux - Newbie 1 05-11-2010 04:24 AM
Running command in gnome-terminal from script results in "...error creating child..." PlancksCnst Linux - General 6 04-08-2009 01:09 PM
how to make Linux router by creating Initrd manually and from scratch nikhilpavithran Linux - Software 1 07-29-2005 09:25 PM
Creating a Linux from Scratch installer? pilot1 Linux From Scratch 6 03-04-2004 04:12 AM


All times are GMT -5. The time now is 07:24 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration