Consider the following function:
function testForBinary {
someBin=$(command -v binary)
# Test if binary is installed
if [[ -n $someBin ]]; then
installSuccess="Success"
echo ${installSuccess}
return
else
# This should never be reached
return 1
fi
}
taken from (Context):
function installAndTestForDialog() {
# dialog allows me to create a pretty installer
# It is a required dependency since XXXXX
# Name Removed as I'm attempting modularization
# of a script from Github that's one huge Script
See https://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then
local dialogBin
local updated=false
local dialogInstallSuccess
dialogBin=$(command -v dialog)
if [[ -z $dialogBin ]]; then
printf "Installing dialog... "
if [[ $updated -eq 0 ]]; then
apt-get update > /dev/null 2>&1
updated=true
fi
# Debian Based OS
apt-get -y install dialog > /dev/null 2>&1
# Test if dialog is installed to verify installation
# above was successful.
dialogBin=$(command -v dialog)
if [[ -n $dialogBin ]]; then
# I cannot get this to echo success
# if I echo from here
dialogInstallSuccess="Success"
# Moving it here doesn't help either Why??
echo ${dialogInstallSuccess}
return
else
# This should never be reached
return 1
fi
fi
}
I'm attempting to treat installSuccess
as a boolean, but what am I doing wrong. If I write the function as above, and then add:
isInstalled=$(testForBinary)
echo "$isInstalled"
isInstalled
returns a blank line. I know this isn't true because when I run command -v binary
outside the function, the path to binary
results.
Output (Context):
Function and Variable Output Test
=====================
# These are other tests
# I'm performing, but note
# the blank line, which should
# print Success
2.9-MODULAR
192.168.1.227
false
false
(blank line)
exit
from a function unless you want the script that's running it to exit. Usereturn
instead. see Difference between return and exit in Bash functions – cas Jul 07 '21 at 07:32binary
is any binary executable. I replaced it to make it generic. I'm actually testing fordialog
I can provide the output I get for context, along with the entire function – eyoung100 Jul 07 '21 at 21:25return
, the execution has already moved out of the function. – muru Jul 07 '21 at 22:46dialogInstallSuccess
is never set becausedialogBin
is still empty and you end up in theelse
block. – Freddy Jul 07 '21 at 22:53dialogBin
is never empty. I do see now that I need to test it the second time, which I have now fixed. The output in question is still blank. The solution is probably staring me in the face, but I can't see it – eyoung100 Jul 07 '21 at 23:21PATH=/home/username/bin:/home/username/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin:/usr/lib/llvm/11/bin:/usr/lib/llvm/12/bin
– eyoung100 Jul 08 '21 at 03:26-z $dialogBiin
can't be nested with the-n $dialogBin
test. Removing the nesting creates the desired output – eyoung100 Jul 08 '21 at 04:44command -v
into a variable.command
returns an exit code of 0 on success (i.e.dialog
is in your path or defined as a function or alias), non-zero on failure. So all you need isif command -v dialog; then ... ; else ... ; fi
– cas Jul 08 '21 at 04:51apt update
andapt install dialog
if dialog is not already installed, and be done with it. Or justapt update
andapt install dialog
without bothering to check if it's already installed - the worst that will happen is that a newer version of dialog (and its dependencies) might be installed. – cas Jul 08 '21 at 04:57