Skip to Main Content

Infrastructure Software

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Check a string in bash for unwanted characters

Dude!Feb 11 2012 — edited Feb 11 2012
Hello,

I'm trying to do a bash script that checks a variable against a list of unwanted characters, e.g. to parse a file name.

This doesn't really sound like a difficult task, but for some reason, whatever I've tried so far does not work, including my last attempt, shown below. Perhaps I'm doing something silly here - and I'm getting tired of it. What would be the best way for instance to parse a file name for invalid characters, or to accomplish or fix the below?

#!/bin/bash

read -p "Enter a filename: " fname 
invalid_chars=", . ! @ # \$ % ^ & \* ( ) + = ? { } [ ] | ~"

i=0
while (( i <= ${#fname} )); do
   char=${fname:$i:1}
   for char in `echo $invalid_chars`; do
     echo "$char"
   done
   (( i += 1 )) 
done
Thanks.
This post has been answered by Tommy.Reynolds-Oracle on Feb 11 2012
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 10 2012
Added on Feb 11 2012
6 comments
1,716 views