executable file doesn't print anything - bash

I've written a simple script, that basically looks like this:
#!/bin/bash
echo Hello World
I'm trying to run this in my unix terminal but it basically does nothing. no errors, no printing, nothing
[solgag#t2 ~]$ olga
[solgag#t2 ~]$
any ideas?

Try ./test instead. If you run just test, bash will look for an executable named test in $PATH and it will find it (or maybe execute its own built-in?) as test is a standard command in UNIX.

if you shell script name is olga you need to run in terminal as
$./olga
To run the script as specified above you need to have executable permissions you can add executable permission using chmod command
$chmod u+x ./olga
You can also run a bash script using sh command
$sh olga

Related

Simple bash script prints command not found

I'm working on an Ubuntu 18.04 and I'm trying to run a very simple bash script named print_test:
#!/bin/bash
echo 123
I have the $PATH variable set correctly (it includes the /bin directory) and I have tested that
I'm using the correct line ending (wc -l print_test results in 2). I have changed the permissions using
chmod +x print_test. When I try to run the script I get:
print_test: command not found
Any idea what I might be doing wrong?
Note: when I simply run echo 123 in the terminal it does prints 123 .
Thanks!
If the path is set properly it must work
Easier way , to invoke a shell script
chmod +x script.sh
./script.sh
Here replace script with the name of your script
Use this at the first line of your script
#!<path/to/bash>

Running gmt.script

I try to run a gmt-script and get the message:
bash-3.2$ plot_scenario.gmt
bash: plot_scenario.gmt: command not found
Does anyone know what could fix the problem?
I got a script from my supervisor, and it worked just fine on the uni Linux pc.
I have a Mac OS.
When you type text in shell, it tries to look for available commands. Here bash doesn't find any command like plot_scenario.gmt and outputs command not found.
The proper way to execute a file is ./filename where . refers to current directory. But you need to give execution permission to file you want to run. So, following commands may help you:
chmod +x <filename>
./<filename>
Note that make sure your pwd is where you're file is located. Another way to run or execute file is calling the shell:
bash /path/to/file

Converting a shell script to a dmg

I have a shell script with some functionalities. I want to convert it to an executable file. Any idea if this is possible?
Thanks
Add the following line at the very top of your script:
#!/bin/sh
This is known as a shebang. It indicates which program to invoke the shell script with when you execute it. You could change it to anything. Eg, to run a zsh script you would use #!/bin/zsh, which is the path to the zsh interpreter on my machine.
Then you need to mark the file as executable using chmod. You can do this as follows:
chmod +x myscript
You can now run the script like this:
/full/path/to/myscript
Or, if you're in the directory the script is in:
./myscript
The '.' expands to the path of your current working directory.
If you want to be able to run the script from anywhere, stick it somewhere in your path. Eg.
mv myscript /usr/bin
You can now run the script from anywhere by typing in just the name.

Why does this sh shebang not work?

In the following script (saved as script.sh):
#!/bin/sh
cd $MY_PYTHON_WORKING_DIRECTORY
python script1.py
python script2.py
Then, when I try to run the command script.sh in my bash shell, I got the error bash: script.sh: command not found. Why does this not work as expected? If the first line of any scripts start by #! prefix, then the following path on the line is interpreted as a command, right? For your information, even if I changed my first line to #!/bin/bash, the same error still occurred. If I run the script as either sh script.sh or bash script.sh, then the script ran as expected.
Is there any way to run the script by just hitting script.sh?
One more question, between sh and bash, which should I use? I'm on OS X 10.8 and my default shell is currently set bash, but I wonder which one to use going forward.
Thanks.
First, make the script executable:
chmod u+x script.sh
Second, your current directory is not in your $PATH. Therefore, you have to run the script with a path (relative is enough):
./script.sh

Cygwin Shell Scripts

I'm not running cygwin, but I have the cygwin ash.exe in my %PATH% as sh.exe and have cygwin1.dll in %PATH%
I am trying to invoke some shell scripts (named with no extension) using sh -c shell-script-name but I get a "permission denied" error. If I run sh and run ./script I also get this error. I have a proper #!/bin/sh shebang line and even renaming to .sh or .exe has no effect. What should I do?
One thing to try to see if Windows permissions are causing a problem is to run Process Monitor and filter it for sh.exe and shell-script-name. That will probably show you if there's particular permission you don't have (eg you might have read but not execute permission).
Try also running the shell interactively, ie:
c:\>sh
sh# . ./script or
sh# sh -c ./script
If this works then you know that the cygwin part is working correctly. Another thing to check is that the line endings for your script are unix, as that can stop scripts from executing correctly.
Everything worked for me after doing:
$ chmod +x script

Resources