Ignore special characters in loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ignore special characters in loop
# 1  
Old 11-27-2014
Ignore special characters in loop

Hi All,

Code:
select app from the menu:  ABC DEF GHI JKL ALL  # ALL will select all the apps in the menu  echo "Enter your option" read option;  if [ ${option} = "ALL" ] then      <execute the below command> elif [ ${option} = "option" ]     # option is the 1 selection from menu...not ALL     <execute the below command> else    echo wrong option."" fi

question:
* id user enters A* or AB?...... the scripts executes at
Code:
elif

.
i want to avoid special characters in if loop...below is the syntax.....but i want to know is there any other where i can eliminate all Special chars with out using below format.
Code:
if [ $option != "*" ] || [ $option != "?" ] || [ $option != "&" ]

Thank you in Advance!
# 2  
Old 11-27-2014
Test for or eliminate? For the second, and the lower/upper case problem, try
Code:
read option
sdj$/%§keb()/&eWEqwe
echo $(tr 'a-z' 'A-Z' <<< ${option//[[:punct:]]})
SDJKEBEWEQWE

Again, needs a recent bash for the here string.
# 3  
Old 11-27-2014
Hi Rudic,

sorry, i made a wrong question in above thread....
actual question: the loop should not execute if the user enters special character(s) with the combination of menu..

Code:
AB*
DE?
GH#
IJ.


Last edited by Don Cragun; 11-27-2014 at 03:42 PM.. Reason: Add CODE tags.
# 4  
Old 11-27-2014
How about:
Code:
read input
echo "$input" | grep -q [\#\?\*\.] && exit 1

Hope this helps

EDIT:
Besides and again, why not use a select menu as in other thread suggested, would help to avoid invalid inputs.
# 5  
Old 11-27-2014
Quote:
Originally Posted by Devaraj A
but i want to know is there any other where i can eliminate all Special chars with out using below format.
Try this (it allows A-Z and a-z only):
Code:
#!/usr/bin/ksh

read -r option

if [[ "$option" =~ [^[:alpha:]] ]]; then
 echo "Variable 'option' contains non-alphabetic stuff."
 exit 1
fi

echo "OK"

=~ requires ksh93 though.
# 6  
Old 11-28-2014
Hi Sea/Jh,

Thanks for your response... but both didn't help me. i have an approach... it should allow only alphabets and - ( - "symbol").

i have command too:
Code:
echo "abcf*?123-" | sed 's/[^a-Z]*//g'

and
Code:
echo "abcf*?123-" | sed 's/[^-]*//g'

...

could you please help me to combine both. sed commands into 1 sed command.

out put required:
Code:
abcf-

# 7  
Old 11-28-2014
man regex:
Quote:
. . . To include a literal '-', make it the first or last character, or the second endpoint of a range. . . .
So:
Code:
echo "abcf*?123-" | sed 's/[^a-Z-]*//g'
abcf-

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed in a while loop with special characters

I have the foolowing data file: File1 <p name="A">5004</p> <p name="B">5004</p> <p name="C">5004</p> <p name="A">15004</p> <p name="B">15004</p> <p name="C">15004</p> In a while loop using sed (100 of line need to be replace), I need the output to File3:... (2 Replies)
Discussion started by: bobo
2 Replies

2. Shell Programming and Scripting

How to ignore characters and print only numbers using awk?

Input: ak=70&cat15481=lot=6991901">Kaschau (1820-1840) ak=7078&cat15482=lot=70121">Principauté (1940-1993) ak=709&cat=lot15484=70183944">Arubas (4543-5043)Output: 70 15481 6991901 7078 15482 70121 709 15484 70183944 (11 Replies)
Discussion started by: sdf
11 Replies

3. Shell Programming and Scripting

ignore blank lines in while loop

Hi, i am having one text file it contains some blank lines and i want to ignore that blank lines . #! /bin/bash clear rdCount=0; while read myline do echo $myline let rdCount=$rdCount+1 done < ps.txt echo "Total line=$rdCount" and ps .txt contains the data- (17 Replies)
Discussion started by: aish11
17 Replies

4. 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

5. 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

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

awk loop: display special characters

Hi everybody; I have a code and this fetches data from first.txt,modify it and outputs it to second.txt file. l awk 'NR>1 {print "l ./gcsw "$1" lt all lset Data="$2" Value "$3}' /home/gcsw/first.txt > /home/gcsw/second.txt this outputs as: l ./gcsw 123 lt all lset Data=456 Value 789 ... (1 Reply)
Discussion started by: gc_sw
1 Replies

8. 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

9. UNIX for Dummies Questions & Answers

How to ignore characters and print only number using unix?

say D45H E67H G779K F8888U T66Y Y333U output shud be like 45 67 779 8888 66 333 (5 Replies)
Discussion started by: cdfd123
5 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