Odd .sh behavior in script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Odd .sh behavior in script
# 1  
Old 02-10-2008
Question Odd .sh behavior in script

Hello, I have been working on a what I thought was a fairly simple script for installing a software kit on Linux and Unix
I am not new to scripting but am far from being fluent in sh scripting.
any assistance would be appreciated.

I have an odd bug occuring when executing the script.

When executing the script in Interactive mode
A series of questions are asked. then verified.

If you run the script and do not choose to use the default directory for install.
for example:
the question asked is
Where would you like to install the software? The default is : [ /tmp/username ]" type /bin and press enter here
then when asked if you are sure you want to use /bin select N for NO
then change it to /var or any other dir.

then proceed by selecting Y

you will then be told that you have http alreayd running...( this is hardcoded with no checks yet. It is a concept)

choose Y or N
the script should proceed.


Where I am seeing a problem is I get asked the question about http Twice ....Only when I change the default directory twice or more

any idea how to prevent this ?
better yet Can you explain Why it is doing this so I can prevent this from happening with other scripts ?



The code below does not install anything ...it simply generates a log file at this time.

Thanks again for any assistance with this bug.
RobertMCol
Code:
#!/bin/sh
#-----------------------------------------------------------------#
#             FUNCTIONS USED BY THIS SCRIPT                       #
#-----------------------------------------------------------------#
usage()
{
        echo "Usage: $0 [ Interactive ] [ AutoInstall ] "
        echo "  AutoInstall is the Prefered method for Installation "
        echo
        echo "--------------------------------------------------------------------"
        echo "Interactive Mode:"
        echo "  You will be asked a series of questions regarding Installation configuration"
        echo "          You can press enter to these questions. Default settings will be used. "
        echo "          If you change a default. you will be asked to confirm the changes [ Y / N ]"
        echo "          You must choose Y [ for Yes ] or N [ for No ] in order to proceed "
        echo
        echo
        echo "AutoInstall Mode:         *** Prefered Method  ***"
        echo "  This method will install the Software with all defaulted answers"
        echo "  This is the Preferred method for Installation."
        echo " "
        echo "Would you like to view the readme file for help? [ Y / N ]"
        read ans
        Answer=${ans}
        check_ans ${Question} ${Answer}
                ans1=`echo $ans |tr '[a-z]' '[A-Z]'`
                if [ ${ans1} = "Y" ]
                then
                more readme.txt
                clear
        echo "Usage: $0 [ Interactive ] [ AutoInstall ] "
        echo "  AutoInstall is the Prefered method for Installation "
                fi
        echo
        exit 1
}
#-----------------------------------------------------------------#
check_ans()
{
        echo $Question
        echo $Answer
MyQuestion=$Question
MyAnswer=$2
        if [[ ! -n $Answer ]]
        then
                echo "${Question}"
                echo "Please Choose [ Y or N  ]"
                return 0
        fi
        echo $Answer >> $LOGOUT
        return 1
}
#-----------------------------------------------------------------#
directory_build()
{
# Ensure the installation root directory exists.
if [ ! -d ${INSTALL_ROOT} ]
then
    mkdir -p ${INSTALL_ROOT}
    if [[ $? -ne 0 ]]
    then
        echo "Error creating ${INSTALL_ROOT}; installation failed."  | tee -a ${LOGOUT}
        exit 1
    fi
fi

}

#-----------------------------------------------------------------#
check_kit()
{
    # Make sure we have only one webserver kit in this directory
    WEBSERVER_TAR_FILE_COUNT=`ls $KIT_SEARCH_NAME | wc -l`
    #echo tar file count is : $WEBSERVER_TAR_FILE_COUNT

    if [ $WEBSERVER_TAR_FILE_COUNT -eq 0 ]
    then
        echo "No kit found. Nothing to do."  |$ tee -a ${LOGOUT}
        exit 1
    fi

    if [ $WEBSERVER_TAR_FILE_COUNT -gt 1 ]
    then
        echo "Multiple kits found. Remove extra files from this directory and re-run."  | tee -a ${LOGOUT}
        ls -l $KIT_SEARCH_NAME
        exit 1
    fi

    # Set the name of the webserver kit.
    WEBSERVER_TAR_FILE=`ls $KIT_SEARCH_NAME | head -1`
        echo "${WEBSERVER_TAR_FILE} Will be used to install"  | tee -a ${LOGOUT}
}

process_defaults()
{
        echo "Processing Default configuration"  | tee -a ${LOGOUT}
}
#-----------------------------------------------------------------#
process_interactive()
{
        INSTALL_ROOT=/tmp/username
        echo "Where would you like to install the software? The default is : [ /tmp/username ]"  | tee -a ${LOGOUT}
        read line
        if [[ ! -n $line ]]
        then
                INSTALL_ROOT=/home/users/sbin
        else
                if [ ${line} != $INSTALL_ROOT ]
                then
                        INSTALL_ROOT=$line
                echo "You chose to install in the following PATH [ $line ]. Are you sure ? [Y / N] "  |tee -a ${LOGOUT}
                Question="Custom INSTALL_ROOT [ Y or N ]"
                while read ans
                        do
                        Answer=`echo $ans |tr '[a-z]' '[A-Z]'`
                        check_ans ${Question} ${Answer}
                                if [[ $? != 0 ]]
                                then
                                        break
                                fi
                        done

                        if [ ${Answer} != "Y" ]
                        then
                                process_interactive
                        fi
                fi
        fi
        echo "INSTALLATION PATH will be [ ${INSTALL_ROOT} ]"  | tee -a ${LOGOUT}
#######
        echo ""
        echo "I notice you have an httpd server already running"
        echo "" |tee -a ${LOGOUT}
        echo "Would you like to use your existing httpd server or install Shttpd for our product?" |tee -a ${LOGOUT}
        echo "Use existing httpd? [ Y or N ]" |tee -a ${LOGOUT}
        Question="Use existing httpd? [ Y or N ]"
        while read ans
        do
        Answer=`echo $ans |tr '[a-z]' '[A-Z]'`
        check_ans ${Question} ${Answer}
        if [[ $? != 0 ]]
        then
                break
        fi
        done
                if [ ${Answer} != "Y" ]
                then
                echo "Installing Shttpd for  web client"  | tee -a ${LOGOUT}
        echo ""  | tee -a ${LOGOUT}
                else
                echo "EXISTING WEB SERVER will be used"  | tee -a ${LOGOUT}
        echo ""  | tee -a ${LOGOUT}
                fi

        check_kit

}
############# END OF FUNCTIONS

#-----------------------------------------------------------------#
# BEGIN MAIN PROCESSING OF SCRIPT
#-----------------------------------------------------------------#
if [ $# -eq 0 ]
then
        usage
else

# DEFINE GLOBAL VARIABLES HERE
KIT_SEARCH_NAME=test.file
INSTALL_ROOT=/tmp/usersname
LOGPATH=/tmp
LOGFILE=Installation.log
LOGOUT=${LOGPATH}/${LOGFILE}

if [ -f ${LOGOUT} ]
then
        echo "Logfile already exists. I Will rename to a timestamp.log"
        stat_time=`stat -c "%x" ${LOGOUT} |awk -F . '{print $1}'`
        logstamp=`echo ${stat_time}|awk '{print $1"_"$2}'`
        mv  ${LOGOUT} ${LOGPATH}/${logstamp}.${LOGFILE}
        echo " ${LOGFILE} was renamed to ${LOGPATH}/${logstamp}.${LOGFILE}"
        echo "----------------------------------------------------------------------"
        #create new logoutfile now
        touch ${LOGOUT}
        echo "Created new Logfile ......continuing installation..."
        echo ""
        echo "Installation Process" >> ${LOGOUT}
        echo "`date +%Y-%m-%d-%H:%M:%S` Begin Installation" >> ${LOGOUT}
        echo ""

else
        touch ${LOGOUT}
        echo "Installation Process" >> ${LOGOUT}
        echo "`date` Begin Installation" >> ${LOGOUT}
        echo ""
fi

        if [ "$1" != "Interactive" ]
        then
                if [ "$1" != "AutoInstall" ]
                then
                        echo "Unknown Option"
                        usage
                fi

          INTERACTIVE=0
          echo "NEED TO CONFIGURE DEFAULTS" | tee -a ${LOGOUT}
          echo "Processing AutoInstall Mode of Installaion" | tee -a ${LOGOUT}
          echo ""

        else
          echo "Processing Interactive Installaion"  | tee -a ${LOGOUT}
          echo ""
          INTERACTIVE=1
        fi
fi

#-----------------------------------------------------------------#
# Find out what OS is running
this_os=`uname -s`

case $this_os in
    AIX )
        ECHO_PREFIX=
        ECHO_SUFFIX=\\c
    ;;
    * )
        ECHO_PREFIX=-n
        ECHO_SUFFIX=
    ;;
esac

echo "Operating system is ${this_os}" | tee -a ${LOGOUT}
echo "" |tee -a ${LOGOUT}
#-----------------------------------------------------------------#

#-----------------------------------------------------------------#
# Make sure root user is the one executing this script
if [ "${LOGNAME}" != "root" ]
then
    echo "Must be root to run $0. Current user is ${LOGNAME}"  | tee -a ${LOGOUT}
    exit 1
fi
#-----------------------------------------------------------------#
if [ $INTERACTIVE = "0" ]
then
        process_defaults
else
        process_interactive
fi

# 2  
Old 02-10-2008
Script problem

Yeah, you've called "process_interactive" recursively. When you get the "Are you sure prompt" and you enter "No", it calls process_interactive() again and eventually gets to the HTTP prompt for the first time. When that call returns/finishes, it continues at the prior execution point, which is to continue with the
Code:
while read; do

loop. When it finishes with that, it goes to the HTTP prompt for the second time. You can fix this with a double loop.
Code:
while true; do
   echo -n "Prompt for path: ";
   read path
   while true; do 
      echo -n "Are you sure? Y/N";
      read yesno
      if [ x$yesno == xY ]; then
        break 2;
      elif [ x$yesno == xN ]; then 
        continue 2;
      else
        continue;
      fi
  done
done

To see what I mean about the recursion, throw in a debugging statement at the top of the function call, like this:

Code:
echo >&2 "Entering function process_interactive"

If you are using ksh or bash, you can also turn on debugging. At the top of the script (or somewhere), do:
Code:
set -x

And you will get a line of output for each expression evaluated by the shell during the processing of the script.
# 3  
Old 02-10-2008
Resolved.... Thank you

otheus,

Thank you so much, this was driving me nuts wondering why it was occuring.

I was not sure how to go about the double loop when I coded this ,at the time all I knew was that if the parameter needed changing I had to call the function again.

The Double loop works nicely. thank you

so Bottom line here is I should not call Function (A) while in Function A.

I am off to fix my Slop .......

Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Cybersecurity

Odd behavior from passwd.

Hi Folks, Before I go off and start checking I'm just wondering if anyone has seen this behaviour before. # passwd e825390 Changing password for user e825390. New password: Retype new password: Retype new password: passwd: all authentication tokens updated successfully. As you can... (2 Replies)
Discussion started by: gull04
2 Replies

2. Programming

Odd behavior from GDB while trying to cross-debug an embedded Linux application.

Some background: The application normally runs on an embedded platform. Currently, for development purposes, I have the rootfs located @ /exports and the target is communicating over NFS. That way I can make a change on my local system, save the application @ /exports, and run the altered... (4 Replies)
Discussion started by: Circuits
4 Replies

3. What is on Your Mind?

Odd(?) shell script practise

Hello, I often stumble over a common shell coding practise. Example 1: #!/bin/sh # # Licensed Materials - Property of IBM # Rational ClearCase # (C) Copyright IBM Corp. 1999, 2010. All Rights Reserved # US Government Users Restricted Rights - # Use, duplication or disclosure restricted... (4 Replies)
Discussion started by: MadeInGermany
4 Replies

4. Shell Programming and Scripting

Odd results when my script runs from cron..

Hi folks, So I wrote a script to run "top", "awk" out values fro the "top" and send the results to a data file. I then set it to run in cron every 15 minutes. Now I'm noticing that the script, and it's sub-commands are not always cleanly finishing and, in my investigations, I am also... (11 Replies)
Discussion started by: Marc G
11 Replies

5. Shell Programming and Scripting

Calling a Perl script in a Bash script -Odd Situation

I am creating a startup script for an application. This application's startup script is in bash. It will also need to call a perl script (which I will not be able to modify) for the application environment prior to calling the application. The problem is that this perl script creates a new shell... (5 Replies)
Discussion started by: leepet01
5 Replies

6. Shell Programming and Scripting

something odd with my awk script

The code I am using #!/bin/sh for FILE in *.cfg; do awk '{ print; if ($1 == "host_name") store_name = $2; if ($1 == "register") { printf("\t\t parents\t\t\t %s-ilo\n", store_name); } }' "$FILE" > ../new-files/hosts/$FILE sed -i -e "s/notification_options.*/notification_options... (0 Replies)
Discussion started by: jag7720
0 Replies

7. Shell Programming and Scripting

"Odd" behavior exiting shell script

Is it normal behavior for a shell script that terminates to terminate its parent shell when executed with the "." option? For example, if I have the example script (we'll name it ex.sh): #!/bin/sh if then echo "Bye." exit 2 fi And I execute it like this: >./ex.sh It... (6 Replies)
Discussion started by: DreamWarrior
6 Replies

8. HP-UX

Odd storage behavior

Hi, We have some troubles with our HP server (rx4640) running HP-UX 11.31. The server is attached to a JBod cabinet. If the JBod cabinet is powered on and we power on the server after then HP-UX can't find the devices (disks) at the cabinet. Does not help to run an ioscan -fnC disk. But if I power... (3 Replies)
Discussion started by: hoff
3 Replies

9. UNIX for Dummies Questions & Answers

even odd script

I need a unix script that check for even or odd. EXAMPLE:::: please enter the number to check: 12 the output: This is an even number it has to have prompts. (2 Replies)
Discussion started by: snyper2k2
2 Replies
Login or Register to Ask a Question