Ommiting special characters as input - Help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ommiting special characters as input - Help
# 1  
Old 11-17-2010
Ommiting special characters as input - Help

Hey Everyone,

I'm quite new to unix (hence the 0 posts!) and im trying to write a simple program that outputs what the user types in to the screen, as long as it is a letter.

This part works fine, however, when a "\" is entered doesnt not display anything and moves to the next line. Is there any way to display an error message when a backslash/or special characters are entered?

thanks in advance everyone
# 2  
Old 11-17-2010
Please show us your code...
# 3  
Old 11-17-2010
Code:
 
#!/bin/bash
 
echo "enter a letter"
read letter
echo $letter
 
lettercheck=$(echo "$letter" | grep [a-zA-Z])
 
                if [ -z $letter ] #checks if length of string is zero
                then
                        echo "Invalid input. Input is not a letter"
                        
                else
                        echo $letter
 
        fi

thats pretty much it. the code works fine, but when a "\" is entered it just moves onto the next line rather than checking the input at all.
# 4  
Old 11-17-2010
You need to test $lettercheck not $letter.


Code:
                if [ -z "${lettercheck}" ] #checks if length of string is zero


And try "read -r" to deal with the reverse solidus character (which has special meaning to the Shell).

Code:
read -r letter


Last edited by methyl; 11-17-2010 at 07:50 AM.. Reason: And ...
# 5  
Old 11-17-2010
thanks a lot methyl!

in my original code I was checking the right variable by I retyped an easier version of what I was doing and forgot to change it.



read -r was exactly was I was looking for so thanks a lot!!
# 6  
Old 11-17-2010
Or use:
bash code:
  1. if [[ $letter == [[:alpha:]] ]]; then
  2.   echo $letter
  3.  else
  4.   echo "Invalid input. Input is not a letter"
  5. fi

or use:
shell code:
  1. case $letter in  
  2.   [[:alpha:]]) echo $letter ;;
  3.   *) "Invalid input. Input is not a letter" ;;
  4. esac

Last edited by Scrutinizer; 11-17-2010 at 08:31 AM..
# 7  
Old 11-17-2010
cheers for the input scrutinizer. however, the problem was that if you enter "\" then without -r it doesnt actually get to any of your checks, as it reads the "\" as an escape character and does nothing.

thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delimit the fields of a input file which has special characters?

Hi All, I am a newbie to Shell scripting. I have a requirement to Delimit the file fields of a Input file having special characters and spaces with ";". Input File ---------------------------------- Server Port ---------------------------------- Local ... (5 Replies)
Discussion started by: Suganbabu
5 Replies

2. Shell Programming and Scripting

How to escape all special characters?

I have an application which I am integrating with that accepts the password via a CLI. I am running in to issues with passwords that contain special characters. I tried to escape them all, but I ran in to an issue where I cannot escape the characters ' ] My attempt is as follows: $... (2 Replies)
Discussion started by: AMG1978
2 Replies

3. Shell Programming and Scripting

Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below. test!=123-> test\!\=123 !@#$%^&*()-= to be replaced by \!\@\#\$\%\^\&\*\(\)\-\= (8 Replies)
Discussion started by: laknar
8 Replies

4. Shell Programming and Scripting

special characters

Hey guys, I'm trying to replace "]Facebook" from the text but sed 's/]Facebook/Johan/g' is not working could you please help me with that? (6 Replies)
Discussion started by: Johanni
6 Replies

5. Shell Programming and Scripting

Check input for lenght, special characters and letter case

I made menu script for users so they can run other script without going in shell just from menu. But i must control their input. These are criteria: Input must have 4 signs First two signs are always lower case letters Input shall not have some special signs just letters and numbers ... (1 Reply)
Discussion started by: waso
1 Replies

6. UNIX for Dummies Questions & Answers

How to see special characters?

Hi all, I was wondering how can i see the special characters like \t, \n or anything else in a file by using Nano or any other linux command like less, more etc (6 Replies)
Discussion started by: gvj
6 Replies

7. Shell Programming and Scripting

Special characters

When I open a file in vi, I see the following characters: \302\240 Can someone explain what these characters mean. Is it ASCII format? I need to trim those characters from a file. I am doing the following: tr -d '\302\240' ---------- Post updated at 08:35 PM ---------- Previous... (1 Reply)
Discussion started by: sid1982
1 Replies

8. Shell Programming and Scripting

Help in preserving special characters from input file

Hi Forum. I've tried to search online for a solution but I cannot seem to find one. Hopefully, someone here can help me out. I would appreciate it. Input file abc.txt: $InputFile_Borrower=CMTSLST\EDW_COMMERCIAL_MTGE_BORROWER_dat.lst... (14 Replies)
Discussion started by: pchang
14 Replies

9. Shell Programming and Scripting

Bourne Shell: Special characters Input Prevention

Environment: Sun UNIX Language: Bourne Shell Problem: I am writing a script that allows user to key in a directory. Example: /root/tmp. Since the user can key in anything he/she wants, I need to validate to make sure that he/she does not key in anything funny i.e.... (37 Replies)
Discussion started by: totziens
37 Replies

10. UNIX for Dummies Questions & Answers

special characters

I have one file which is named ^? ( the DEL character ) I'd like to know how to rename or copy the file by using its i-node number TYIA (2 Replies)
Discussion started by: nawnaw
2 Replies
Login or Register to Ask a Question