LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell Programming - Delete user input (https://www.linuxquestions.org/questions/programming-9/shell-programming-delete-user-input-857821/)

cppnb 01-21-2011 11:42 AM

Shell Programming - Delete user input
 
HI!
I recently started shell programming and my task now is to do a menu display.Currently i am stuck whereby user will input both title and author and it will delete it.

function remove book
{
echo "Enter Title"
read title
echo "Enter Author"
read author
}
do i have to use sed command?

MensaWater 01-21-2011 12:49 PM

Presumably this is Bash?

The following would work in Bash, ksh and posix sh (but not csh or its variants):

Code:

function remove_book()
{
echo "Enter Title"
read title
echo "Enter Author"
read author
echo Title is: $title by Author: $author
}
remove_book

Some notes:
First you had a space in your function name "remove book" - I changed that to underscore "remove_book". Setting up commands with spaces in them is begging for trouble.
You didn't have the () after the function name and it is required.
You didn't have anything calling the function. You must first define it then call it to use it so the final "remove_book" line is what is doing that in my script.

You can add verbiage within the function to do the deletion of the book but need to account for how you're going to do that. For example when I ran a test:

Enter Title
Moby Dick
Enter Author
Herman Melville
Title is: Moby Dick by Author: Herman Melville

Notice that both the title and the author include spaces. You might need to do special handling for that in your removal (e.g. say you had a book by Herman Wouk - you'd want to make sure your deletion routine was just looking for Herman.)

Snark1994 01-22-2011 05:05 AM

Quote:

Originally Posted by cppnb (Post 4233209)
My task now is to do a menu display

What exactly did you mean by that? Do you mean show the user a prompt so that they can type commands which your programme will execute? If so, it would just be something like:

Code:

#!/bin/bash

function remove_book() {
echo "Enter Title"
read title
echo "Enter Author"
read author
echo Title is: $title by Author: $author
}
carryOn=true
while $carryOn
do
        echo -ne ">"
        read command
        case $command in
                "removebook" )
                        remove_book ;;
                "quit" )
                        carryOn=false ;;
                * )
                        echo "Type 'quit' or 'removebook'"
        esac
done


cppnb 01-23-2011 10:44 AM

yea i doing bash.mine is to create a menustyle program whereby once the user select delete,the screen will prompt which author to delete..

@snark1994,
pardon me but is it alright to explain the codes from line carryOn onwards,i dun seem to understand:)

Snark1994 01-24-2011 03:06 PM

No problem :)

The basic structure is that we're going to go into a loop - ask the user for some input, then act on the input, and then go back to asking for input. However, we want to be able to break out of this loop; so we have a variable "carryOn" which is set to true, and when we want to quit we will change it to false. So we have:
Code:

carryOn=true
while $carryOn
do
done

This will keep going on forever. The next thing we want to add is asking the user for some input. We first print a prompt, ">", which tells the user that we're expecting them to type something, then we use the command read to read input into a variable. So now we have:
Code:

carryOn=true
while $carryOn
do
        echo -ne ">"
        read command
done

This will also keep on looping. The last thing we want to add is the ability to actually act on the command which the user types. This is done using a "case" statement. You give case a variable, then you tell it different values of the variable that you want to do things for. So we get the last bit of the code:
Code:

carryOn=true
while $carryOn
do
        echo -ne ">"
        read command
        case $command in                  # Using the variable "command"...
                "removebook" )            # ...if command == "removebook" then...
                        remove_book ;;    # ...run the function we wrote previously, called remove_book.
                "quit" )                  # However, if command == "quit" then...
                        carryOn=false ;;  # ...set carryOn to be false, so the loop will finish and the programme will terminate
                * )                        # If they type anything else, then tell them the commands that the programme accepts:
                        echo "Type 'quit' or 'removebook'"
        esac
done

Hope this helps clear things up :) the only complicated bit is really the 'case' command. This link explains it in a bit more depth


All times are GMT -5. The time now is 04:11 PM.