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
Related
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 a bash script that looks like this:
if [ $# -eq 2 ]; then
REDIS_HOME=$2
else
if [ $# -eq 3 ]; then
REDIS_HOME=$2
PORTS=$3
else
REDIS_HOME="/usr/local/redis"
fi
fi
case "$1" in
start)
if [ -z "$PORTS" ]; then
cmd="$REDIS_HOME/bin/redis-server $REDIS_HOME/redis-6379.conf"
$cmd
else
IFS=",";
for port in $PORTS
do
cmd="$REDIS_HOME/bin/redis-server $REDIS_HOME/redis-$port.conf"
$cmd
done
fi
;;
*)
;;
esac
exit 0
When I run the script with my_script.sh start, it works well by using the default redis home in the script. But when I ran it with my_script.sh start /usr/local/redis 6379, it says "/usr/local/redis/bin/redis-server /usr/local/redis/redis-6379.conf" No such file or directory.
Basically, I'm passing in the same REDIS_HOME, but I cannot figure out why the script cannot resolve the path if it's passed in as a parameter.
IFS=, is causing the problem. When you do
$cmd
it uses $IFS to break the expansion of $cmd into words. Since space is not in $IFS, the space is not treated as a delimiter between the program name and the argument, so the entire result is treated as the program name. And of course it's not found.
I'm not sure why you need the $cmd variable in the first place. You can just do:
IFS=",";
for port in $PORTS
do
$REDIS_HOME/bin/redis-server $REDIS_HOME/redis-$port.conf
done
So, I have a simple script whose only purpose is to help me in my lazyness by allowing me to type less thing when setting acl for someone using blih
#!/bin/sh
if [ "$1" "$2" ]; then
~/.blih.py -u X.X#X.eu repository setacl "$1" "$2" rw
fi
I've named it setacl.sh and set it as an aliases in my .bash_aliases
alias setacl='~/.bash_scripts/setacl.sh'
and yet when I use it, I get the following
setacl Java_epicture_2017 X
/root/.bash_scripts/setacl.sh: 3: [: Java_epicture_2017: unexpected operator
What am I doing wrong?
If you're trying to verify that both arguments are set, write:
if [ -n "$1" ] && [ -n "$2" ]
Or more simply, check the number of arguments passed:
if [ "$#" -eq 2 ]
cplane_pid=`pidof hnb_gw.exe`
if [ -z $cplane_pid ]
then
STATUS=`failure`
echo "Cplane hnbgw running $STATUS"
else
STATUS=`success`
echo "Cplane hnbgw running $STATUS"
fi
echo
If there are multiple instances of hnb_gw.exe, pidof will return multiple pids. The -z of [ expects only one pid. One solution might be to use the -s switch of pidof to return only one pid.
You need to Use More Quotes™:
if [ -z "$cplane_pid" ]
Adding set -x before and set +x after the command shows you what it results in. For example:
$ cplane_pid="1 2 3"
$ set -x
$ [ -z $cplane_pid ]
+ '[' -z 1 2 3 ']'
bash: [: too many arguments
In other words, each of the whitespace-separated values in the variable was used as a single parameter. Since -z requires exactly one parameter, this results in a syntax error.
Rather than saving this as a variable, you can simply do
if ! pidof hnb_gw.exe > /dev/null
If the process doesn't exist, it will return 1 ("false").
When you execute
cplane_pid=`pidof hnb_gw.exe`
then cplane_pid can contain more (space separated) items.
So the expansion in
if [ -z $cplane_pid ]
will become
if [ -z firstPid secondPid etc ]
and that is your error "[: too many arguments"
You can solve this with quoting the variable (you should do this ALWAYS in shell)
if [ -z "$cplane_pid" ]
or use [[ (if it's installed on your system), which is better in many ways. For instance you don't need to quote variable :)
if [[ -z $cplane_pid ]]
is the same as
if [[ -z "$cplane_pid" ]]
For testing purposes (and erros like this) use -x hasbang bash option
#!/bin/bash -x
or use debug sections
-- normal code --
set -x # debug section starts here
[ -z $cplane_pid ] && echo zero
eval something
set +x # debug section ends here
-- normal code --
also you can call the script
/bin/bash -x yourScript.sh
pidof can return more than one pid, in these cases, your test will get too many arguments.
We have code to check for a node install:
which="type -p"
if [ $SHELL = "/bin/zsh" ]; then
which="whence"
fi
# make sure that node exists
node=`$which node 2>&1`
ret=$?
if [ $ret -ne 0 ] || ! [ -x "$node" ]; then
<"This error code is returned">
But when I run this with ZSH (OhMyZsh) it returns a 127 (does not exist). Commenting out the which="whence" lets it run fine.
Without removing the whole aliasing bit is there any way to have ZSH play along with this? Ideally I'd like to make a change on my end to make this work rather than modifying this code at all.
You mean, you run $node and it appears that you’ve tried to run command whose name is node --alias-args which does not exist?
If this is true, change the third line to use whence -p: it has the same output as type -p in bash. If not, please, explain when this code is returned.
Update: I do not know what was done in ohmyzsh (though I have not a single idea how to make a builtin not found) so just try to rewrite the code in this way:
# At the very top
if [ -n $ZSH_VERSION ] ; then
emulate -L zsh
endif
<...>
which="type -p"
if [ -n $ZSH_VERSION ] ; then
which=( whence -p ) # Changes variable type as well
endif
node=`$which node 2>&1`
if (( ? )) || ! test -x $node ; then
<...>