test: -v: unary operator expected - bash

I try to run such code via "github actions"
- name: Run tests
run: |
set -e
...
test -v A || export B=42
shell: bash
and got error at line test -v A || export B=42:
test: -v: unary operator expected
I have no idea what is this about,
if I run this command in my local bash all works as expected,
what is wrong in my yaml code for "github actions"?

By any chance does your test happen to be running under a macOS CI environment? test -v tests if a variable has been set, but, it was only added in Bash 4.2.
macOS by default comes with Bash 3.2, and that's the expected error if it doesn't recognize -v as a unary operator.
To fix this, you can install the latest Bash, or use a different approach for testing for existence:
[ -z "${A+x}" ] && export B=42

Related

Shell Script failing on alpine linux while working in mac terminal

The following line fails when run in a alpine docker container:
toDelete=( $(curl --silent $url/_cat/indices\?format=json | jq -r '.[].index | select(startswith('\".kibana\"'))') )
The following error message appears:
run.sh: line 1: syntax error: unexpected "("
When I run the command in the terminal on my mac, everything works properly. The brackets are added so that the result (variable toDelete) is interpreted as array and can be looped through with a for loop like so:
for index in "${toDelete[#]}"; do
curl -X DELETE $url/$index
done
Any help in how to solve this problem is appreciated!
Marking down the answer.
The issue was with the interpreter.
worked after making the below change.
["/bin/ash", "run.sh"]
the passed one was
["/bin/sh", "run.sh"]

travis go error 'The command "eval go get -t -v ./..." failed'

I have a pretty straightforward setup..
- a Travis.yml file : https://github.com/openassistive/OpenATFrontEnd/blob/master/.travis.yml
which has this line:
before_script:
- go get -u -v github.com/spf13/hugo
but it fails - with
The command "eval go get -t -v ./..." failed. Retrying, 2 of 3.
(https://travis-ci.org/openassistive/OpenATFrontEnd/builds/166105574)
I can't figure it out. I see the language is set correctly - and looking at other SO posts the version number is correct. Is there a different version I should be using?
Read this, the go get .... is part of the default go build script on travis, if no makefile is found.
A simple solution may be to add a Makefile with an empty recipe
$ cat Makefile
target: ;
$ make && echo "ok"
make: « target » uptodate.
ok
So travis will set the default install step to true, which should avoid the got get

Eval in docker-machine: terminal vs shell script

I'm trying to run a simple shell script to automate changing docker-machine environments. The problem is this, when I run the following command directly in the Mac terminal the following is outputted:
eval $(docker-machine env default)
docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default * digitalocean Running tcp://***.**.***.***:**** v1.12.0
So basically what you would expect, however when I run the following .sh script:
#!/usr/bin/env bash
eval $(docker-machine env default)
The output is:
./run.sh
docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default digitalocean Running tcp://***.**.***.***:**** v1.12.0
So basically, it is not setting it as active and I cannot access it.
Has anyone run into this issue before and knows how to solve it? Seems really strange to me, have got pretty much everything else running and automated apart from this facet.
Cheers, Aaron
I think you need to source your shell script
source ./myscript.sh
as the exports in the eval are being returned to the process you started to run the shell in and then being disposed of. These need to go to the parent e.g. login shell
Consider a.sh
#!/bin/bash
eval $(echo 'export a=123')
export b=234
when run in two ways
$ ./a.sh
$ echo $a
$ echo $b
$ source a.sh
$ echo $a
123
$ echo $b
234
$

RVM 1.9.2 install fails on Cloud9 IDE — bash syntax error

I'm trying a classic Ruby install via RVM un Cloud9, but I get a bash error:
bash: 1.1G: syntax error: invalid arithmetic operator (error token is ".1G")
UPDATE:
So I did some digging into the RVM script which does the magic, and it seems this error comes from this part of the script /usr/local/rvm/scripts/functions/utility (line 416):
__rvm_calculate_space_free()
{
# OpenBSD does not have 'df -m' param
__free_space="$( \command \df -Pk "$1" | __rvm_awk 'BEGIN{x=4} /Free/{x=3} $3=="Avail" {x=3} END{print $x}' )"
if [[ "${__free_space}" == *M ]]
then __free_space="${__free_space%M}" # some systems ignore -k and print M
else __free_space="$(( __free_space / 1024 ))"
fi
}
This is way beyond my knowledge, but it would seem to me that the reported free space includes that G which somehow messes with the operation, hence the arithmetic error.
Any help appreciated.
A solution is to modify your PATH variable like from:
$ echo $PATH
/home/ubuntu/.nvm/v0.10.30/bin:/usr/local/rvm/gems/ruby-2.1.1#rails4/bin:/usr/local/rvm/gems/ruby-2.1.1#global/bin:/usr/local/rvm/rubies/ruby-2.1.1/bin:/mnt/shared/bin:/home/ubuntu/workspace/node_modules/.bin:/home/ubuntu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/mnt/shared/sbin:/opt/gitl:/opt/go/bin:/mnt/shared/c9/app.nw/bin:/usr/local/rvm/bin
to
$ export PATH=/home/ubuntu/.nvm/v0.10.30/bin:/usr/local/rvm/gems/ruby-2.1.1#rails4/bin:/usr/local/rvm/gems/ruby-2.1.1#global/bin:/usr/local/rvm/rubies/ruby-2.1.1/bin:/home/ubuntu/workspace/node_modules/.bin:/home/ubuntu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/mnt/shared/sbin:/opt/gitl:/opt/go/bin:/mnt/shared/c9/app.nw/bin:/usr/local/rvm/bin:/mnt/shared/bin
Note the /mnt/shared/bin dir is now at the last position of the PATH variable.
Could you try installing it as root (so with 'sudo rvm install 1.9.2')?

What's wrong with this bash script?

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?
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
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 :).

Resources