Exiting the script if the character is not recognized


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exiting the script if the character is not recognized
# 1  
Old 04-10-2014
Hammer & Screwdriver Exiting the script if the character is not recognized

Below is the script that i'm using but i'm getting an error,
Code:
echo -n "Read the letter >(enter a or b or c) "
read letter

if [ $letter -ne a ] || [ $letter -ne b ] || [ $letter -ne c ];
then
        echo "unacceptable character"
else
echo "Character Accepted"
fi

if the character entered is not equal to a or b or c, the script should exit with the message displayed on the screen.

Any help will be appreciated.

Thanks

Last edited by Franklin52; 04-11-2014 at 03:36 AM.. Reason: Please use code tags
# 2  
Old 04-10-2014
Code:
       arg1 OP arg2
              OP  is  one  of  -eq,  -ne, -lt, -le, -gt, or -ge.  These arithmetic binary operators
              return true if arg1 is equal to, not equal to, less than,  less  than  or  equal  to,
              greater  than,  or greater than or equal to arg2, respectively.  Arg1 and arg2 may be
              positive or negative integers.

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 04-10-2014
Code:
#!/bin/bash

echo -n "Read the letter >(enter a or b or c) "
read letter

# One easiest way to Compare 
for i in a b c; do
	[ "$i" = "$letter" ] && break
done

# ok will be zero if true else 1
ok=$?

# Compare
if [ $ok -ne 0 ];then
	echo "unacceptable character"
else
	echo "Character Accepted"
fi

Syntax for string
Code:
# Syntax for string 

a="A"; b="A";

if    [ "$a" != "$b" ]; then
	echo "Not Equal"
elif  [ "$a" = "$b" ]; then
    	echo "Equal"
fi

This User Gave Thanks to Akshay Hegde For This Post:
# 4  
Old 04-10-2014
Hi Akshay.

your code, worked.
Thanks a ton.
# 5  
Old 04-12-2014
Use
Code:
if [ $letter != a ] && [ $letter != b ] && [ $letter != c ]

in your code.
# 6  
Old 04-12-2014
Using a case statement:
Code:
#!/bin/bash

echo -n "Read the letter >(enter a or b or c) "
read letter

case $letter in
  a|b|c)  echo "Character Accepted"
          ;;
      *)  echo "Unacceptable Character"
          exit
          ;;
esac

This User Gave Thanks to fpmurphy For This Post:
# 7  
Old 04-12-2014
Quote:
Originally Posted by fpmurphy
Using a case statement:
I like the case-statement for this task. You can also easily add another block to fine tune the error handling, e.g. ??*) echo Too many characters;;.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Weird issue - *ksh script not recognized when being called

HI Team - I'm running into a weird issue when trying to call a .ksh script. In my shell script, I'm using the following command to call my environment file: cd /hypbin/test ./_env.ksh But it's saying not found. Permissions are set correctly, shebang is set but I'm unsure why it's not... (5 Replies)
Discussion started by: SIMMS7400
5 Replies

2. Shell Programming and Scripting

Help for exiting the function not script

function2() { cmd1 cmd2 cmd3 .... cmdn } function2() { cmd11 cmd12 cmd13 .... .... } for i in {1,2} (7 Replies)
Discussion started by: yanglei_fage
7 Replies

3. Shell Programming and Scripting

Exiting from the script abruptly

Hi Team, Need your help for the below code snippet. I wrote a module to read the file names remote server using file name convention. Issue : My script is coming out from while loop without reading complete file. test1() { while read line do echo $line file_nm_convention=`echo... (3 Replies)
Discussion started by: gvkumar25
3 Replies

4. Shell Programming and Scripting

Alias not recognized within script

What would cause a script to work under one user account and not another? Here's an example of what I'm referring to. Here's a simple script. Let's put it in a file called “thescript”. #! /bin/bash alias a='echo hello world' a Here are the results when this script is executed logged in... (3 Replies)
Discussion started by: sszd
3 Replies

5. Shell Programming and Scripting

Exiting out of the script

I have to write a script in ksh which again should call another script. Say A.ksh is calling B.ksh. Now in B.ksh if the condition we are checking for is true then we have to go back to the main script A.ksh or if the condition in B.ksh is false then we have to totally come out of the scripts. I... (1 Reply)
Discussion started by: vpv0002
1 Replies

6. Shell Programming and Scripting

Bash Script Not Exiting

I have a script planned for the Helpdesk to allow them to (on a couple of RHEL 3 / RHEL 5 servers) reset passwords, stop / start printers, and clear print queues. The appropriate sudo permissions were given to their accounts, and the individual functions all work just fine. The ability to move... (0 Replies)
Discussion started by: tearsong
0 Replies

7. Shell Programming and Scripting

exiting from script

Hi, I am trying to exit the script from a function. I was in assumption that if we use exit ( inside or outside the function) it will exit from the script. alternatively, return will exit from that particular function. but in my case, exit is exiting from the function and not the script.... (8 Replies)
Discussion started by: shellwell
8 Replies

8. Shell Programming and Scripting

shell script exiting before completing

I have a script which has the following statement, /opt/oracle/product/9i/bin/sqlplus << EOC >> $LOG_FILE 2>&1 username/password ---- Enters the SQL prompt @/export/home/oracle/shells/grant_userview.sql ---Runs the SQL script @/export/home/oracle/shells/grant_proc_userview.sql ---Runs the... (6 Replies)
Discussion started by: welldone
6 Replies

9. Shell Programming and Scripting

Exiting a script

I have a script abc.sh. Its contents are as follows: (7 Replies)
Discussion started by: lassimanji
7 Replies

10. Shell Programming and Scripting

exiting from script

there are many script in my project.i am having a problem when i am trying to quit from child script.what is the command to wrap up all the parent script and calling script as well? exit 0 is not working.please help.... (1 Reply)
Discussion started by: arghya_owen
1 Replies
Login or Register to Ask a Question