I have Pipeline job in Jenkins and there is a step that executes this bash script:
sh """
$ANDROID_HOME/platform-tools/adb pull /sdcard/Pictures/screenshots
if [ "$DEFAULT_LOCALE" = "en" ]
then
DEFAULT_LOCALE="en-US"
fi
if [ "${env.UPDATE_BASE}" == "true" ] || [ ! -d "${env.CACHE_HOME}/${env.BRANCH}" ]; then
if [ ! -d "${env.CACHE_HOME}/${env.BRANCH}" ]; then
mkdir -p ${env.CACHE_HOME}/${env.BRANCH}
fi
for imgfile in screenshots/*.png; do
if [[ $imgfile == *"_${env.DEFAULT_LOCALE}-"*.png ]]; then
cp -rf screenshots/$imgfile ${env.CACHE_HOME}/${env.BRANCH}
fi
done
else
rm -f screenshots/*_${env.DEFAULT_LOCALE}-*.png
cp -rf ${env.CACHE_HOME}/${env.BRANCH}/* screenshots
fi
"""
However, when the pipeline reaches this step, it fails with this error:
groovy.lang.MissingPropertyException: No such property: imgfile for class: groovy.lang.Binding
What is wrong in the script?
If all your variables are shell variables then you should use triple single quotes.
If you have a mix of shell and Groovy variables (or only Groovy ones) then you should use triple double quotes.
In order to defer the evaluation of the shell variables in the latter case, you need to escape their dollar signs using one of these forms (I'm not sure which):
if [[ \$imgfile == *"_${env.DEFAULT_LOCALE}-"*.png ]]; then
or
if [[ \\$imgfile == *"_${env.DEFAULT_LOCALE}-"*.png ]]; then
or
if [[ ${'$'}imgfile == *"_${env.DEFAULT_LOCALE}-"*.png ]]; then
Related
I want to create a bash function to load certain environment variables when called, but I'm getting the error loadenv:4: = not found. this function, along with the variables DEV_ENVIRONMENT_NAME, DEV_ENVIRONMENT_DIRECTORY, PROD_ENVIRONMENT_NAME and PROD_ENVIRONMENT_DIRECTORY are defined within my .zshrc file so the exported variables are available in the bash session I run the function in. But I don't know what it means by the error I mentioned.
function loadenv() {
environment=$1
envname=""
envdir=""
if [ "$environment" == "dev" ]
then
echo "Assuming development credentials"
envname="$DEV_ENVIRONMENT_NAME"
envdir="$DEV_ENVIRONMENT_DIRECTORY"
elif [ "$environment" == "prod" ]
then
echo "Assuming production credentials"
envname="$PROD_ENVIRONMENT_NAME"
envdir="$PROD_ENVIRONMENT_DIRECTORY"
fi
if [[ -z $envname || -z $envdir ]]
then
echo "Credentials for $environment not properly configured"
return 1
else
export APP_ENVIRONMENT="$envname"
export APP_DIRECTORY="$envdir"
return 0
fi
echo "Environment '$environment' not valid"
return 1
}
The error comes from the fact that the two forms of bash logical expressions are either (single brackets with single "="),
if [ "$environment" = "dev" ]
or (double brackets with "==" ),
if [[ "$environment" == "dev" ]]
If that script is meant to be bash, then you need to have
#!/bin/bash
as the first line in your script, for it to work, regardless of the environment.
Also, be sure to NOT source that script into your zsh. Otherwise, it will not execute as bash.
I have this function inside a .sh script :
prepare_for_test(){
stopresources;
initresources;
if [ "$1" = "--gf" ]; then
startglassfish;
fi
docker ps;
notify-send "Done!" "You can now test" -t 10000;
};
The script's name's preparefortests.sh. When I run it on bash, passing --gf or "--gf":
preparefortests.sh --gf
it does not run alias startglassfish, as if that if statement was false.
I even tried to add a check on the parameter:
if [ "$1" ] && [ "$1" != "--gf" ]; then
echo "uknown parameter $1"
fi
but it's not working neither, when e.g. I try to run it like:
preparefortests.sh hello
I'd expect "unknown parameter hello".
What am I doing wrong?
The comparison statement is correct:
if [ "$1" = "--gf" ]; then
startglassfish;
fi
There can be other issue like:
Make sure you pass $1 argument, while calling function:
Write prepare_for_test $1
The problem might be the alias used. For almost every purpose, aliases are superseded by shell functions. So either you need to make alias as function and export it or instead use special variable BASH_ALIASES. In your case:
if [ "$1" = "--gf" ];then
${BASH_ALIASES[startglassfish]};
fi
I have a bash script which checks for a paticular string and proceeds further but seems to be getting some error with binary operator expected
local artifacts=$(realpath artifacts)/middle-end
local env
local account_id=${1}
local branch_name=${2}
local user_environments=${3}
local gitlab_user_id=${4}
env=$(ci/scripts/get-details-env.py -m "${user_environments}" -u "${user_id}")
# Deploy to int1 from develop
echo "$branch_name"
if [ "${branch_name}" == "develop" ]; then
env=brt-int;
fi
if [ "${branch_name}" =~ ^brt-rc-.* ]; then
env=brt-uat;
fi
mkdir -p ${artifacts}
cd middle-end
ln -s ${NODE_PATH} ./node_modules
npm run build
I know what your problem is, you must be using the new test operator [[. [http://mywiki.wooledge.org/BashFAQ/031][bash new test details]
if [[ "${branch_name}" =~ ^atf-rc-.* ]]; then
env=atf-stage1;
fi
I have a bash script that checks if a file already exists or has changed. If either of these case are true, copy the file from one location to anther.
DIR="$( cd "$( dirname "${BASH_SOURCE}" )/my-dir" && pwd )"
FILE="file.json"
copy() {
local SAME=$(cmp --silent "${DIR}/${FILE}" "${PWD}/${FILE}")
if [ ! -f "${PWD}/${FILE}" ] || [ ! $SAME ]; then
cp "${DIR}/${FILE}" "${PWD}/${FILE}" && echo "'$FILE' has been copied." || echo "Copy of '$FILE' has failed.";
else
echo "'$FILE' already exists and has not changed (not copied).";
fi;
}
copy
But when the file exists and has not changed, it is still copied.
echo "$SAME" doesn't echo anything but echo $? echos the exit code 0
So my question is: is it possible to negate the output of the 'cmp' command in a condition?
Thanks.
You need to quote your parameter expansion. If $SAME is the empty string (and it always will be, because you use --silent), your test devolves to [ ! ]. Because ! is a non-empty string, the test succeeds.
if [ ! -f "${PWD}/${FILE}" ] || [ ! "$SAME" ]; then
SAME also needs to contain the output of cmp:
SAME=$(cmp "${DIR}/${FILE}" "${PWD}/${FILE}")
However, it would be better to ignore the actual output of cmp and use its exit status instead.
if [ ! -f "$PWD/$FILE" ] || ! cmp --silent "${DIR}/${FILE}" "${PWD}/${FILE}"; then
In the old Scratchbox one could do something like:
if [ -f /targets/links/scratchbox.config ]; then
echo "here goes my sbox-dependent stuff"
fi
but what about Scratchbox2, is there a way to find out this?
How about test the environment variable 'LD_PRELOAD'
if [[ $LD_PRELOAD =~ "sb2" ]]; then
true # is running under sb2
fi