0

I'm becoming freaking crazy because of bash programming

I have the following:

result="ERROR|0"  # From a complex sed execution
IFS='|' read -a array <<< "$result"

if [ "${array[0]}" == "ERROR" ]; then
    echo "Error"
fi

It should print Error, but it doesn't.

Any ideas what I'm doing wrong?

Editing: Actually, now it works, so I add the previous code where I get the "result" string

result=$( [some code that gives me an output] \
          | grep "Executed" \
          | sed s/'\(.*\) Executed \(.*\) of \(.*\) \(.*\) (.*)'/'\4|\3'/ \
        )

it should return a string, shouldn't it? The string I wrote before

4
  • 1
    it does for me (now that you have corrected the errors in the script) Commented Sep 5, 2013 at 15:24
  • are you sure you are running the snippet with bash? do you get errors? what does echo ${array[@]} print? Commented Sep 5, 2013 at 15:29
  • 1
    The problem is that there are not visible characters for colouring the output and it's pretty weird get a regexp for it Commented Sep 17, 2013 at 12:31
  • you are probably aware that nobody can know about extra characters if you don't tell us (you gave "ERROR|0" as non-working input). even now, you give us some code that gives me an output, which can be virtually anything. Commented Sep 17, 2013 at 12:38

2 Answers 2

4

You had two small errors in the first version of your code:

  • $result="ERROR... (to assign, you have to use result=value)
  • <<< "$result (you missed the second " to close quotation).

With this code, it works:

result="ERROR|0"  # From a complex sed exectution
IFS='|' read -a array <<< "$result"

if [ "${array[0]}" == "ERROR" ]; then
    echo "Error"
fi

In fact, I think you updated your code in the grace period and now it is fine.

I added #!/bin/bash in the first line and executed:

$ ./script
Error
Sign up to request clarification or add additional context in comments.

Comments

0

when you assign values to variables, do not use $ notation. e.g. use

 result="foo"

rather than

 $result="foo"

and then don't forget the terminating quotes when writing a string:

IFS='|' read -a array <<< "$result

misses the terminating quote in "$result.

a good syntax highlighter can help a lot (simply compare the syntax-highlighting in your question with that in the answers)

1 Comment

I've forgot the quotes and accidentally added the $ notation at begin because I had to adapt my script in order to make it clear. Nonetheless it still doesn't work

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.