calling multiple functions from within if statement - cannot get it to work
by Grobe from LinuxQuestions.org on (#6M23E)
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
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