Find the answer to your Linux question:
Results 1 to 5 of 5
I could use some help! I've written a bash script that allows a user to input a directory location to find out the size of the directory. However, if the ...
  1. #1
    Just Joined! Scallywag's Avatar
    Join Date
    Jul 2009
    Location
    Black Mountain, NC
    Posts
    24

    User Input Bash Script

    I could use some help!

    I've written a bash script that allows a user to input a directory location to find out the size of the directory. However, if the user inputs a directory and finds its size then inputs another directory then wants to quit, the script asks the user numerous times if he wants to quit!! The script won't exit until the number of times the user looks at a directory is reached! What gives?

    Here's the script:
    Code:
    # size - find the size of any directory
    clear
    echo "This utility allows you to find the size of a directory."
    echo -e "\e[1;33m NOTE: the default location is /home/jim/\033[0m"
    echo "------------------------------------------------------"
    echo -ne "Enter a location and press [ENTER]: "
    read file
    if  [ "$file" != "" ]; then
    	clear	
    	echo
    	echo -ne "The size of this directory is: "
    	du -sh $file;
    while true; do
        echo
        read -p "Do you want to enter another location? (y/n) " yn
        case $yn in
            [Yy]* ) clear; size;;
            [Nn]* ) exit;;
            * ) echo "Please answer yes or no.";;
        esac
    done
    elif  [ "$file" == "" ]; then
      clear
      while true; do
        echo "You didn't enter a location."
        read -p "Would you like to quit? (y/n) " yn
        echo
        case $yn in
            [Yy]* ) exit;;
            [Nn]* ) clear; size;;
            * ) echo "Please answer yes or no.";;
        esac
    done
    fi
    exit 0
    Any help would be appreciated...

  2. #2
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    Code:
    THIS="$0"
    
    ...
    ...
    
    #CHANGE 
    [Yy]* ) clear; size;;
    #BY
    [Yy]* ) clear; $THIS; exit 0;;
    Your script name size conflicts with /usr/bin/size. And the script must exit from the running instance if the script calls itself recursively.

    Also, that's your choice, but I would not have asked for new user input in the script succeeds after tne first request. And if the user does not input a dir, I would simply exit. The developper is not the user's nanny.
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    So to be a bit more specific:

    When you call "size" in your if statement that nmset pointed out, you are actually launching a new version of your script. So if you check the size of three directories, there are now three copies of your script running. The first request to exit exits the third one, then the second, then the first. If you have your script output the variable $$ (the PID) at some point, you will see the different instances of the script.

    The simplest way around this would be to turn your program into a loop, not a recursion. You could abstract your checking code out into a function, and call that function for each requested directory, then exit the whole script once.
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    Cabhan is quite right, a function and a loop are more elegant and effective to use than recursion.
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  5. #5
    Just Joined! Scallywag's Avatar
    Join Date
    Jul 2009
    Location
    Black Mountain, NC
    Posts
    24
    Thanks. i appreciate the help. I'm going to recode it with a loop...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...