2

My issues is that I want to read a character from the stdin and compare it with a string value. If that character exists then I want to display a message. Eg, saving % into the variable $a:

$ read a % $echo $a % 

There is variable defined say v='~`!@#$%^&*()_-+=:;{[}]|\/<>,."' all possible special character including single tick '

I need to check if the character input exists in the string value. How can I do this.

    3 Answers 3

    2
    case $v in *"$a"*) printf '<%s> is in <%s>\n' "$a" "$v" esac 
    0
      1

      Here you go:

      #!/bin/bash v='~`!@#$%^&*()_-+=:;{[}]|/<>,."' echo type any character and press enter IFS= read -r a [[ $v =~ "$a" ]] && echo match || echo not 
      2
      • Janos, I didn't know how to run this from the script: [[ $v =~ $a ]] && echo match || echo notCommentedJan 16, 2014 at 22:31
      • @NirmalArri save the script for example in script.sh, and run it with bash script.sh
        – janos
        CommentedJan 16, 2014 at 22:52
      -1

      Try this

      cat | egrep -e '[~`!@#$%^&*()_-+=:;{[}]|/<>,."]' 
      4
      • What are you cating here? Did you mean something like echo "$a" | grep -E ["$v"]?
        – terdon
        CommentedJan 16, 2014 at 23:22
      • 1
        No, not quite: the first ] marks the end of the set.CommentedJan 16, 2014 at 23:48
      • @Giles You're correct hadn't noticed that!
        – X Tian
        CommentedJan 17, 2014 at 1:32
      • @terdon I just typed a couple of test lines straight into cat, but agree your use of variables closer to question.
        – X Tian
        CommentedJan 17, 2014 at 1:32

      You must log in to answer this question.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.