-3

I'm very new to Unix scripting (.ksh). I have to implement a functionality to check whether my argument says "welcome" present in an string array e.g.

{"welcome","test","exit"}

The logic is similar to String.contains in Java.

Any help will be appreciated.

2
  • ksh or bash? (you have bash in tags) Commented Jan 8, 2013 at 12:54
  • @Stefanos Kalantzis its ksh Commented Jan 8, 2013 at 13:16

1 Answer 1

1

You can do something like this. Following is in bash, you need to change it accordingly to ksh.

script

array=(welcome test exit)
string='welcome';
for item in ${array[*]}
do
    if [[ $string =~ .*$item.* ]]
    then
        echo "It's present!"
    fi
done

Output

It's present!

To iterate over arguments passed to a shell script, use for with empty in, that default the iteration over arguments, or in '$@'.

1

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.