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.
Related
I'm trying to run a CLI tool in Linux (Mint) which allows me to edit subtitles. It is named subedit: github link. In order to run it, I've added executable permission with chmod +x and added it to the path in bash. However, when I run it, I get the following error message:
bash: /home/main/Documents/shellTools/subedit/subedit: /usr/bin/bash: bad interpreter: No such file or directory
I'm not very experienced with external bash programs and forgot to do something that would be obvious in hindsight.
When I do echo $PATH this is the output:
/home/main/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/main/Documents/shellTools/subedit/
Could somebody please help?
Assuming bash is installed, (it usually is), change the first line of subedit from:
#!/usr/bin/bash
to:
#!/bin/bash
Or if one would prefer not to edit subedit, try this one-liner covering what Al-waleed Shihadeh suggested:
ln -s "$(which bash)" /usr/bin/bash
It seems that you don't have bash installed, you can verify that by running
which bash
if the above command returns "bash not found", then you need to install it.
In case the above command returns a path, you can use the below command to add a symlink to the expected path
ln -s $(path from the above command) /usr/bin/bash
Use the command termux-chroot ONCE!
If you want to always run at the start of a session, be sure to check if it was never run before.
if [ -z $CHROOT ]; then
CHROOT=1
termux-chroot
fi
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.
I am running the following simple script to test the block of code:
#!/bin/bash
# Load nvm and install latest production node
source $HOME/.nvm/nvm.sh
nvm install v0.10.12
nvm use v0.10.12
If the file name is test.sh with the above code, I get the following error:
test.sh: 5: test.sh: source: not found
test.sh: 6: test.sh: nvm: not found
test.sh: 7: test.sh: nvm: not found
Based on my review in SO, it appears the error with the script is originating at ==> source: not found. When I type this at CLI myself the code works fine. For some reason I am not able to execute this code through a shell script.
Any advice would be appreciated.
Your shebang line says Bash but the symptom suggests that you are running it with sh, which doesn't have a source command. Superficially, changing source to just a dot will fix that, but of course, if the sourced file contains any Bashisms, it will fail on those instead (perhaps more subtly, with incorrect results but no error message).
#!/bin/sh
# Load nvm and install latest production node
. $HOME/.nvm/nvm.sh
nvm install v0.10.12
nvm use v0.10.12
Usually, it is better to mark the script as executable and have the OS select the correct interpreter through the shebang mechanism; then you don't have to remember whether the script file contains Bash commands or sh commands or Python code or compiled code or whatever, nor notice if this changes from one version to another.
So instead of
sh script
you'd do
chmod +x ./script # the first time
./script
Once the permissions are correct, only the second line is needed. There are some minor additional caveats, which is why some busy developers sometimes give the simplest possible instructions for the immediate problem.
chmod +x $HOME/.nvm/nvm.sh
then
sh $HOME/.nvm/nvm.sh or . $HOME/.nvm/nvm.sh
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
i have a script that reads a file line by line
the code is
FILE=commands.txt
while read CMD; do
echo "$CMD"
done < "$FILE"
This code is stored in a script file vxm_alarm.sh
In Korn shell, this loop works perfectly, when i run the command vxm_alarm.sh. In bash however i get the following error
vxm_alarm.sh: syntax error at line 4: `done' unexpected
In Bash I'm executing the script using the command sh vxm_alarm.sh. what am i doing wrong? And why can't we execute a script simply by doing this in bash
chmod +x filename.sh
filename.sh
Your code works on my machine using GNU bash 4.1.5
Try adding a shebang to the top:
#!/bin/sh
FILE=commands.txt
while read CMD; do
echo "$CMD"
done < "$FILE"
If you run sh vxm_alarm.sh you are most likely not running Bash. Try sh --version - If you get anything other than a version string, it's not Bash. Try running bash vxm_alarm.sh instead.
To be able to run a script without a path before it it has to be in one of the directories listed in the PATH variable. For example, if
echo "$PATH"
prints
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
you can put filename.sh in /usr/local/sbin, /usr/local/bin, /usr/sbin, /usr/bin, /sbin or /bin and run it as simply filename.sh. If you want your script to be run from a directory not in the path, you have three choices:
Modify $PATH to include the directory where the script resides.
Run it with a relative or absolute path.
Create an alias or function pointing to the relative or absolute path.
I'd like to answer this part of your question:
why can't we execute a script simply by doing this in bash
chmod +x filename.sh
filename.sh
As others already pointed out in part, there are several things required for that to work:
execution rights (You ensured that with your chmod command)
the shebang, so the system knows what shell/interpreter to use
#!/bin/bash
(it is important to say bash if you want bash and not sh)
make sure the command is found. This is the case when its directory is found in PATH. However what you'd rather do in this case is specify the directory. For the current directory you can do it like this
./name-of-the-script
In contrast to DOS and (IIRC) the various Windows Command line interfaces, Unix systems usually don't have the current directory on the PATH. It is possible to add it, but discouraged due to severe implications on security.