G0Y1M
G0Y1M

Reputation: 79

Declaring BASH. Array of strings in a a script

I'm writing a small script to test my regex understanding of comparison operator "=~". I thought that my syntax was alright but I keep getting:

3: Syntax error: "(" unexpected

this is my small script link to this syntax error :

#!/bin/bash

inputsArr=("ab" "67" "7b7" "g" "67777" "07x7g7" "77777" "7777" "")

for input in ${inputsArr[@]}; do
  if [[ "$1" =~ "$input" ]]; then
    echo "$?"
fi
done

I try to compare in a loop with an array some "strings" against my arg1 or "$1"

Upvotes: 2

Views: 2681

Answers (2)

G0Y1M
G0Y1M

Reputation: 79

I just reach my goal with this !

#!/bin/bash

inputsArr=("ab" "67" "7b7" "g" "67777" "07x7g7" "77777" "7777" "")

for input in ${inputsArr[@]}; do [[ "$input" =~ $1 ]]; echo "$?" ; done 

I would like to say thanks you to every person that give me some tips on this basic BASH script problem. Without you I would certainly not reach my goal by my own way and it is beautiful to see this cooperation in action.

Upvotes: 0

Mike Q
Mike Q

Reputation: 7327

The code works in bash, you just need to run it in the right shell, you can do the following:

  bash ./script.sh g

Also type ps -p $$ (not echo $SHELL) to see what shell you are currently in:

Examples:

# ps -p $$
    PID TTY          TIME CMD
  25583 pts/0    00:00:00 sh
# exit
# ps -p $$
    PID TTY          TIME CMD
  22538 pts/0    00:00:00 bash

Upvotes: 3

Related Questions