尝试在shell脚本中调用函数时找不到命令

时间:2017-01-10 20:07:32

标签: bash shell scripting

看来我需要一些shell脚本的帮助。

背景:首先,我是网络管理员,并且通常首先尝试远离编码。不幸的是,此刻我的手被迫,并提出了一些代码来完成工作。

根本问题是我似乎无法在shell脚本中调用已定义的函数。我尝试过不同的解决方案,包括命令替换,列出脚本之外的函数(不是最佳解决方案),甚至更改函数内部变量中使用的引号类型,但问题仍然存在。我一直得到"命令未找到"当脚本尝试调用函数时:

./massmenu.sh: line 288: funk_passcheck: command not found

这是我的一些代码:

#!/bin/bash

# DEFINING FUNCTIONS
#------------------------------------------------------------------------------------BEGIN PASSWORD CHECK FUNCTION:
function funk_passcheck () 
{
echo "Calling password checking module..."
unset newpasswd
prompt="Please enter the NEW password you would like to configure:  "
while IFS= read -p "$prompt" -r -s -n 1 char
do
    if [[ $char == $"\0" ]];
    then
        break
    fi

    if [[ $char == $"\177" ]];
    then
        prompt=$"\b \b"
        newpasswd="${newpasswd%?}"
    else
        prompt="*"
        newpasswd+="$char"
    fi
done

echo -ne "\n"

unset chkpasswd
prompt="Please re-enter the password:  "
while IFS= read -p "$prompt" -r -s -n 1 char
do
    if [[ $char == $"\0" ]];
    then
        break
    fi

    if [[ $char == $"\177" ]];
    then
        prompt=$"\b \b"
        chkpasswd="${chkpasswd%?}"
    else
        prompt="*"
        chkpasswd+="$char"
    fi
done

echo -ne "\n"
        if [ "$newpasswd" != "$chkpasswd" ];
            then
                        echo $"\n \n"
                        echo "Passwords for new configuration did not match!"
                        echo $"\n \n"
                        exit

        elif [ "$newpasswd" == "$chkpasswd" ];
            then
                        echo $"\n \n"
                        echo "Passwords match.  Moving on..."
    fi
return $newpasswd   
}
#------------------------------------------------------------------------------------END PASSWORD CHECK FUNCTION
#------------------------------------------------------------------------------------BEGIN PASSWORD ENTRY FUNCTION
function funk_passwd () {
echo "Calling password entry module..."
while IFS= read -p "$prompt" -r -s -n 1 char
do
    if [[ $char == $"\0" ]];     then
        break
    fi

    if [[ $char == $"\177" ]];  then
        prompt=$"\b \b"
        passwd="${passwd%?}"
    else
        prompt="*"
        passwd+="$char"
    fi
done;
return $passwd
}
#------------------------------------------------------------------------------------END PASSWORD ENTRY FUNCTION
#------------------------------------------------------------------------------------BEGIN ENABLE PASSWORD ENTRY MODULE
function funk_en_passwd () {
echo "Calling ENABLE password entry module....."
echo "Clearing function parameters....."
unset enpasswd
prompt="Please enter the ENABLE Password:  "
while IFS= read -p "$prompt" -r -s -n 1 char
do
    if [[ $char == $"\0" ]];     then
        break
    fi

    if [[ $char == $"\177" ]];  then
        prompt=$"\b \b"
        enpasswd="${enpasswd%?}"
    else
        prompt="*"
        enpasswd+="$char"
    fi
done;
return $enpasswd
}
#------------------------------------------------------------------------------------END ENABLE PASSWORD ENTRY MODULE

selection=
until [ "$selection" = "0" ]; do
        echo "******************************************************************"
        echo "*              NOC MASS CONFIGURATION UTILITY                    *"
        echo "******************************************************************"
        echo ""
        echo "******************************************************************"
        echo "* THIS SHELL SCRIPT IS DESIGNED TO PERFORM MASS CONFIGURATION    *"
        echo "* FOR ROUTED AND SWITCHED DEVICES ON THE MDU BROADBAND           *"
        echo "* NETWORK.  PLEASE MAKE SURE THAT THE SEED FILES ARE POPULATED   *"
        echo "* WITH THE IPv4 ADDRESSES OF THE HOSTS TO BE CONFIGURED PRIOR    *"
        echo "* TO LAUNCHING THIS UTILITY.                                     *"
        echo "******************************************************************"
        echo ""
        echo "MAIN PROGRAM MENU"
        echo "1 - MANAGE USER CREDENTIAL SETS" #------------------------MODULE 1
        echo "2 - MANAGE SNMP CREDENTIAL SETS" #------------------------MODULE 2
        echo "3 - MAKE MASS CONFIGURATION CHANGES:  CISCO" #------------MODULE 3
        echo "4 - MAKE MASS CONFIGURATION CHANGES:  ALCATEL" #----------MODULE 4
        echo "5 - BACKUP CONFIGURATIONS VIA FTP:  ALCATEL" #------------MODULE 5
        echo ""
        echo "0 - exit program"
        echo ""
        echo -n "Enter selection: "
        read selection
        echo ""
        case $selection in
#-----------------------------------------------------------------------MODULE 1
        1 ) echo "USER CREDENTIAL SET MANAGEMENT"
                selection=
                until [ "$selection" = "0" ]; do
                echo ""
                echo "MANAGING USER CREDENTIAL SETS"
                echo "1 - MANAGE CREDENTIALS:  CISCO"
                echo "2 - MANAGE READ/WRITE CREDENTIALS:  ALCATEL"
                echo "3 - MANAGE READ-ONLY CREDENTIALS: ALCATEL"
                echo "4 - CHANGE ADMIN ACCOUNT PASSWORD: ALCATEL"
                echo ""
                echo "0 - exit program"
                echo ""
                echo -n "Enter selection: "
                read selection
                echo ""
                        case $selection in
                        1 )     echo "USER CREDENTIALS FOR CISCO DEVICES"
                                echo "**********************************************"
                                echo "* ACCOUNT INFORMATION FOR THE TELNET SESSION *"
                                echo "**********************************************"
                                echo ""
                                echo -ne "Please enter the username for the TELNET session:  "
                                read -e loginid
                                echo -ne '\n'
                                unset passwd
                                prompt="Please enter the TELNET Password:  "
                                unset -f funk_passwd
                                passwd=$( funk_passwd )
                                echo ""
                                echo ""
                                unset -f funk_en_passwd
                                enpasswd=$( funk_en_passwd )
                                echo ""
                                echo ""
                                echo ""
                                echo "***********************************************"
                                echo "*       >>>>>>>>>>>IMPORTANT<<<<<<<<<<<<      *"
                                echo "* ACCOUNT INFORMATION FOR THE NEW OR EXISTING *"
                                echo "* USER ACCOUNT.  PASSWORDS MUST BE AT LEAST 8 *"
                                echo "* CHARACTERS IN LENGTH, AND CANNOT MATCH ANY  *"
                                echo "* OF THE PREVIOUS FOUR (4) PASSWORDS.         *"
                                echo "*                                             *"
                                echo "*       CONFIGURING READ/WRITE ACCESS         *"
                                echo "***********************************************"
                                echo ""

                                echo -ne "Please enter the username that is to be configured:  "
                                read -e newuid
                                echo -ne '\n'
                                unset -f funk_passcheck
                                newpasswd=$( funk_passcheck )
                                for device in `cat lists/cisco-list.txt`; do
                                ./expect/cisco-rw.exp $device $loginid $passwd $enpasswd $newuid $newpasswd;
                                done
                                echo ""
                                echo "**********************************"
                                echo "*  SYSTEM CONFIGURATION COMPLETE *"
                                echo "**********************************"
                                echo ""
                                exit ;;

                        2 ) echo "*******************************************"
                                echo "* ACCOUNT INFORMATION FOR THE SSH SESSION *"
                                echo "*******************************************"
                                echo ""
                                echo -ne "Please enter the username for the SSH session:  "
                                read -e loginid
                                echo -ne '\n'
                                unset passwd
                                prompt="Please enter the SSH session Password:  "
                                unset -f funk_passwd
                                passwd=$( funk_passwd )
                                echo ""
                                echo ""
                                echo ""

                                echo "***********************************************"
                                echo "*       >>>>>>>>>>>IMPORTANT<<<<<<<<<<<<      *"
                                echo "* ACCOUNT INFORMATION FOR THE NEW OR EXISTING *"
                                echo "* USER ACCOUNT.  PASSWORDS MUST BE AT LEAST 8 *"
                                echo "* CHARACTERS IN LENGTH, AND CANNOT MATCH ANY  *"
                                echo "* OF THE PREVIOUS FOUR (4) PASSWORDS.         *"
                                echo "*                                             *"
                                echo "*       CONFIGURING READ/WRITE ACCESS         *"
                                echo "***********************************************"
                                echo ""
                                echo -ne "Please enter the username that is to be configured:"
                                read -e newuid
                                echo -ne '\n'
                                unset -f funk_passcheck
                                newpasswd=$( funk_passcheck )
                                for device in `cat lists/ale-list.txt`; do
                                ./expect/ale-rw.exp $device $loginid $passwd $newpasswd $newuid;
                                done

                                echo ""
                                echo "**********************************"
                                echo "*  SYSTEM CONFIGURATION COMPLETE *"
                                echo "**********************************"
                                echo ""
                                exit ;;

                        3 ) echo "*******************************************"
                                echo "* ACCOUNT INFORMATION FOR THE SSH SESSION *"
                                echo "*******************************************"
                                echo ""
                                echo -ne "Please enter the username for the SSH session:  "
                                read -e loginid
                                echo -ne '\n'
                                unset passwd
                                prompt="Please enter the SSH session Password:  "
                                unset -f funk_passwd
                                passwd=$( funk_passwd )
                                echo -ne '\n'
                                echo ""
                                echo ""
                                echo ""
                                echo "***********************************************"
                                echo "*       >>>>>>>>>>>IMPORTANT<<<<<<<<<<<<      *"
                                echo "* ACCOUNT INFORMATION FOR THE NEW OR EXISTING *"
                                echo "* USER ACCOUNT.  PASSWORDS MUST BE AT LEAST 8 *"
                                echo "* CHARACTERS IN LENGTH, AND CANNOT MATCH ANY  *"
                                echo "* OF THE PREVIOUS FOUR (4) PASSWORDS.         *"
                                echo "*                                             *"
                                echo "*        CONFIGURING READ-ONLY ACCESS         *"
                                echo "***********************************************"
                                echo ""
                                echo -ne "Please enter the username that is to be configured:"
                                read -e newuid
                                echo -ne '\n'
                                unset -f funk_passcheck
                                newpasswd=$( funk_passcheck )
                                for device in `cat lists/ale-list.txt`; do
                                ./expect/ale-ro.exp $device $loginid $passwd $newpasswd $newuid;
                                done
                            echo ""
                            echo "**********************************"
                            echo "*  SYSTEM CONFIGURATION COMPLETE *"
                            echo "**********************************"
                            echo ""
                            exit ;;

1 个答案:

答案 0 :(得分:0)

答案结果是答案部分提到的两件事情的组合,两件都经过了尝试,但没有在一起。删除未设置-f并格式化函数以正确返回字符串,而不是错误地尝试将返回输出分配给变量。赛勒斯和乔罗巴,非常感谢你。