Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run .sh or .bat files from Terminal?

I have a pretty basic problem here, that has happened so haphazardly to me that up until now, I've just ignored it. I downloaded tomcat web server and "Murach's Java Servlets and JSP" book is telling me to navigate to the tomcat/bin directory and start the server my typing in Terminal

$ startup

However, I get the error

-bash: startup: command not found 

The relevant files in this directory are startup.sh and startup.bat. Typing both of these returns the same error message

So my questions are, what are .bat and sh files, and how do I run these files? I've read several tutorials for different languages and software programs, and some times when the tutorial says execute a bunch of files in the command line, I get a "command not found" error. Sometimes it works, sometimes it doesn't. This is perplexing to me, so what are some common solutions to solving these sort of "command not found" Terminal problems?

like image 456
almel Avatar asked Jun 10 '13 00:06

almel


People also ask

How do I run a .bat file in Linux?

Batch files can be run by typing "start FILENAME. bat". Alternately, type "wine cmd" to run the Windows-Console in the Linux terminal. When in the native Linux shell, the batch files can be executed by typing "wine cmd.exe /c FILENAME.

Is .bat and .sh file same?

bat file is a windows batch file it contains a sequence of windows/dos commands. These cannot be used in a Unix shell prompt. A . sh file is a unix shell script it contains a series of unix commands.


2 Answers

The .sh is for *nix systems and .bat should be for Windows. Since your example shows a bash error and you mention Terminal, I'm assuming it's OS X you're using.

In this case you should go to the folder and type:

./startup.sh 

./ just means that you should call the script located in the current directory. (Alternatively, just type the full path of the startup.sh). If it doesn't work then, check if startup.sh has execute permissions.

like image 177
pilsetnieks Avatar answered Oct 17 '22 07:10

pilsetnieks


This is because the script is not in your $PATH. Use

./scriptname 

You can also copy this to one of the folders in your $PATH or alter the $PATH variable so you can always use just the script name. Take care, however, there is a reason why your current folder is not in $PATH. It might be a security risk.

If you still have problems executing the script, you might want to check its permissions - you must have execute permissions to execute it, obviously. Use

chmod u+x scriptname 

A .sh file is a Unix shell script. A .bat file is a Windows batch file.

like image 43
KamikazeCZ Avatar answered Oct 17 '22 08:10

KamikazeCZ