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.
Related
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
apps="http:git.abc.com";
cluster-ui="http:git.xyz.com";
customer-ui="http:git.xxx.com";
SERVICE=$1;
My requirement is if I pass service name as a 'apps' then I need to clone the $apps url.
Here
if [ $Service -eq apps ]
not think a good approach as my repo url might get increased so more and more loop will come
Any suggestions?
The $ sign assigns the input argument, so we're getting first input if it matches the below variable, so do what you want inside if condition.
#!/bin/bash
apps="http:git.abc.com";
clusterui="http:git.xyz.com";
customerui="http:git.xxx.com";
#SERVICE=$1;
#Store global
repo=''
# if empty parameter is passed
if [ $# -lt 1 ] ; then
echo "Parameters Need"
exit 1
fi;
# for search the correct parameter
if [ $1 = "apps" ]; then
repo=$apps
elif [ $1 = "cluster-ui" ] ; then
repo=$clusterui
elif [ $1 = "customer-ui" ] ; then
repo=$customerui
else
echo "Not found"
fi;
echo $repo
Note just repeat elif [ ] ;then for more entries or think!
how to access run this file like this sh ./file.sh apps just replace apps with yours. make sure you have permission to execute the file if you don't have, give it to permission like below
chmod 766 file
now run the shell script sh ./file.sh clusterui
'Case statement' would suit here more than if ladder
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
I'm writing my first bash script that will do some visual testing using wraith. I've stripped down the code to make it easier to read.
What I'm trying to write:
- The BASH command accepts an argument - 1, 2 or 3. i.e. regressiontest 1
- server will be assigned the argument passed
- alias config will be assigned to wraith capture configs/capture-staging-1.yaml, wraith capture configs/capture-staging-2.yaml or
wraith capture configs/capture-staging-3.yaml
- The echo statement is written to a txt file.
The script works as expected. The only issue is:
If run regressiontest 1, all good, runs wraith capture configs/capture-staging-1.yaml as expected.
I run regressiontest 2, I would expect it to run wraith capture configs/capture-staging-2.yaml but seems to run wraith capture configs/capture-staging-1.yaml again.
It seems to be running using the previous config file. If I close and open terminal again, it works as expected but if I run the same command with a different argument consecutively it seems to always run the first command I use.
What am I doing wrong?
I'm new to BASH scripts and am having trouble googling to find an answer
function regressiontest {
regressionfolder=~/path/to/folder
cd $regressionfolder
alias config
if [ $# -eq 0 ]; then
echo "No arguments provided - USAGE: regressiontest <server>"
return 0
else
server=$1
fi
if [ $server != 1 ] && [ $server != 2 ] && [ $server != 3 ]; then
echo "Visual Regression Testing argument invalid - USAGE: regressiontest <server>"
return 0
elif [ $server == 1 ]; then
server="1"
alias config='wraith capture configs/capture-staging-1.yaml'
elif [ $server == 2 ]; then
server="2"
alias config='wraith capture configs/capture-staging-2.yaml'
elif [ $server == 3 ]; then
server="3"
alias config='wraith capture configs/capture-staging-3.yaml'
fi
echo "https://website-staging-$server/" > data/server.txt
config
}
Any help would be much appreciated.
Thanks, All
Moe
You are thinking right, but making things harder than need be. Your initial part of the script is fine, though I would validate that the cd succeeds, e.g.
regressionfolder=~/path/to/folder
cd "$regressionfolder" || {
printf "error: unable to change to %s\n" "$regressionfolder" >&2
return 1
}
(note: a return of 1 generally indicates error and always double-quote your variables)
After your check on "$server" != 1 ... all you need to do is set your alias with $server as the number. No additional if ... elif ... are required, e.g.
if [ "$server" != 1 ] && [ "$server" != 2 ] && [ "$server" != 3 ]; then
echo "Visual Regression Testing argument invalid - USAGE: regressiontest <server>"
return 1
fi
alias config="wraith capture configs/capture-staging-$server.yaml"
config
}
(note: always double-quote variables withing [...])
Eliminate the alias
There is no need for the alias, you can simply run:
wraith capture configs/capture-staging-$server.yaml
Putting it altogether, you could do something similar to:
function regressiontest {
regressionfolder="$HOME/path/to/folder"
cd "$regressionfolder" || {
printf "error: unable to change to %s\n" "$regressionfolder" >&2
return 1
}
if [ $# -eq 0 ]; then
echo "No arguments provided - USAGE: regressiontest <server>"
return 1
else
server=$1
fi
if [ "$server" != 1 ] && [ "$server" != 2 ] && [ "$server" != 3 ]; then
printf "Visual Regression Testing argument invalid - "
printf "USAGE: regressiontest <server>\n"
return 1
fi
wraith capture "configs/capture-staging-$server.yaml"
}
(note: also the use of "$HOME" instead of ~. While ~ will expand on the command line, you will quickly run into problems using it within scripts)
Use a case Statement
A shorter more condensed version of your function using case ... esac would probably be a bit better, e.g.
function regressiontest {
regressionfolder="$HOME/path/to/folder"
cd "$regressionfolder" || {
printf "error: unable to change to %s\n" "$regressionfolder" >&2
return 1
}
case "$server" in
[123] ) wraith capture "configs/capture-staging-$server.yaml";;
* ) printf "Visual Regression Testing argument invalid - "
printf "USAGE: regressiontest <server>\n"
return 1;;
esac
}
I don't think you want to declare aliases, but store commands for later execution; just remove the "alias" from alias config='…' and at the end call it via $config.
Code
if [ $setup==="y" ]
then
echo "kurulum:"$setup
exit
full_dir=$full_dir"/public"
else
echo "Sub-Public folder is exist? [public,web]"
read folder_extend
if [ $folder_extend ]
then
full_dir=$full_dir"/"$folder_extend
fi
fi
Setup param $setup view as "n" after run sh but still condition firts part run. Where wrong code ?
Thanks.
Change it to:
if [ "$setup" = "y" ]
then
echo "kurulum:"$setup
exit
full_dir=$full_dir"/public"
else
echo "Sub-Public folder is exist? [public,web]"
read folder_extend
if [ "$folder_extend" ]
then
full_dir=$full_dir"/"$folder_extend
fi
fi
It should just be a single =, and you need spaces around it. You should also quote variables in most contexts, in case they contain spaces.