I have a .sh file and I want to execute it from shell without writing the extension.
What I did:
I created a directory and added it to $PATH
I gave to the file.sh chmod 711
and the file contain #!/bin/sh (I tried also bash).
However when I try to execute myscript without sh I get command not found
while if I try with myscript.sh I get the right result.
How could I do?
I read also: How to run a shell script on a Unix console or Mac terminal? and executing shell script without calling sh implicitly but no solution
Result of ls -l
ls -l /Users/Mitro/scripts
total 8
-rwx--x--x 1 Mitro staff 22 Nov 26 10:25 myscript.sh
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Users/Mitro/scripts
Two problems...
Firstly, file is already an executable program in /usr/bin that tells you the type of a file - i.e. whether it is an image, or a song or a database. Quick example:
file a.png
a.png: PNG image data, 1 x 1, 1-bit colormap, non-interlaced
So, file is a bad name for a shell script - likewise is test.
Secondly, if you want to execute a script or program in your current directory, also known as dot (.), you either need to have dot in your PATH, or you need to explicitly tell your shell that the file you want to run is in the current directory. The easier option is the second, which means if your script is called fred, you run it with
./fred
which tells the shell it is in your current directory.
The longer option, if you want to always be able to run scripts in the current directory, is to add dot to your PATH. So, you locate your login script (probably $HOME/.profile) and you find the line that sets your PATH and you add the current directory to it.
export PATH=$PATH:.
Once you have set that, you are best off logging out and back in to have it take effect.
Some folks disapprove of the idea of adding dot to their PATH - I don't. YMMV.
You can add alias. If you have /some/path/to/script.py, do:
alias my_script='/some/path/to/my_script.py'
Now when you enter my_script, your script would be executed.
For mac the profile file is ~/.bash_profile as opposed to ~/.profile which did not work.
Related
I have the file script.sh in my-directory folder.
How to run this script with the command `script' from the terminal with no regards to the location I am in the terminal?
You can do so by exporting the path where your script in the PATH environment variable, so that you don't ever have to worry about what your actual script's location is, i.e. if your script is present under say /path/to/dir, do
export PATH=$PATH:/path/to/dir
so that your script's path gets appended to an already existing set if paths under PATH, also remember if you run the above from the command-line, it is not permanent and gets lost soon after the session is terminated. To make it permanent add the same line in .bashrc (or) .bash_profile, depending upon your environment.
Or creating a symbolic link from /usr/bin that is what you intent to do you can do something like ln -s /full/path/to/myscript.sh /usr/bin/myscript and then run as just myscript directly from command line. You can also confirm if is properly added by checking the script's location by which command,
$ which myscript
/usr/bin/myscript
Say your directory is /home/Cristian/my-directory then you can make that part of PATH environment variable like export PATH=$PATH:/home/Cristian/my-directory and then you will be able to call it by typing script.sh and not script. If you want it to be called as script then you should name it script and rename the extension.
The export command will make the directory in question part of PATH temporarily. To make it permanent you may want it to part of .bashrc or other shell rc file if you are in other shells.
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.
Getting "command not found" when trying to run shell script file. The file itself is stored in a directory, that is added to PATH, but still, terminal doesn't recognize it as shell script.
Here's the sequence I try:
tajimura/GAMIT% echo $PATH
/usr/local/bin:/usr/bin:/bin:/user/games:/usr/X11R6/bin:/usr/bin/X11:/usr/lib64/jvm/jre/bin:/home/tajimura/GAMIT/gamit/bin:/home/jaimura/GAMIT/kv/bin:/home/tajimura/GAMIT/com
tajirmura/GAMIT% ls /home/tajimura/GAMIT/com/sh_steup
/home/tajimura/GAMIT/com/sh_setup
tajimura/GAMIT% sh_setup
sh_setup: Command not found.
tajimura/GAMIT% sh sh_setup
sh: sh_setup: No such file or directory
tajimura/GAMIT% l /home/tajimura/GAMIT/com/sh_setup
-rwxr-xr-x 1 tajimura users 11109 Aug 20 2013 /home/tajimura/GAMIT/com/sh_setup
Here is a screenshot:
PS: Opensuse 12.1 here.
ADDED: I was executing it just fine during first 4 days, so I guess hashbang is not an issue. But I can't guarantee that workstation wasn't rebooted between my sessions, so maybe (just may be) -noexec is the cause. However, script sits in my home directory on hard disk, it's not a removable drive.
ADDED: The first five lines of sh_setup:
/home/tajimura% sed 5q /home/tajimura/GAMIT/com/sh_setup
#!/bin/csh -f
#
#doc Check and setup the GAMIT tables directory
#doc
#
Your script is either not executable (make it so with chmod +x sh_setup) or it specifies a broken (non-existing) interpreter on its hash-bang line (the first line of the sh_setup file, starting with #!).
Your sh sh_setup invocation fails because it doesn't use $PATH and you're in the wrong directory.
EDIT: Your script is clearly executable (I didn't spot this in your screenshot at first), which leaves us with a possibly incorrect interpreter.
I've found an unexpected solution of the problem. If I call the script under bash instead of csh everything is good.
I am creating a terminal program and cannot find out what the ending is for Linux. I know in windows it is .cmd. Any help would be great.
Thank you.
Yes, you can remove the .sh at the end and it should work, generally using ./cmd will get it to run. this goes for C programs as well. You do not need to give an extension for the object file, You could then add a path to your bash file and then you can execute it as a normal command.
Look here.
https://stackoverflow.com/a/8779980/2720497
You don't need a file extension on Linux, though typically, people use .sh (sh being short for 'shell').
You can run it one of two ways:
bash myscript.sh
or you can make the script itself executable and run it directly:
chmod a+x myscript.sh # make it executable
./myscript.sh # run it
Linux scripts' first line is typically #!/bin/bash which is the path to the specific shell used to run the script with the second method.
I have created a simple script:
echo "the path of the current directory is `pwd`"
and saved it by the name pathinfo
then i have created a bin directory at my home page with path as
/home/vpnsadmin/bin
and copied my script(pathinfo) to that bin directory.
Now i want run this script as a command but it is showing error
-bash: /usr/bin/test2: No such file or directory
but if copy my script(pathinfo) to "/usr/bin/" then it runs as a command.
the PATH environment variable is set as-
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/home/vpnsadmin/bin
My question is why does the shell not run it as a command when it is present in /home/vpnsadmin/bin.
or else
why does it only check for the binary at /usr/bin and not at /home/vpnsadmin/bin or at /bin
The shell that is to execute your command needs to have the correct PATH variable set at the time of execution and, depending on shell, might need to have created its own internal (hash)map of the available commands.
Assuming you are using bash, try the following with your script saved in /usr/bin:
$ PATH=/ test2
$ PATH=/usr/bin test2
In the first case you should get an expected "not found" error, in the second it should work. The third test to perform is left as an exercise...
And I have to say that the supplied error message looks a bit odd if you actually tried to do
$ test2
and not
$ /usr/bin/test2
before copying the command to /usr/bin.
Edit:
Also, avoid naming your scripts test, in any way shape or form. This causes so much confusion for beginners.
Hint:
man test
Did you have the path to bash at the top of your script and did you use backticks around pwd?
#!/bin/bash
echo "the path of the current directory is `pwd`"
Did you make the file executable?
chmod +x pathinfo
There is another script pathinfo somewhere in your path which contains a call to /usr/bin/test2
Try whereis pathinfo to see how many there are and which pathinfo to see which one your shell currently prefers.