How to make commands in linux

How to create custom commands in Unix/Linux? [closed]

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

For future reference, you should check out the facts page. It explains the things you should put in your questions. (such as posting the things that you have researched & tried). That’s likely why someone gave this question a down vote.

4 Answers 4

Create a bash script in your /usr/bin folder, it should look something like this

#!/bin/bash Whatever combination of commands you want to run when you type this thing. 

Just name the bash script what you want to type in to the terminal, and make it excecutable: chmod +x filename and you’re good to go!

/usr/bin folder is intended for the Linux distributor. ~/bin is the right place to put personal stuff and /usr/local/bin is for custom software intended for everyone on the system to use. One would like to keep a degree of logical separation / easy navigation over their own files.

  1. Create a directory say «bin» under your home directory.
  2. Update your path variable to include this bin directory. Put this in .profile or .bash_profle file to make it permanent. export PATH=$PATH»:$HOME/bin»
  3. Create a script say, «hello» and keep it in your bin directory. Give execute permission to the hello script by $ chmod +x hello .
#!/bin/bash echo My first program

I know this is very old but I have seen your suggestion of creating a bin folder in a lot of answers here. Is there any problem if it’s .bin instead of bin ?. I am really picky about how my home looks and I dont want an extra folder just because.

I have tried using this unsuccessfully and was very frustrated for a while thinking there was something wrong with my zsh install or $PATH but it ended up being that the chmod -x hello wasn’t working, neither does a capital -X . Instead I tried chmod 755 hello , while I am not sure of the security risks in regards to this command it actually let me run hello . Does anyone have an explanation for this? I am assuming it is a problem in regards to age?

@ConstantFun Use chmod +x hello to add ‘run’ privileges to the script. chmod -x hello does the exact opposite — removes the ‘run’ privileges. (Notice the plus/minus sign difference in the answer and in your comment.)

Say you want to write a command to cd into your download directory. And you want to call it cdd.

You can create any command you want.

This doesn’t persist across sessions, though. This command works, but isn’t permanent. A more permanent way would be to modify your ~/.bash_aliases file and add the line you suggested there.

@JeffGrimes you can put it in the ~/.profile file (or .bashrc as Deilicia mentioned) and it will be permanent enough. very useful on hostings with limited file editing rights btw

I think add alias to your .bashrc file is the most elegant and simple way to custom your command. Thanks!

Most, if not all by now, Linux distributions have a little script in ~/.bashrc that looks almost identical to this:

if [ -e ~/.bash_aliases ] then . ~/.bash_aliases fi 

This merely means you can create your own commands (also known as ‘ aliases ‘ usually referred to an existing command with some arguments you always have to use, or a list of commands that have to be executed in order).

Читайте также:  Break command in linux

Your Linux distribution will most likely not have the .bash_aliases file created in your home, unless you’ve manually done that already. So to create the file, type in the following command:
touch ~/.bash_aliases

Now that file will be executed automatically every time you fire off a new Terminal.

What you can do now is create a list of aliases and add them to that file for later uses. For example, the rm (remove) command by default does NOT ask you to confirm your request when you tell it to delete a file/directory. However, there is an argument that tells rm to ask you to confirm your request, -i . So, rm -i filePath will display a message asking you if you were sure you want to delete the specified file. Now, if you were to accidentally delete a file, you will very likely forget to include the -i option, and that’s where an alias becomes very beneficial. Typing the following command

echo "alias rm='\rm -i'" >> ~/.bash_aliases 

will tell Bash that every time you request to delete a file, a confirming message will be displayed to you. Of course, there is a lot more you can do—this is just the basics.

If you want to learn how to use some basic commands (i.e. cd , touch , rm , mkdir , pushd , popd , etc.) and/or more sophisticated ones, I’d recommend a very good book you can have on your bookshelf as a reference called

Источник

How to Build Your Own Commands in Linux?

Linux is one of the most widely used open-source operating systems which supports both GUI as well as CLI. It has been widely used all over the world since its first distribution launched on September 17, 1991, by Linus Torvalds. It is widely known for its command-line operations. We have been using several commands in Linux like date command, mkdir command and many more. You can read more from Linux Commands.

date command in Linux

There are more than thousands of commands we use in Linux but we never thought of creating our commands. Building your commands can be a really fun and interesting way of learning and experimenting with your knowledge. While building new things you will come across many new concepts and you will get to learn many new things.

Here, we are going to build a new command for Linux distros which is not present in any of the distributions of Linux. We are going to build a real-time command for showing time at the terminal continuously without disturbing our terminal to perform other tasks. Let’s just have a look at it.

live date command in linux

To build something like this we need to have a good knowledge of Linux commands with their attributes and how to integrate Linux knowledge with scripting. To know all the attributes of command we can use, man command

man command in Linux

Moving further there is a command named echo which is used to display the output in the screen. For basic uses of echo command with examples click here. Apart from the basics, this command can also be used for displaying outputs of other commands. For example,

echo command in Linux

Here in this example, you can observe that when we run date command just within double quotes (“date”), echo took it as a string and printed it but when we run it within backquotes (“`date`”) it gave the output of date command. So we can also use echo for displaying the output of other commands.

We are already familiar with attributes of echo command like -n, -e, etc. If not, then please click on the above link and read the article for the detailed use of echo command. Now if we want just to display the time using date command we can use “date +%T”. For more details, you can use man command.

Читайте также:  How to add sudo user in linux

Now we know that in echo command for printing in next line we use \n, for backspace \b, etc. To work with these attributes we use -e and for printing output in same line we use -n. Suppose we need to print time in same line but we don’t want to show seconds, we can use echo -n -e “`date +%T` \b\b\b”.

echo with n and e option in Linux

As in this example, we can see that when we used only -n attribute the output was printed in the same line, and also when we used \b with -n it just printed it as a string. When we used only -e the output was on the next line. But when we used -n and -e together with \b, the seconds were not visible. In simple terms, the last three digits ie, “:12” were not printed.

Now we need to think, how we can use these skills in such a way that every time date should be printed with seconds also. If somehow we make it run continuously then every second we will get a new time and at the same time, it will be deleted due to backspace (\b) which we used. To make it run this command continuously we need to use scripting and now we are going to integrate our Linux knowledge with scripting.

For any command to run using scripting, a very basic syntax is followed. We just need to write the commands in a file and save it. To run all those commands present in that file, we need to just write “bash file_name” in which commands are present. For example,

In this example, we can see that on the left side we created a file named script in which we have given some of the commands. In the terminal, when we run “bash script” we can observe that all those commands which were written in the file are executed. Please note that in this example I have given file name as “script” but you can give it whatever you want.

Now if we want the date to be printed every second we need to run it under a loop. The syntax of loop in scripting is very simple. for example,

In the above example, you can see how simple it is to use while loop in bash scripting. You just need to add do before the command and as the command is over done keyword is written. As we have not given any limits, so it will run infinitely until all your resources are exhausted. You can see in the below image, after running the script it is continuously printing the statement which is written in f1.txt file. To stop it we use CTRL + C.

Now let’s see what happens if we use this concept to build our new command for time. If we put our command which we created in while loop, let’s see what happens,

Here we can observe that we can see the time at runtime with seconds changing every second, but the major issue is that we are not able to use our terminal for other tasks. It is just showing the time only and if we want to run other commands we need to stop this script.

Читайте также:  Sony bravia linux приложения

To solve this issue, we have to put the command in the background so that the script will run in the background and we can use the terminal for other commands. To put any task at the background we use “&” at the end of the commands. For example,

In the above picture when we run “gedit abhi” you can observe that it is using our terminal and we cannot run any other command in our terminal until we terminate it. As we use & after the command you can observe in the below picture that gedit is running in the background and our terminal is free. We are free to use our terminal for other commands also and at the same time gedit is also running.

Now, this was the concept of “&” for running anything in the background. Now we need to implement it to our new command so that we can make it quite more efficient. We are aware of using loops in scripting as I have discussed above. Now we just need to append “&” after “done” keyword to make it run in the background. Now if we add it, let’s see how it works,

Now you can see, it is working perfectly and we can use our terminal as well. In the below picture you can see that our live-time is also running and we can run other commands as well. Here I have used “bash livedate” command but you can save your file with any name like live-time, or anything you wish to. I am using livedate because I have used date command to build this live real-time command.

Источник

How to create my own command in linux

I wrote a simple cpp code by using vi. I want to use cat command inside this program (by using execl and it will take my file as argument). I also have to write my own command. For example it will work like that: $mycommand filename. And cat command which is inside my cpp code will show my cpp file’s content. I’m new so I don’t know how to do it. Do you have any suggestions?

1 Answer 1

1) Redirecting output
The output of the cat command should go to the standard output of your program. For this your program should pass its standard output FD to the exec command. This will be the default case. So you don’t have to do anything extra.

2) cat on the source file
Now, the cat thing : «And cat command which is inside my cpp code will show my cpp file’s content. » This statement does not explain your need. If you want the cat of the executable you are running, you can do an objdump on your executable. The executable path can be found out from the proc file system with your pid. For a generic case, it does not matter whether the file is a cpp file or not.

3) making command
To make your executable run as a standard command on a terminal, you must add the directory containing the executable to the PATH directories list. You can do so by adding your executable to /usr/local/bin or /usr/bin. (Convention is that it should go in /usr/local/bin since it has been compiled locally). If you don’t have administrator privileges on the machine, you must append the source directory to the PATH directory list.

Источник

Оцените статью
Adblock
detector