calling multiple functions from within if statement - cannot get it to work
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
calling multiple functions from within if statement - cannot get it to work
Hi.
I have this sample/training bash file that is supposed to check username and computer name.
There is something making this bash file not working correctly. But I'm not able to figure exact what that is.
I've put comments in the script to explain the problem(s)
Code:
#!/bin/bash
username=$(whoami)
thisComputerHostname=$(hostname)
function staticControlUsername () {
if [ $username != 'grobe' ]; then
echo 'WARNING: Pohibited from running, not logged in as correct user'
echo 'Your login name ' $username
return 0
else
echo 'INFO: User granted to run script (test 1)'
echo 'Your login name ' $username
return 1
fi
}
function staticControlMachineName () {
if [ $thisComputerHostname != 'resirkulert-Acer' ]; then
echo 'WARNING: Pohibited from running, the computer name are incorrect'
echo 'Current computer name:' $thisComputerHostname
return 0
else
echo 'INFO: User granted to run script (test 2)'
echo 'Name of current computer:' $thisComputerHostname
return 1
fi
}
# Those function call gives error when executed
#if [ staticControlUsername -a staticControlMachineName ]; then
#if [ staticControlUsername -eq 1 -a staticControlMachineName -eq 1 ]; then
#if [ [ staticControlUsername ] -a [ staticControlMachineName ] ]; then
# This returns 'Looks good' when executed, but doesn't print echo from within the functions, indicating
# that the functions isn't actually executed.'
if [ staticControlUsername ] && [ staticControlMachineName ] ; then
echo 'Looks good'
else
echo 'At least one test failed...'
fi
Also, using ShellCheck to identify issues is always a good idea.
Thanks, I did the shellcheck.net but this only gave corrections on quotes.
It seems to work better now, but it still doesn't work as intended:
Updated code (only line 38 is edited)
Code:
#!/bin/bash
username=$(whoami)
thisComputerHostname=$(hostname)
function staticControlUsername () {
if [ $username != 'grobe' ]; then
echo 'WARNING: Pohibited from running, not logged in as correct user'
echo 'Your login name ' $username
return 0
else
echo 'INFO: User granted to run script (test 1)'
echo 'Your login name ' $username
return 1
fi
}
function staticControlMachineName () {
if [ $thisComputerHostname != 'resirkulert-Acer' ]; then
echo 'WARNING: Pohibited from running, the computer name are incorrect'
echo 'Current computer name:' $thisComputerHostname
return 0
else
echo 'INFO: User granted to run script (test 2)'
echo 'Name of current computer:' $thisComputerHostname
return 1
fi
}
# Those function call gives error when executed
#if [ staticControlUsername -a staticControlMachineName ]; then
#if [ staticControlUsername -eq 1 -a staticControlMachineName -eq 1 ]; then
#if [ [ staticControlUsername ] -a [ staticControlMachineName ] ]; then
# This returns 'Looks good' when executed, but doesn't print echo from within the functions, indicating
# that the functions isn't actually executed.'
if staticControlUsername && staticControlMachineName ; then
echo 'Looks good'
else
echo 'At least one test failed...'
fi
On my end, both username and computer name is tested correct, but the script still runs wrong - details:
Line 12 and 13 executes normally
Line 41 wrongly execute - Line 39 is expected to execute but doesn't
Line 24 and 25 expected to execute but doesn't - neither does line 20/21.
The script seems to run as if the if the function staticControlUsername was returning 0, but it shouldn't.
The script seems to run as if the if the function staticControlUsername was returning 0, but it shouldn't.
Re-read the first piece of documentation I linked, paying careful attention to the terms "zero" and "non-zero", specifically:
Quote:
The test-commands list is executed, and if its return status is zero, the consequent-commands list is executed.
,..
If ‘else alternate-consequents’ is present, and the final command in the final if or elif clause has a non-zero exit status, then alternate-consequents is executed.
With exit statuses, zero means success, non-zero means failure.
Further, in the shell 0 is the success return code, 1 is a failure.
You can as well return the exit status of a true/false command.
Ah, THIS is where I was thinking wrong about the concept. Thank you very much for that info, after I inverted all the return codes in functions, the script behaved as expected.
So the misconception I have is coming from earlier (a life before Linux) I played around with Authohotkey script and the wbs script that was including in MS os. In those script languages, any call to a function, where the call is part of an if-statement, is normally treated as true for any number or non-zero strings. And this use for function calls therefore follows other rules.
#!/bin/bash
#I thought of sharing my thought.
#I always use full path
if [[ ! -f /usr/bin/whoami ]]
then
echo /usr/bin/whoami file not found
elif [[ ! -f /usr/bin/hostname ]]
then
echo /usr/bin/hostname file not found
else
#you can use export if using this variable using awk/gawk/gawk.exe
username=$(/usr/bin/whoami)
thisComputerHostname=$(/usr/bin/hostname)
#if some other included before this line other script could have defined alias. Hence using unalias and unset -f
unalias staticControlUsername >/dev/null 2>&1
unset -f staticControlUsername
function staticControlUsername()
{
if [[ "$username" != "grobe" ]]
then
echo "WARNING: Pohibited from running, not logged in as correct user"
echo "Your login name $username"
#return 1 fail result
return 1
else
echo "INFO: User granted to run script (test 1)"
echo "Your login name $username"
#return 0 pass result
return 0
fi
}
unalias staticControlMachineName >/dev/null 2>&1
unset -f staticControlMachineName
function staticControlMachineName()
{
#Better to use [[ .. ]] to have multiple conditional readable condition
if [[ "$thisComputerHostname" != "resirkulert-Acer" ]]
then
echo "WARNING: Pohibited from running, the computer name are incorrect"
echo "Current computer name: $thisComputerHostname"
return 0
else
echo "INFO: User granted to run script (test 2)"
echo "Name of current computer: $thisComputerHostname"
return 1
fi
}
# ========= OLD COMMENTS SHARED BY YOU =========
# Those function call gives error when executed
#if [[ staticControlUsername -a staticControlMachineName ]; then
#if [[ staticControlUsername -eq 1 -a staticControlMachineName -eq 1 ]; then
#if [[ staticControlUsername -a staticControlMachineName ]]; then
# This returns "Looks good" when executed, but doesn't print echo from within the functions, indicating
# that the functions isn't actually executed.
# ========= OLD COMMENTS SHARED BY YOU =========
if [[ 0 -eq $# ]]
then
staticControlUsername
else
staticControlUsername $@
fi
staticControlUsernameRet=$?
if [[ 0 -eq $staticControlUsernameRet ]]
then
if [[ 0 -eq $# ]]
then
staticControlMachineName
else
staticControlMachineName $@
fi
staticControlMachineNameRet=$?
if [[ 0 -eq $staticControlMachineNameRet ]]
then
echo "Looks good"
else
echo "staticControlMachineName test failed..."
fi
else
echo "staticControlUsername test failed..."
fi
fi
# COMMENTS
# Example at return values using cygwin bash.exe at Windows 11:
# $ /usr/bin/ls.exe /usr/bin/ls
# $ echo $?
# 0
# $ /usr/bin/ls.exe invalidfile.txt >/dev/null 2>&1
# $ echo $?
# 2
# $ /usr/bin/file.exe /cygdrive/c/Users/murugesan_openssl | /usr/bin/grep file
# $ echo $?
# 1
# $ /usr/bin/file /cygdrive/c/Users/murugesan_openssl | /usr/bin/grep.exe directory
# /cygdrive/c/Users/murugesan_openssl: directory
# $ echo $?
# 0
Last edited by murugesandins; 04-16-2024 at 12:25 PM.
Reason: Call 2nd function only if first function returning 0 (pass result).
Putting the function declaration within an if statement is new for me. I think I must try to wrap my mind around this now
I agree this is difficult for 1/2/3/few months during startup.
I am having printpassbook.sh shell script from 2003 till now.
Before modification I used to take a backup of that file
After modification I used to gzip that backup file.
printpassbook.sh used to display:
1) my post office transactions (incoming/outgoing +/- four days from today
2) available balance that I can withdraw excluding recurring deposit amount and MAB
3) MIS interest I am going to receive tomorrow and day after tomorrow
4) my total salary from MIS at post office for current month
5) waiting day/month/years at RD/MIS accounts.
6) displaying related colors based on date/waiting day/month/years.
Remember to take backup before modification for maintenance.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.