How do I execute a .lua file in CygWin? - windows

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.

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.

Bash script works in Terminal but in Applescript gives "python3: command not found"

SOLVED: see solution in replies
I have a bash script that looks like this:
#!/bin/bash
python3 /Users/me/path/to/my/file.py
python3 /Users/me/path/to/my/file2.py
Rscript /Users/me/path/to/my/rfile.R
python3 /Users/me/path/to/my/file.py
When I execute it from terminal with the command
bash /Users/me/path/to/my/shellscript.sh
it executes perfectly. However, I would like to be able to run it from Keyboard Maestro or AppleScript. In AppleScript, when I run
do shell script "bash /Users/me/path/to/my/shellscript.sh"
I get the following error message:
error "/Users/me/path/to/my/shellscript.sh: line 2: python3: command not found
/Users/me/path/to/my/shellscript.sh: line 3: python3: command not found
/Users/me/path/to/my/shellscript.sh: line 4: Rscript: command not found
/Users/me/path/to/my/shellscript.sh: line 5: python3: command not found" number 127
I receive the same error message if I run the equivalent command in Keyboard Maestro. I have tried changing python3 to python, but it fails anyway because it runs the files in Python 2 (despite the shebang of the Python files specifying Python 3), and the Rscript command would surely still throw an error.
What is the difference between the environments that causes the shell script to fail in Keyboard Maestro and AppleScript but not Terminal?
I got it to work by changing the command to PATH=$PATH:/usr/local/bin; bash /Users/me/path/to/my/shellscript.sh.

Failure in executing shell script producing output on Ubuntu on Windows

I have had a hard time in executing my shell scripts on bash on Ubuntu on windows 10. The script is very simple:
# file name: submission.sh
echo "Hello world" > output.txt
When I executed it with a command sh submission.sh, it gave me an error:
$ sh submission.sh
: Directory nonexistentssion.sh: cannot create output.txt
However, when I changed the script into
# file name: submission.sh
echo "Hello world"
and executed it with the same command sh submission.sh, it gave me the right output
$ sh submission.sh
Hello world
It seems like bash on Ubuntu on Windows cannot get it right when the script involves directing the output to a file. Is there any solution or workaround to this?
EDIT:
Details on my system:
Program: "Bash on Ubuntu on Windows"
OS: Windows 10 Version 1709
EDIT:
Typing the command directly on the terminal works, i.e.
$ echo "Hello world" > output.txt
$ cat output.txt
Hello world
I still wants to put the commands on a file and execute the file instead of writing the command directly to the terminal, and this is still unsolved.
You appear to have mangled text in nonexistentssion.sh: and No such file or directorytput.txt which suggests you might have Windows line-endings in the file \r. If you created the script using a Windows program (like Notepad) then that could be the case.
If you have dos2unix then run it on your script and try again.
By the way for future reference, running sh is not the same as running bash. In this case it would have made no difference, but sh is a POSIX shell, full bash has many extensions which will not work under sh.
Some platforms run sh as a symbolic link to bash which fools people into thinking they are the same, but bash detects this and switches to POSIX mode when running as sh. It is a common issue here.
In your submission.sh file, is better to add a shebang as first line.
Another thing you want to consider if you want to use sh instead of bash is to replace echo with printf, for portability
Your code should look something like:
#!/bin/bash
printf "Hello world!\n" > output.txt
You can call it simply by ./submission.sh and because of the shebang, your terminal will know how to open it [:
P.S. Keep in mind that because of the standard umask in Ubuntu, you might want to execute chmod u+x submission.sh before running it.
Also, notice that your error is probably caused by a permissions issue.
Try adding write permissions in the folder you are launching the script.

Javac command returns "Invalid flag" when run from SHell script

So I have a .sh script (in Ubuntu):
#!/bin/bash
javac betz2.java
When I run it, it says:
Invalid flag: betz2.java
WHen run in terminal, it works just fine? why?
javac betz2.java
Maybe javac is not in the Bash path, but your command line is using a different shell that does have it. In your command line type echo $SHELL and you'll see if it is bash or not.
Did you edit your file in Windows and then opened it in a different operating system? (Linux). Carriage returns might be causing you trouble, just create a new file and paste your command.

Installing Homebrew via shell script

This must be an easy one. I'd like to install Homebrew via a shell script on OS X.
Homebrew's recommended installation from the terminal works,
$ ruby <(curl -fsSk https://raw.github.com/mxcl/homebrew/go)
but if I put the following in a file test.sh,
#!/bin/sh
ruby <(curl -fsSk https://raw.github.com/mxcl/homebrew/go)
then execute it,
$ sh test.sh
I receive the following error:
test.sh: line 2: syntax error near unexpected token `('
test.sh: line 2: `ruby <(curl -fsSk https://raw.github.com/mxcl/homebrew/go)'
What is the correct syntax to use in a shell script to get this to work and why is it different from the command line? Thanks!
It's complaining because sh doesn't have that syntax, but bash does. Use #!/bin/bash instead.
Also, no need to use the sh command to execute shell scripts (that's the whole point of putting the hashbang!). Just chmod +x script.sh and invoke with ./script.sh
When you run bash as sh it emulates sh, which has many fewer features than bash (including one you're trying to use here). Use /bin/bash instead.

Resources