How to set a program to run in Linux terminal only with program name - linux

I'm new to Linux and I wonder there are many programs we can use only program name to start it in Linux terminal, like gedit,vi,firefox instead of providing the all program's path,I like to run my own programs like this in terminal only typing program name, programs I like to run are written in Java and Python (.jar, .pyc, .py and .class)
I like to know how to do it with step by step

You can write whatever program/script you have to behave as a command. Let's say your executable script/program is named as my_script and is placed in /path/to/my_script.
Be sure that the script is executable. If not,then please do
chmod +x /path/to/my_script
Then, place a symlink to this location in /usr/local/bin as
sudo ln -s /path/to/my_script /usr/local/bin
You can add the symlink to any of the paths mentioned in $PATH.
That's it and enjoy your program.

The other answers all involve creating a symlink in a directory that is already listed in the system PATH, but I think it is more unixy to add needed directories to your PATH.
If your script is located at $HOME/bin/myscript and you have already made sure that it is executable then you can run
export PATH=$HOME/bin:$PATH
to run it without giving the full path. And you can add that same line to your .bashrc file in your home directory to have it preloaded whenever you start your shell. This approach does not require that the user has permission to create symlinks in system directories.

If you have an executable binary file in your home folder (let's say for example sublime_text) you must give it execute permision and call it with its relative path
chmod +x sublime_text
./sublime_text
If you made a symlink to it in /usr/bin (or other folders included in your PATH), you would be able to call it by its name
sudo ln -s ~/sublime_text /usr/bin/sublime_text
sublime_text
In your case, you aren't dealing with binary files, but with scripts meant to be interpreted. For this you must prepend a shebang telling linux what's the binary meant to execute the script. If it was, for example, a python script ~/hello.py, these could be the contents of the script:
#!/usr/bin/python
print "Hello, World!"
Where the first line tells linux to use the python binary to execute the script.
From then on, you can do:
chmod +x hello.py
sudo ln -s ~/hello.py /usr/bin/hello
hello
And it will echo "Hello World" to the console.

Related

Cannot run .sh script under sudo in linux

I have a script foo.sh located in /home/pi/Documents/Python directory. Purpose of this shell script is to run python script which needs root priviledges as it must reset usb device.
The script is as follows:
#!/bin/sh
export PATH="$PATH:/home/pi/.local/lib/python3.7"
python3 /home/pi/Documents/Python/foo.py
When I run the foo.py from Midnight Commander (setting a cursor on the file and pressing enter) it works, it exports the path correctly and the python script fails as it does not have enough priviledges to reset usb device.
I have actually made this script to run python script under root, but the root needs set a path to used module first.
However when I run
sudo foo.sh
I receive an answer:
sudo: foo.sh: command not found
I have checked the permissions and the foo.sh file has -rwxr-xr-x
sudo python3
typed in terminal also works correctly and opens python interpreter.
What is the problem that causes wrong behaviour under sudo?
I might be mistaken (I don't have a Linux Machine at hand atm, so I cannot verify), but if I recall correctly the user_home is part of the PATH variable exported for that user.
When you use the command sudo you are acting on the behalf of root which has got a different user_home than yours (== the current user), therefore your script is not found in any of the directories listed in the active PATH (the one of root because you are using the sudo command).
However, it should be possible to run successfully the following command:
$ sudo ./foo.sh
I hope this might shed some light.
Unless foo.sh is in a directory shown referenced by the PATH environmental variable, the environment will not recognise the command and hence the error
If you are in the directory with the foo.sh script, execute it with:
sudo ./foo.sh
If you are in a different directory, execute with:
sudo /pathtosh/foo.sh

How do I bind a terminal command to the execution of a shell script?

Say I have a programme that's run from a shell script:
cd /path/to/file/
./programme
How would I bind that to a single command so I only have to type one thing? ie for most installed programmes I can just type the name of the programme and it's running
programme
you must add your script path in $PATH variable in ~/.bashrc file ,like this:
export PATH=$PATH:/path/to/file
or put your script in linux binary directory like /usr/local/bin , /usr/bin
For this you need to create a script with a name and give execute permission to it. Then Copy that script to /usr/bin directory . Now you can run your scrpt as a command in terminal. For details please refer the link https://devopsmanual.in/2018/04/17/create-our-own-script-in-linux/

How to execute a file without .sh extension in shell

I want to execute a file in bash without the .sh extension.
Example: I have file "abc.sh" which I can execute directly (as I have added #!/bin/bash as the first line) but I want the filename to be just "abc"
If the file is already executable as abc.sh, then all you need to do is
mv abc.sh abc
(assuming you are in the directory where the file lives)
In a Linux or Unix shell, file extension doesn't affect whether it will execute or not.
In Linux you use ./filename too run a script. And you need execute permission:
chmod 755 filename
But you still need the "Shebang":
#!/bin/bash
From here I got this:
If you did not put the scripts directory in your PATH, and . (the
current directory) is not in the PATH either, you can activate the
script like this:
./script_name.sh
A script can also explicitly be executed by a given shell, but
generally we only do this if we want to obtain special behavior, such
as checking if the script works with another shell or printing traces
for debugging:
rbash script_name.sh
sh script_name.sh
bash -x script_name.sh
What are the permissions on the file? To make it executable with doing something like ./abc.sh it needs to have EXECUTABLE rights.
You can always do bash abc.sh
Linux permissions overview
Filename in Linux doesn't mean anything in terms of execution capabilities, you can call the file myfile.something.something and it can still be executable. You can name it abc but it has to have EXECUTABLE rights for the user,group,other.
To add that permission you can do chmod +x <filename> but you should look at the link above for a better understanding.

Why i can excute a newly created file which is not executable?

In Ubuntu, The default umask on Ubuntu is 022 which means that newly created files are readable by everyone, but only writable by the owner, nobody can excute it.
In this case, i create a new file :
touch test.rb # Its content is: puts "hello world"
ls -l demo.rb # -rw-r--r--
Then i excute test.rb :
ruby test.rb # output: "hello world"
Since the owner of the file does not have the "x" permission , then why I can successfully run the file ? or I have missed some knowledge about it ?
You are not executing the file as a binary. You are executing ruby binary with argument test.rb and it interprets the Ruby script. Therefore, only ruby binary needs execution privilage and not the script itself.
You can check the privileges of the binary by running stat (which ruby).
On the other hand if you place
#!/usr/bin/ruby
on the top of your script and make it executable with chmod a+x test.rb you could then make Linux run it. The binfmt module of the kernel will check search for #! (called shebang) in the file and run the interpreter for you.
You can find this shebang in lot of the shell scripts. Nowadays it is common to put #!/usr/bin/env ruby or #!/usr/bin/env python in order to use interpreter binary in other location that is available on PATH variable like /usr/local/bin/ruby. Again env is just another binary program. It will run its argument as a program. The kernel will pass script as the parameter which will result in command /usr/bin/env ruby test.rb.
Grzegorz Żur is right.
you can modify your test.rb like this:
#!/usr/bin/env ruby
puts 'hello world'
and then you excute it with .:
$ ./test.rb
you will see Permission denied.

I'm learning about shebangs. How do I make it work with node.js in a Mac terminal?

I have:
#!/usr/bin/env node
console.log("It works!");
I learned that env finds the node program and interprets it with node. I checked that env exists in /usr/bin.
When I call node itworks.js it works and outputs It works!. However, from what I understand, I should just be able to call itworks.js without node due to the shebang. But when I make this command it says -bash: itworks.js: command not found.
Could someone help me get the shebang to work?
First of all you need to make the file executable:
chmod +x itworks.js
Then you need to call it by specifying the path as well. Either:
/where/it/is/on/disk/itworks.js
or:
./itworks.js
The reason for :
-bash: itworks.js: command not found
is because bash looks for programs in directories in the PATH environment variable when you do not say where the file is - it does not look in the current directory unless you tell it.
You could update the PATH variable with the current directory shortcut ., but that can be a security risk, so most run the program like this:
./itworks.js
Of course if you put your scripts all in one directory then you could add that to PATH in one of your start-up files. For example, if you had a directory called bin in your home directory that held your scripts:
PATH=$PATH:"$HOME/bin"
You also need to add the execute permissions to the script:
chmod u+x itworks.js
The u indicates that we only give permission for the current user to execute this file. If we omit the u then anyone can run it.

Resources