Search code examples
bashcharacterdetection

bash: special character detection ( & ` ´ ' " ~ / | \ [ ] $ * ; ) via if;then


Wrecking my head over this for days, i've tried quite a few solutions to this. Each method worked to some degree (searched here on stackoverflow), but each method failed to pick up most of the special characters. (and i'm really not good with the ` ' " [ { characters for if checks)

Basically i want to detect if one of these characters is in the filename ( & ` ´ ' " ~ / | \ [ ] $ * ; ) if the user selects a file and i'll make an error dialogue and a exit 0 in the code.

I'm currently using this:

STARTUPFILE="$(/usr/bin/osascript -e "tell application \"System Events\" to activate" -e "tell application \"System Events\" to set thefile to choose file with prompt \"choose file \"" -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")")"


FILENAME=$(basename "$STARTUPFILE") # via osascript input dialogue

if [[ $FILENAME = *[( & ` ´  ' " ~ / | \ [ ] $ * ; )]* ]]; then

What's the best way to detect all of them short of only allowing a-Z & 0-9?


Solution

  • Thanks everyone, but nothing except limiting it to a-Z and 0-9 worked so far, but then i'd have to go the other way around and allow specific characters.

    I'll post the answer here if anyone wants it.

    I've experimented around with a few things in the morning (heh, i can never solve problems like these coding at night) and got something working.

    It's this:

    if [[ $FILENAME = *['&''#''$''['']''\/''\'':''|'';''{''}''´''`''"''*''~'"'"]* ]]; then
    

    I had to escape just about every character with ', except escaping ' with a double " " at the end. Also as chepner mentioned, / converts to : in OSX, so checking for it is done via / and :, hence both in the code.

    Only this method worked so far.

    written a little shell for checking some filenames:

    if [[ $FILENAME = *['&''#''$''['']''\/''\'':''|'';''{''}''´''`''"''*''~'"'"]* ]]
    then
        echo FILENAME = "$FILENAME"  >> "/Users/zero/Desktop/text.txt"
        echo FILENAME does not pass character filter >> "/Users/zero/Desktop/text.txt"
        echo " " >> "/Users/zero/Desktop/text.txt"
    else
        echo FILENAME = "$FILENAME"  >> "/Users/zero/Desktop/text.txt"
        echo FILENAME passes character filter >> "/Users/zero/Desktop/text.txt"
        echo " " >> "/Users/zero/Desktop/text.txt"
    fi
    

    that gives the following log output:

    FILENAME = _PI   something normal even with () quotes or - + _ .iso
    FILENAME passes character filter
    
    FILENAME = _PI  _ something.iso
    FILENAME passes character filter
    
    FILENAME = _PI  - something.iso
    FILENAME passes character filter
    
    FILENAME = _PI  ! something.iso
    FILENAME passes character filter
    
    FILENAME = _PI  ? something.iso
    FILENAME passes character filter
    
     FILENAME = _PI  + something.iso
    FILENAME passes character filter
    
    FILENAME = _PI  # something.iso
    FILENAME does not pass character filter
    
    FILENAME = _PI ' something.iso
    FILENAME does not pass character filter
    
    FILENAME = _PI " something.iso
    FILENAME does not pass character filter
    
    FILENAME = _PI [ something.iso
    FILENAME does not pass character filter
    
    FILENAME = _PI ] something.iso
    FILENAME does not pass character filter
    
    FILENAME = _PI { something.iso
    FILENAME does not pass character filter
    
    FILENAME = _PI } something.iso
    FILENAME does not pass character filter
    
    FILENAME = _PI * something.iso
    FILENAME does not pass character filter
    
    FILENAME = _PI : something.iso
    FILENAME does not pass character filter
    
    FILENAME = _PI \ something.iso
    FILENAME does not pass character filter
    
    FILENAME = _PI & something.iso
    FILENAME does not pass character filter
    
    FILENAME = _PI ` something.iso
    FILENAME does not pass character filter
    
    FILENAME = _PI ´ something.iso
    FILENAME does not pass character filter
    
    FILENAME = _PI | something.iso
    FILENAME does not pass character filter
    
    FILENAME = _PI ~ something.iso
    FILENAME does not pass character filter
    
    FILENAME = _PI $ something.iso
    FILENAME does not pass character filter