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)
exitfrom a function unless you want the script that's running it to exit. Usereturninstead. see Difference between return and exit in Bash functions – cas Jul 07 '21 at 07:32binaryis any binary executable. I replaced it to make it generic. I'm actually testing fordialogI 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:46dialogInstallSuccessis never set becausedialogBinis still empty and you end up in theelseblock. – Freddy Jul 07 '21 at 22:53dialogBinis 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 $dialogBiincan't be nested with the-n $dialogBintest. Removing the nesting creates the desired output – eyoung100 Jul 08 '21 at 04:44command -vinto a variable.commandreturns an exit code of 0 on success (i.e.dialogis 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 updateandapt install dialogif dialog is not already installed, and be done with it. Or justapt updateandapt install dialogwithout 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