Trying to run file in Mac Terminal - terminal

I type the name of the file into the Terminal as just "set.bat" but get:.
-bash: run: command not found
I also fail to understand how this question fails to meet stack overflow's quality standards without this extra paragraph, so if someone could enlighten me about this also I would be much obliged.

First you have to make sure that you are in the same folder of the file to run, then you issue:
$ ./filetoexecute
mind the ./, you'll have to write it because current directory is (almost never) in the path.
Additionally, .bat are Windows batch scripts, Bash will not understand them. You will have to look for a .sh (usual extension) or extensionless script.

Related

Terminal says there is no file

enter image description here
PLEASE HELP! I can't seem to run my file. I've compiled it so it should run shouldn't it?
It also happens to my other files and I can't seem to fix it.
Ok, so a quick point of notice:
It is usually appreciated if you post your code directly in your question and use the code markup for that. It makes it easier to answer your question and prevents the image from turning into a dead-link at one point in time.
Then, regarding your code: it seems your Topic3ex1.c still has some errors, it always helps to fix them first. Maybe the errors are preventing Topic3ex1 (which I believe you have selected as the output file with the -o option? I am not familiar with gcc's syntax.) from being formed. Check if the file exist first by executing ls -a from the command line.
If you get a message saying "No such file or directory" while it is indeed there, this is usually caused by a lack of user rights. Try executing chmod u+x Topic3ex1.c and then run your command again. This will give your current user the right to execute the file (read up on chmod if you do not know it already, you will need it often).
Final question: is Topic3ex1 supposed to be a file or a folder? For if it is a file, then executing it like ./Topic3ex1 will only work if it is a shellscript (in that case, best rename your file to Topic3ex1.sh, its best to always mention the extension of the file). If it is a folder the ./ command won't do anything and the cd Topic3ex1 (cq. change directory-)command you where experiencing with will activate it as your working directory. If it is a file however, then the change directory command will, of course, be useless.

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?
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!!

bash. Usage of: './'

I did try to find the answer online but unfortunately came up empty handed. Searching for './' in combination with other keywords brings up many hits, but none that helped...
Anyhow, as you can probably tell, I am rather new to MacOs. I am running mongo from the shell.
I cd into the /bin folder and start the mongo daemon with:
'./mongod'
.
If I just enter 'mongod', I get the following error:
'-bash: mongod: command not found'
What does the
'./'
in './mongod' stand for? Why is it needed? Why can't I just execute mongo by typing mongod. After all, I am in the correct directory.
After all, I am in the correct directory
But that directory isn't in the PATH. The gist of the matter is that when the shell wants to execute something it looks into a list of directories (specified in a variable called PATH). And the current directory isn't in that list.
As explained in the answer by cnicutar the shell tries to match the command you enter with executables in the directories that are listed in the PATH environment variable.
Although you could add the current directory to the PATH, it's probably not a good idea to do it. This article gives some good reasons why Linux doesn't add it by default:
Its a measure to plug possible security holes. You can't run commands
in the current directory without specifically calling them.
ie ./myscript.sh
To stop nasties writing a vicious shell script, placing the script in
an innocent location such as /tmp and getting root to run the script.
Would root run a nefarious script? If the script was named 'ls' and
root did a listing, the script would automatically run.

Add directory to system path in OS X Lion

I can't figure out how to add a directory to the system path. I found out that the command is something like this:
export PATH=$PATH:/my_path/to/my_dir
Example
I run the terminal in the path where my installation directory is located. In this case I'm talking about Play Framework. And I type:
export PATH=$PATH:/to/play20
It looks like nothing happens. In fact, when I type the command "play" (to execute the framework) I get:
-bash: play: command not found
Can someone please give me a decent step-by-step guide?
Execute the command “/to/play20/play help”. If this gives the expected output (help for the play command), then the executable is available, and the problem is in the shell path. If it does not give the expected output, then the executable is not working.
In the former case, ensure you are running the bash shell. (This is the default for recent versions of Mac OS X, but it may be changed for specific accounts.) To do this temporarily, execute the command “bash”. (When you want to exit the temporary shell, execute the command “exit”.) Then execute the export command again. (When the export command works, it changes the variable without displaying any output, so this is normal.) Check the spelling in the export command carefully.
In the latter case, execute “ls -ld /to/play20/play”. If you get a message that the file does not exist, then the executable is not installed correctly, and I cannot help you further. If the file is present, then it is not executable for some reason. This might be because you do not have permission to access it (especially permission to execute it, marked by “x” in certain places in the initial field of the ls output that may contain hyphens, “r”, “w”, and a few other letters) or that it is a symbolic link to a file that does not exist (indicated by an “l” in the first character of the ls output for the file). Lack of permission can be fixed by the chmod command, assuming you have appropriate permissions for changing permissions on the system. If the file is a symbolic link to another file, you may have a bad installation, or the target file may have permission issues (or be another symbolic link, and so on).

How can I gain permission to rename a file for my Ruby program?

As per the answer to this question, I am trying to backup a file by renaming it, before I replace it with a new, modified file with the old name.
As per the comments and the documentation here, I am using the following line of code:
File.rename(File.basename(modRaw), File.basename(modRaw)+'.bak')
However, when I do so, I get the following error at runtime:
The program then aborts. (leatherReplacer.rb is the name of my program, and line 88 is the above line of code)
How do I allow my program to rename the files it needs to to run successfully?
Windows has some special rules regarding permissions. The important one at work here, is that the OS prevents moving or renaming a file while the file is open.
Depending on the nature of your code (in size and scope) and the importance of the file you're trying to back up, it may be unfeasible or otherwise not worthwhile to refactor the code in such a way as to make backups possible.
You probably don't want to be calling File.basename in there, that strips off the directory:
Returns the last component of the filename given in *file_name*, which must be formed using forward slashes ("/") regardless of the separator used on the local file system.
So, if modRaw is /where/is/pancakes.house, then you're saying:
File.rename('pancakes.house', 'pancakes.house.bak')
But pancakes.house probably isn't in the script's current directory. Try without the File.basename calls:
File.rename(modRaw, modRaw + '.bak')
If you are owner of that file, use File.chmod to set desired permissions.
I don't know much about ruby, but could you run it under command line/bash with admin privileges, such as "run as administrator" or "su root"?
According to Objectmix and ruby-forum, you should set it to 755 or +x, then perhaps chown to yourself.
try using full file path e.t
File.rename('c:\pancakes.house', 'c:\pancakes.house.bak')
in win7 i encounter same problem

Resources