Cannot make bash script work from cloud-init - linux

Obviously I am doing something wrong here.
Cloud init script /etc/cloud/cloud.cfg
...
runcmd:
- [ sh, /opt/cloud-init-scripts/whatever.sh ]
The script /opt/cloud-init-scripts/whatever.sh
#!/bin/bash
...
. /home/ubuntu/third-party/script.sh --silent
Third party script /home/ubuntu/third-party/script.sh
#!/usr/bin/env bash
function some_function() {
...
Error I am getting in /var/log/cloud-init-output.log
/opt/cloud-init-scripts/whatever.sh: 3: /home/ubuntu/third-party/script.sh: Syntax error: "(" unexpected
I must be missing something obvious here. I tried using source, . and sh when calling the third party script, tried changing shebangs everywhere but no success.
If I run the same command from command line it works.

You have specified sh shell under runcmd, but have she-bang set to bash. The latter does not matter because if you run as sh /opt/cloud-init-scripts/whatever.sh it will be run with sh shell. I guess you are probably using a non POSIX shell feature which is incompatible with the sh shell.
Or alternatively if your intention is to run the script in bash shell, change the runCmd in cloud-init script to
runcmd:
- [ bash, /opt/cloud-init-scripts/whatever.sh ]

Related

Is there a package a need to install to use operators with Bash? [duplicate]

I want to run this script:
#!/bin/bash
echo <(true)
I run it as:
sh file.sh
And I get "Syntax error: "(" unexpected" . I found some similar situations but still can't solve this.
I'm a beginner at shell scripting , but as I understand:
the shebang I use is correct and chooses the bash shell , so the process substitution syntax should work
I try the same from the command line and it works. I checked with echo $0 and it gives me "bash" , so what's the difference from running the command in the command line and from a script that invokes the same shell?
Maybe it's something simple, but I couldn't find an explanation or solution.
You should run your script with bash, i.e. either bash ./script.sh or making use of the shebang by ./script.sh after setting it to executable. Only running it with sh ./script.sh do I get your error, as commented by Cyrus.
See also: role of shebang at unix.SE
Remove export POSIXLY_CORRECT=1 from your ~/.bashrc or ~/.profile (etc.) files.
The issue is that process substitution is an added bash feature that is not part of the posix standards.
sh file.sh
errorsh: 3: Syntax error: "(" unexpected
solution:
bash file.sh

Can substring expansion be used in dash shell or bourne shell?

I'm converting an app to a new image, and the existing commands use substring expansion to set the artifact version like so: mvn clean versions:set -DnewVersion="0.1.$VCSINFO.I${INFO:0:6}.M$OTHER_INFO". I'm using a ubuntu image that defaults to /bin/sh, and I am unable to figure out how to either do something equivalent in bourne shell, or switch shells to run the command. I know bash is installed because I can see it in /etc/shells.
I tried using RUN ['/bin/bash', '-c', '...'] but I can see it is just running that command like so The command '/bin/sh -c ['/bin/bash', '-c',.... What is the best way to convert this functionality over to this new image?
You can run a bash command in two ways, even from sh: Either by passing the string '/bin/bash path/to/your/cmd' to the -c option of sh, or by setting the x-bit in cmd and having as the first line in cmd a #!/bin/bash.
Hence in your setting I would try either a RUN ['/bin/bash /path/to/your/cmd'] or just do a RUN ['/path/to/your/cmd'] and ensure that cmd has the #! line mentioned above, or complicated but fail safe - write a sh wrapper script, which then invokes the bash script in turn. Hence, if this wrappe script is called /path/to/your/cmdwrapper.sh, its content would be
:
/bin/bash /path/to/your/cmd

syntax error when compare two files using shell script [duplicate]

I want to run this script:
#!/bin/bash
echo <(true)
I run it as:
sh file.sh
And I get "Syntax error: "(" unexpected" . I found some similar situations but still can't solve this.
I'm a beginner at shell scripting , but as I understand:
the shebang I use is correct and chooses the bash shell , so the process substitution syntax should work
I try the same from the command line and it works. I checked with echo $0 and it gives me "bash" , so what's the difference from running the command in the command line and from a script that invokes the same shell?
Maybe it's something simple, but I couldn't find an explanation or solution.
You should run your script with bash, i.e. either bash ./script.sh or making use of the shebang by ./script.sh after setting it to executable. Only running it with sh ./script.sh do I get your error, as commented by Cyrus.
See also: role of shebang at unix.SE
Remove export POSIXLY_CORRECT=1 from your ~/.bashrc or ~/.profile (etc.) files.
The issue is that process substitution is an added bash feature that is not part of the posix standards.
sh file.sh
errorsh: 3: Syntax error: "(" unexpected
solution:
bash file.sh

What is the difference between `./example.sh` and `sh example.sh`

I am trying to play with bash and arrays. But executing a sample script, I got an unexpected syntax error message: example.sh: 3: example.sh: Syntax error: "(" unexpected. And this is the script
#!/bin/bash
array=( one two three )
If I run the script with ./example.sh it works and no errors are displayed. But if I run sh example.sh I get the error message.
I thought that these two commands are the same:
sh example.sh
./example.sh
so ... what is the difference between the two?
When you launch it via ./example.sh then the command specified in the first line of the script is used to interpret the content. So your script executes in a bash, where such syntax is allowed for arrays.
When you launch it via sh example.sh then sh is the command that is used to interpret the content of the file. sh is the original Unix shell (aka Bourne shell) and this shell is a little more rude than bash (Bourne again shell). You don't have such arrays. Note that in sh the first line of your script is just interpreted as a comment.
by using sh example.sh
- you are specifying what shell interpreter to use for that script. Example being "bash example.sh" instead of "sh example.sh" etc etc.
Running scripts this way disregards the "shebang (#!/bin/bash)" that you have specified inside of the script. Since you wrote a bash script but are trying to run it as just "sh", this is why it is failing
by using ./example.sh,
- You are specifying to run the script from your current directory. This will attempt to run the script in whatever shell you are currently in unless a shebang is specified. Since you have a "shebang" specified to run the script in bash... this is why it is working.
array_name=(value1 ... valuen)
This is how to initializes an array in bash only. When you execute ./example.sh, the shebang line #!/bin/bash tells the system to use bash to execute.
However, when you execute sh example.sh, sh is used to execute. In many Unix systems (like Linux), sh is equivalent to bash. It seems sh is a different shell on your system.

Set bash script in Eclipse

I have a bash script .sh which needs to be executed as a Target Simulator in Eclipse. The problem is, if I run the script with sh run.sh command in terminal, it throws Bad Substitution error. But it works perfectly with bash run.sh. Apparently, Eclipse run it with sh command cause it gives the same error in console. But how can I make Eclipse to run the script with bash instead?
I'm on Ubuntu 13.10.
bash and sh aren't the same shell. There are many constructs valid in bash that are not understood by sh.
Have you provided a correct sheebang as the first line of your script?
#!/bin/bash
If so -- and if Eclipse insist on running script with sh, you still have the option of wrap your script in a heredoc and pass it to bash explicitly:
sh$ cat run.sh
bash << EOF
#
# Here is your bash script
#
EOF
This is mostly a hack until you find how to instruct Eclipse of using the right shell. I'm sure there is a way!

Resources