What does [ "$var" ] && $var do? - shell

I was going through a codebase and came across the following piece of code in a shell script:
./some_script.sh
errcode=$?
if [ "$errcode" != 0 ]; then
[ "$SCRIPT_PATH" ] && $SCRIPT_PATH do_something
fi
SCRIPT_PATH is an environment variable which expands to the path of a script (say /usr/bin/abc.sh). However, I do not understand what the code in the if loop does. What does [ "$SCRIPT_PATH" ] do? Is there any use of it or was this just something written incorrectly? Only thing I can think of is that it was supposed to be [ -f "$SCRIPT_PATH" ] to check if file exists before running it, but I'm not sure anymore.
Any idea what else this could be doing?

It runs the program in SCRIPT_PATH if it is not empty or unset (because that's what the [ one_arg ] test does.)

It's a shorthand for the following:
if [ "$errcode" != 0 ]; then
if [ "$SCRIPT_PATH" ]; then
$SCRIPT_PATH do_something
fi
fi
It works by abusing the way chained conditions "short circuit" in an if statement.
Whether this is a good idea is … debatable.

Related

Equal and not equal operators not working in bash script

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

Shell Script compare the values with input parameter

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

The `continue` inside `if condition` is not going to next lines of the script

I have a bash script with an if condition and continuous lines of script. However, after if condition nothing in the script seems to run. For example, here are the lines of script (The lines after continue in if condition are not reacting).
dir=/usr/path/tofiles
for file in $(find $dir -name \*txt)
do
fbase={file%.txt}
if [ "$step" == "1" -a ! -f "${fbase}.header" ]
then
continue
fi
### From here nothing in the script runs #####
if [ -f "${fbase}merged" ]
then
echo "$file already merged"
fi
files="$files ${fbase}g.txt.gz"
cnt=$(($cnt + 1))
if [ "$cnt" -eq "$chunksize" ]
then
outid=$(uuidgen)
logfile=${outdir}/${outid}.cluster.log
echo "sh $(pwd)/mysecript.sh $outid $files"
fi
done
After the first if condition nothing in the script is running, I tried printing using echo nothing is showing up. Precisely, the issue is after the continue statement within the if condition. Any help/suggestions are much appreciated.
Thanking you
You seem to have a wrong interpretation of the continue statements.
The continue statement skips the lines below it and starts with the next iteration.
while (true) {
print(1);
if (true) {
continue;
}
print(2);
}
In the above print(2) will never get executed as it skips it everytime and starts with the next iteration.
For deeper insight please read Nested-If statements in Shell-scripting
For your scenario please try this
dir=/usr/path/tofiles
for file in $(find $dir -name \*txt)
do
fbase={file%.txt}
if ! [ "$step" == "1" -a ! -f "${fbase}.header" ]
then
if [ -f "${fbase}merged" ]
then
echo "$file already merged"
fi
files="$files ${fbase}g.txt.gz"
cnt=$(($cnt + 1))
if [ "$cnt" -eq "$chunksize" ]
then
outid=$(uuidgen)
logfile=${outdir}/${outid}.cluster.log
echo "sh $(pwd)/mysecript.sh $outid $files"
fi
fi
done
The problem with your script is, that in case your first if statement evaluates always to true you'll always skip the rest of the loop, due to the continue, so nothing else will be executed. This behavior is the same as in all other programming languages.
continue, like break, is a keyword to control the loop behavior. This means that using continueit is possible to skip the rest of the current loop iteration. And using break it is possible to exit the loop.
In case you need to go further to the next if statement, you need to nest your ifs, so that both are checked/evaluated.
More background information regarding this issue can be found here.
According to your comments, your code should include nested ifs, like:
dir=/usr/path/tofiles
for file in $(find $dir -name \*txt)
do
fbase={file%.txt}
if [ "$step" == "1" -a ! -f "${fbase}.header" ]
then
if [ -f "${fbase}merged" ]
then
echo "$file already merged"
fi
files="$files ${fbase}g.txt.gz"
cnt=$(($cnt + 1))
if [ "$cnt" -eq "$chunksize" ]
then
outid=$(uuidgen)
logfile=${outdir}/${outid}.cluster.log
echo "sh $(pwd)/mysecript.sh $outid $files"
fi
fi
done

If condition not work regular in bash

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.

How do I test in a shell script whether I'm running inside Scratchbox2

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

Resources