Reputation: 56944
Here's my myscript.sh
:
alias apt-get-update="apt-get update -qq"
alias apt-get-install="apt-get install -f -y -qq --force-yes"
alias yum-install="yum --quiet --nogpgcheck -y install"
function ensure_cmd_or_install_package_apt(){
local cmd=$1
shift
local pkg=$*
hash $cmd 2>/dev/null || ( apt-get-update && apt-get-install $pkg )
}
When I run sh myscript.sh
I get:
myscript.sh: 5: myscript.sh: Syntax error: "(" unexpected
It looks perfectly fine to me; any ideas?
Upvotes: 0
Views: 65
Reputation: 1016
Also in general you should be explicit in your script header as to the app/shell you want to run the script with:
#!/bin/bash
If the script requires bash-isms then you should tell it to run with bash.
In your case you are on ubuntu (confirm) and ubuntu uses dash as the default shell (ie /bin/sh is a symlink to dash). Dash doesn't allow:
function name () {}
and instead just wants:
name () {}
In fact the first form should be avoided if possible since it's not portable. But if you use the function keyword, don't use parens since they are not required (it's an accident that it even works).
With regards to setting a script header, sometimes it's better to use env to find the program (say in the case of ruby or perl for instance where you might have numerous ruby/perl executables but the one furthest up on your path is the one you want to run with).
#!/usr/bin/env ruby
Is the way to go. Usually for shells, /bin/bash or /bin/csh etc is sufficient in your shebang, but never assume :).
Upvotes: 1
Reputation: 3357
Does running bash myscript.sh
fix it?
It could be your script is running in dash
instead of bash
.
According to this answer you can change it with the following command:
chsh
Upvotes: 1