If-statement nested in case


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If-statement nested in case
# 1  
Old 08-05-2010
If-statement nested in case

I'm trying to write case statements with 'if statements' embedded inside of them. I'm using the korn shell but it's not functioning.

If I want to see if a string exists in a file and then perform an action, what would be the best way to do this?

For file "asg51fin" to delete a line if a user input is matched:
Code:
Smith:Joseph:joe:12365

User searches "smith" and "smith is deleted. If name is search that doesn't exist then "sorry, no match found" is displayed.

Here's the code I'm using:
Code:
echo  "Please make a decision: \c"
read choice

case "$choice" in
[!AaDdMmIiXxVv]) echo "invalid entry, please try again";sleep 2;;
[Aa]) echo "Please enter the last name"; read last;
 echo "Please enter the first name";read first;
 echo "Please enter a nickname"; read nick;
 echo "Please enter a phone number in the following format: xxx-xxx-xxxx";
 read phone;echo "$last:$first:$nick:$phone" >> asg51fin;;

[Dd]) echo "Please enter the last name of the entry you want to remove"
read delete
if [ 'egrep "$delete" asg51fin' ]
then set -- 'grep "^$delete" >> asg51fin' && echo "$delete deleted" && sleep 2
else echo "Sorry, no match found for $delete" && sleep 2
fi;;

#Thanks so much for reading

Last edited by dazeman27; 08-05-2010 at 09:58 PM.. Reason: Please use code tags
# 2  
Old 08-06-2010
An if block will work just fine in a case statement as long as you make sure you have it formatted correctly. I did a little revamping of your code
and it executes fine.

Code:
#!/bin/ksh
#set -x
read choice?"Please make a decision: "
 
case "$choice" in
[!AaDdMmIiXxVv]) echo "invalid entry, please try again"
                         sleep 2
                         ;;
                 [Aa]) read last?"Please enter the last name: "
                         read first?"Please enter the first name: "
                         read nick?"Please enter a nickname: "
                         read phone?"Please enter a phone number in the
                                          following format: xxx-xxx-xxxx: "
                         echo "$last:$first:$nick:$phone" >> asg51fin
                         ;;
                 [Dd]) read delete?"Please enter the last name of the entry
                                          you want to remove: "
                         if [ `grep "^$delete" asg51fin` ]; then
                             echo `grep "^$delete" asg51fin`
                             echo "$delete deleted"
                             sleep 2
                         else
                             echo "Sorry, no match found for $delete"
                             sleep 2
                         fi
                         ;;
esac

If I were you I would use the sed command to remove the line from the file since your grep is finding the whole string. Either that or use awk where you can use $ format to find specific fields.

Last edited by port43; 08-06-2010 at 03:50 AM..
This User Gave Thanks to port43 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nested case: rsync command not found

Hello! Below my first bash script. As you can see i build 2 nested cases. The second one (sync databases) is working fine. Bu the first one (sync datadirs) is not working. It says: rsync: command not found. However when i move the rsync command to the top of the script its working. So i suppose... (2 Replies)
Discussion started by: hyperconnected
2 Replies

2. UNIX for Dummies Questions & Answers

Case statement help

Hi I am new to shell scripting, I wanted to make a shell script that has a case statement asking the user to select their city 1)london 2)tokyo 3) etc., I then want the users input to be stored in a variable and echoed out in another script; so for example if the user selects tokyo, tokyo city code... (2 Replies)
Discussion started by: scriptnewbie
2 Replies

3. Shell Programming and Scripting

Nested case inside awk

please let me know if the below code could be written efficiently inside single awk case "$INP" in ksh) cat catalog | awk 'BEGIN {FS=",";} { print $2 } END {}' ;; pset) cat catalog | awk 'BEGIN {FS=",";} { print $3 } END {}' ;; dml) cat catalog | awk 'BEGIN {FS=",";} {... (2 Replies)
Discussion started by: cvsanthosh
2 Replies

4. Shell Programming and Scripting

Nested case

Hi there, I have nested case in my script. I am asking user, want to continue? if user press y/Y then my inner case should continue, rather than that my code start from beginning. I would like to continue my inner case until user press n or N. Is any one tell me how can I do? Thanking You,... (2 Replies)
Discussion started by: kasparov
2 Replies

5. Shell Programming and Scripting

Perl nested if statement

I'm just having a bit of trouble running this code. It tells me that there's a syntax error on line 29. Any help appreciated. #!/usr/bin/perl # # Phone Book Application # %phonebook = ( "Wayne", '34687368', "Home", '378643287', "Work", '017374637', "School",... (2 Replies)
Discussion started by: cabaiste
2 Replies

6. UNIX for Dummies Questions & Answers

Nested If statement within Do / Done

Hi all! I'm really hoping you can help me out here; now i have searched and searched and have at least worked out that you can't have a nested if statement with a 'done' in it (as i have) as you're killing the parent before the child. So here's what i have, and here's hoping someone can help... (2 Replies)
Discussion started by: dalgibbard
2 Replies

7. Shell Programming and Scripting

help with case statement

I am writing a script to pull diskspace information from our servers. Here is the script that I wrote: #!/bin/ksh for host in `cat /oper/hosts/esc.misc` do ssh -q -o ConnectTimeout=10 operator@$host df -h|grep "/dev/" |egrep '8%|9%|100%' | awk '{print H " " "at " $5 " with " $4 "... (1 Reply)
Discussion started by: rkruck
1 Replies

8. Shell Programming and Scripting

Nested Case in UNIX script

Hi I wanted to know if we can write a nested case in UNIX script. Something like following - Case ${sDB} in Srvr1) case ${sSchema} Sch1) DBusr=Username1 DBPwd=Pwd1 ;; Sch2) DBusr=Username2 ... (1 Reply)
Discussion started by: sumeet
1 Replies

9. Shell Programming and Scripting

case statement

Hi all, I think i'm asking a sqtupid question here.. i'm using case sttament, what is the syntax or symbol for "or"? I thought was || here a quick sample of my case statment echo "Would you like to update your detail ?" read response case $response in ... (2 Replies)
Discussion started by: c00kie88
2 Replies

10. Shell Programming and Scripting

while read loop w/ a nested if statement - doesn't treat each entry individually

Hi - Trying to take a list of ldap suffixes in a file, run an ldapsearch command on them, then run a grep command to see if it's a match, if not, then flag that and send an email alert. The list file (ldaplist) would look like - *********** o=company a o=company b *********** **... (7 Replies)
Discussion started by: littlefrog
7 Replies
Login or Register to Ask a Question