如果我有3个sperate文件,每个文件都有相同的功能:
# switch1.sh
switch()
{
echo "SWITCH 1"
}
# switch2.sh
switch()
{
echo "SWITCH 2"
}
# switch3.sh
switch()
{
echo "SWITCH 3"
}
有没有办法指定调用哪个函数而不使函数名称唯一?
答案 0 :(得分:0)
@Barmar是对的,“当你获取每个文件时,它的功能取代了前一个文件。”
对于任何人,我对这个问题的目的感兴趣,我的目标是在这个bash脚本中获得某种面向对象的理智:
R_X11.so
switch()
{
echo "switch1"
}
check_administratorpassword()
{
file="switch1.sh"
description="Administrator password required to change system-wide preferences"
switch=$(switch)
if [ -n "$(security authorizationdb read system.preferences 2> /dev/null | grep -A1 shared | grep -E '(true|false)' | grep 'false')" ]; then
return 0
else
return 1
fi
}
switch()
{
echo "switch2"
if which brew > /dev/null; then
read -p "Check for Homebrew updates? (y/n): " input
if [[ $input == "Y" || $input == "y" ]]; then
echo "Checking for Homebrew updates..."
brew update
elif [[ $input == "N" || $input == "n" ]]; then
:
else
echo "Not a recognized input."
switch_homebrewinstall
fi
else
echo "Homebrew is not installed."
read -p "Install Homebrew? (y/n): " input
if [[ $input == "Y" || $input == "y" ]]; then
# Install Homebrew, TODO: Find secure way to download homebrew
echo -e "Installing Homebrew..."
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
elif [[ $input == "N" || $input == "n" ]]; then
:
else
echo "Not a recognized input."
switch_homebrewinstall
fi
fi
unset input
}
check_homebrewinstall()
{
file="switch2.sh"
description="Homebrew is installed."
switch=$(switch)
if which brew > /dev/null; then
return 0
else
return 1
fi
}
switch()
{
echo "switch3"
}
check_javainstall()
{
file="switch3.sh"
description="Java is installed."
switch=$(switch)
path=$(type -p java)
if [[ $path = /dev/null ]]; then
return 1
else
return 0
fi
unset path
}
source switch1.sh
source switch2.sh
source switch3.sh
declare -a checkArray
checkArray[1]=check_administratorpassword
checkArray[2]=check_homebrewinstall
checkArray[3]=check_javainstall
for i in "${!checkArray[@]}"; do
${checkArray[$i]}
if [ $? -eq 1 ]; then
echo -e "\xE2\x9C\x97 ${description}"
else
echo -e "\xE2\x9C\x93 ${description}"
fi
source "${file}"
switch
done
这个文件在切换函数被调用之前就被重新发送,给出了一种其OOP的时髦幻觉。我不建议这样做,因为没有人看着你的代码会假设这是发生了什么,但它有点酷。