Make cannot be called from script without sourcing the script - bash

I want to call the make command from a bash script in a MinGW bash shell. However the script seems to know "make" only when I call the script using source:
build.sh:
#!/bin/bash
make all
Calling
source build.sh
from the terminal works: The target all is built.
Calling only
build.sh
from the terminal results in
./build.sh: line 2: make: command not found
Why do I have to source the script to have a working make command?

Related

Having issues executing a makefile (run.sh) in git bash

I am trying to use git bash to run my .sh file that was generated using Makefile.
When running the command ./run.sh I get this message ./run.sh: line 1: /home/user/run: No such file or directory
To run a script file (using Git Bash), you do the following:
Add a "sh-bang" line on the first line (e.g. #!/bin/bash OR #!/usr/bin/env sh) this is how git bash knows a file is executable.
Use ./ (or any valid dir spec): ./script.sh
Note : any "sh-bang" will work
You are using git bash so I suppose you are using Windows.
As for me I always use shebang on my scripts. Depending on the content of your script, you may add one of the following lines at the first line of your script.
#!/bin/sh
#!/bin/bash
#!/usr/bin/perl
#!/usr/bin/tcl
#!/bin/sed
#!/usr/awk
#!/usr/bin/python
If you still have problems running the script with ./run.sh command, you may try to use sh run.sh (on Git bash) and it should execute the script just as ./run.sh does it on linux.
This error message says that the first line of the script tries to execute an executable program named run in your home directory, and this does not exist.
I don't know what run.sh is supposed to do, but if you want to execute it a program, you need to make sure that the program exists, for instance by creating it.

Running Python Function from Bash

I have created a function configure_propeties() in configure.py that I'm trying to call in a bash script using the following command:
python3.6 -c "import configure; print configure.configure_properties()"
After executing the the bash script I get the following warning:
warning
Both the script and configure.py are in the same directory. Not sure what's happening it doesn't seem to fail on the import. Has anyone seen this issue before?
print "blabla" its only available in python 2, with python 3 you have to use parentheses print("blabla"), it would be better to set your python script inside .py file, just make sure the python executable is in your PATH environment variable then add in your script
python3 path/to/the/python_script.py
In the bash script file bashFile.sh, put this :
#!/bin/bash
python3 path/to/the/python_script.py
then you can execute your bash file bashFile.sh :
bash path/to/bashFile.sh
Use parentheses for print, as is necessary for Python 3

How do I execute a .lua file in CygWin?

I have just installed CygWin and curl because I wanted to do something unrelated. But now, I want to execute a .lua file in CygWin and I want the results to print on the current window, the CygWin window. I want it to be like the equivalent of just opening CMD and then do cd <directory where the file is>. And then just do <filename>.lua and it prints the results. So how would I go about doing that? Sorry, I'm kinda new to Linux, Unix, CLI, ect., and I don't know much about the bash command.
I tried using the method from here: How do I execute a file in Cygwin?
I just did ./<filename>.lua and I get
./<filename>.lua: line 1: syntax error near unexpected token `"Hello world"'
./<filename>.lua: line 1: `print("Hello world")'
The file just has
print("hello world")
If your file is marked as an executable, running ./<filename>.lua will default to executing the file as a shell script, (i.e., sh, bash, zsh, etc.). This results in the error you see, which is easily recreated.
In bash:
$ echo 'print("Hello world")' > script.sh && chmod +x script.sh && ./script.sh
./script.sh: line 1: syntax error near unexpected token `"Hello world"'
./script.sh: line 1: `print("Hello world")'
The first thing you need to do is make sure Lua is installed (rerun the Cygwin setup GUI, or use a tool like apt-cyg), and is located in your $PATH.
Then instead of executing the file directly, run it with the Lua interpreter.
$ lua <filename>.lua
Alternatively, use a shebang directive to instruct the shell on how the file should be executed.

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