LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-21-2011, 11:42 AM   #1
cppnb
LQ Newbie
 
Registered: Jan 2011
Posts: 2

Rep: Reputation: 0
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?
 
Old 01-21-2011, 12:49 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,986
Blog Entries: 14

Rep: Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187
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.)
 
Old 01-22-2011, 05:05 AM   #3
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Originally Posted by cppnb View Post
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
 
Old 01-23-2011, 10:44 AM   #4
cppnb
LQ Newbie
 
Registered: Jan 2011
Posts: 2

Original Poster
Rep: Reputation: 0
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
 
Old 01-24-2011, 03:06 PM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script to delete the user define input Hariharan Linux - Software 5 10-29-2010 02:05 AM
Shell script user input tuckermaddox Linux - Newbie 5 08-12-2004 03:14 AM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 08:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration