0

I am working in CSH script and i am new to this script. My requirement is to grep a pattern in an array. I am able to get the same using bash but unable in CSH. Could you please help me this.

Bash programmin

        a = (arun kumar input output pin port)
        if [[ "${a[*]}" =~ "varun" ]]; then
               echo "Match found"
        fi

Thanks in Advance

14
  • grep is a program Commented Jul 17, 2022 at 7:18
  • so what is equivalent to above code in CSH Commented Jul 17, 2022 at 7:47
  • perhaps use grep? Commented Jul 17, 2022 at 7:53
  • I used grep but it is excepting filename instead of array. i have tried below one Commented Jul 17, 2022 at 7:55
  • grep "arun" $a. Commented Jul 17, 2022 at 7:56

1 Answer 1

1

csh has a very different syntax from Bourne shells such as bash; specifically:

  • You need to use set to assign variables, so:

      a = (arun kumar input output pin port)
    

    won't work, you need to use:

    set a = (arun kumar input output pin port)
    
  • The syntax for if is if ( .. ) then [..] endif, rather then if [[ .. ]]; then [..] fi.

There are many other differences too. If you must use csh, then it's best to just forget about the Bourne shell and start from zero.


Anyway, what you want to do is "check if a word appears in a list". Your bash example isn't quite correct either, since it just converts the array to a string and then checks if a pattern occurs with =~. This works fine for some cases, but consider what would happen if you want to check if the word "out" appears in your list: it would match output.

One way to do this would be to use:

set a = (arun kumar input output pin port)
if ( " $a " =~ *" varun "* ) then
        echo "Match found"
endif

Lists are automatically converted to a string. We surround this in spaces so that it only checks for the full word varun, rather than anything containing varun to fix the issue mentioned above.

I adapted the above from this answer on the Unix & Linux site (I don't use csh that often these days, so I couldn't remember what the best way to do it was), which I found with this search.


As an aside, csh is a very awkward language with many limitations and problems. I would highly recommend avoiding it and using bash (or even better: zsh) if you're able to.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.