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

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?

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.

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.

Type bash script_name.sh or ./script_name in linux terminal. Before using ./script_name make you script executeable by sudo chmod 700 script_name and type script_name.bat in windows.

Drag-And-Drop
Easiest way for a lazy Mac user like me: Drag-and-drop the startup.sh file from the Finder to the Terminal window and press Return.
To shutdown Tomcat, do the same with shutdown.sh.
You can delete all the .bat files as they are only for a Windows PC, of no use on a Mac to other Unix computer. I delete them as it makes it easier to read that folder's listing.
File Permissions
I find that a fresh Tomcat download will not run on my Mac because of file permission restrictions throwing errors during startup. I use the BatChmod app which wraps a GUI around the equivelant Unix commands to reset file permissions.
Port-Forwarding
Unix systems protect access to ports numbered under 1024. So if you want to use port 80 with Tomcat you will need to learn how to do "port-forwarding" to forward incoming requests to port 8080 where Tomcat listens by default. To do port-forwarding, you issue commands to the packet-filtering (firewall) app built into Mac OS X (and BSD). In the old days we used ipfw. In Mac OS X 10.7 (Lion) and later Apple is moving to a newer tool, pf.

Based on IsmailS' comment the command which worked for me on OSX was:
sudo sh ./startup.sh

On windows type either startup or startup.bat
On unix type ./startup.sh
(assuming you are located in tomcat/bin directory)

Batch files can be run on Linux. This article explains how (http://www.linux.org/threads/running-windows-batch-files-on-linux.7610/).

Type in
chmod 755 scriptname.sh
In other words, give yourself permission to run the file. I'm guessing you only have r/w permission on it.

add #!bin/bash on top of the your .sh file
sudo chmod +x your .sh file
./your.sh file
these steps work~

My suggestion does not come from Terminal; however, this is a much easier way.
For .bat files, you can run them through Wine. Use this video to help you install it: https://www.youtube.com/watch?v=DkS8i_blVCA. This video will explain how to install, setup and use Wine. It is as simple as opening the .bat file in Wine itself, and it will run just as it would on Windows.
Through this, you can also run .exe files, as well .sh files.
This is much simpler than trying to work out all kinds of terminal code.

I had this problem for *.sh files in Yosemite and couldn't figure out what the correct path is for a folder on my Desktop...after some gnashing of teeth, dragged the file itself into the Terminal window; hey presto!!

Related

Bash script can't find file in /opt and gives strange error message

I am trying to use a simple bash script that uses a script in /opt
#!/bin/bash
pvpython=/opt/paraviewopenfoam54/bin/pvbatch
script_path=save_contours.py
$pvpython $script_path
The file pvbatch does exist, however when I try to run the script I get this strange error message which feels like it is missing some characters:
: No such file or directoryaviewopenfoam54/bin/pvbatch
The Ubuntu I am using is Ubuntu 18.04.1 LTS inside a Windows subsystem for Linux. What could cause this error message?
When I run ls -al pvbatch in /opt/paraviewopenfoam54/bin I get
-rwxr-xr-x 1 root root 84200 May 29 2018 pvbatch
cat pvbatch returns an error message
cat: write error: Input/output error
So cat cant read the file which is strange!
My first thought is that there is an issue with the file itself. where was the file created? I see that it is under /opt/paraviewopenfoam54/bin which is a linux managed folder, did you create and edit the file using vim in WSL or did you create it in windows somehow with VS Code or the like?
Let me just check that you are not creating and editing files in windows directly in the /opt folder inside the %LOCALAPPDATA% folders. If the file was created in this method you risk corrupting the WSL installation. Dont Edit WSL files
When using WSL I commonly find issues and especially this Input/output error issue whenever I create a file directly from windows into the Linux filesystem. There are essentially three main ways to fix this specific issue: (these are not steps but rather separate solutions)
-Restart your Ubuntu terminal
-Restart your entire computer
-Delete the file directly from Windows, touch the file, then overwrite the file with the old one
For a more scalable solution, I recommend leaving the file within the windows system and only working within the Linux system within Linux.

How to send a file to Desktop with bash script?

I am trying to send a file or folder to Desktop with Linux Command Prompt but I don't know how.
Please tell me what command can I use for this?
The move command mv. Use man mv for more information, as this command is a lot more complex than it seems. With cd Desktop/ you should be able to find your desktop on variations of linux like Mint or Ubuntu. To find your present working directory, as in your current path for the terminal, type pwd. This will give you your directory which will be similar to /home/Desktop.

What is the code to start and close tomcat 7 on terminal mac?

I am very new to using the terminal and using tomcat.
I had already installed tomcat yesterday and it was working, however today when I turn on my computer again, it is not working, I am assuming I need to start it up again , but I don't know how.
Can somebody explain to me or write the code to start it up please?
Thanks in advance!
This works!
Make all Tomcat's bin shell scripts executable for files under apache-tomcat-7.0.30/bin
The code is:
chmod a+x *.sh
After that Run from tomcat/bin with the script:
./startup.sh
Here's the instructions for the catalina.sh shell script that controls Tomcat.
The Tomcat control scripts resides under the bin directory inside the
Tomcat home (Where you have Tomcat installed). For the Unix or Unix
flavored operating systems the scripts with extention ".sh" needs to
be executed
catalina.sh provides a lot of options/controls, but startup.sh can be used to simply start it.
Under some circumstances the startup scripts do not execute because the execute permission has not been set. If this is the case you can change the execute permission to the scripts by typing the following:
cd ../bin
chmod 750 *.sh
This signifies read, write, and execute permissions for the owner, read and execute permissions for the group, and no permissions for others.

Command not found

I am quite new to the Mac Terminal environment.
I donwloaded sqlplus (which is recognised as a UNIX executable program) and then in Terminal I do cd a few times until I arrive in the folder I put this in (\Applications\instantclient_10) in this case.
When I type 'ls' I see a listing of all the files including the sqlplus. So I would then expect to simply type at the Telnet prompt 'sqlplus' and then this would start but instead keep getting
-bash: sqlplus: command not found
This is problem one.
I have now downloaded MySQL and again, when I go to the correct folder (\Library\StarupItems\MySQLCOM) and I type 'ls' I see my files (including MySQLCOM) but when I come to try to 'run' this by simply typing 'MySQLCOM' again the message is:
-base: MySQLCOM: command not found
What am I doing wrong?
Thanks
To run binary/executables in current folder you need to prefix them with ./
./sqlplus
The idea here is that you want to force execution of local file and not run it accidentally. Imagine app that would put ls binary into it's folder and it would automatically run if did ls in that folder.

Can i make a shell script executable in Mac without chmod stuff?

I have seen that i can use chmod +x command in Mac to make a shell script executable. This works fine, but i have noticed that i have to do the same thing every time this shell script file is copied to another Mac computer.
Is there a way to make the shell script executable by default when double clicked, without such command ... As the shell script file will be given to many users, and doing this will be hard for some of them ?
Best regards.
If you pack your whole program in a .tar file (or in a .tar.gz-file, which is the same, but compressed), the executable-"permission" will be preserved.
Give it the '.command' extension and it can be executed from the Finder.

Resources