I have several Exception classes like this one (is PHP, but it doesnt matter right now):
class FileNotFoundException extends OtherException {
const DEFAULT_FILE_NOT_FOUND_ERROR_CODE = 159;
public function __construct($code=self::DEFAULT_FILE_NOT_FOUND_ERROR_CODE) {
parent::__construct($code);
}
}
The Problem
I need to get the error codes from all files to store them into an array (the index is the error code, and the value is the constant name) to just print it with order.
Tips
- All the files are
.phpfiles - The constant error codes are always like
const DEFAULT_[A-Z_]_ERROR_CODE = [0-9]+; - I tried to be stored in an array like this
array[159]="DEFAULT_FILE_NOT_FOUND_ERROR_CODE";
What have I done
What I tried to do –with my little bashscripting knowledge– is a script that parses all this php exception files and gets only the constant DEFAULT_[...]_ERROR_CODE = "number";
This is my script.sh trying to get it:
#! /bin/bash
sed -n "s/const \(DEFAULT[A-Z_]*\) = \([0-9]*\);/$array[\2]=\1;/p" $1
And if I do this:
script.sh < FileNotFoundException.php
It outputs [159]=DEFAULT_FILE_NOT_FOUND_ERROR_CODE;
Then I tried to put a variable “array” in there, like this:
eval('$array(`sed -n "s/const \(DEFAULT[A-Z_]*\) = \([0-9]*\);/$array[\2]=\1;/p" $1`)')
and several other combinations, but with no success.
Why I’m posting this in stackOverflow
I wanted to know if is possible to solve it and how, or if there is other way easier to do it.
Thanks
Remove the “$” in front of
array:Now it will output
array[159]=DEFAULT_FILE_NOT_FOUND_ERROR_CODE;which can be eval’d to set array elements:(I also added some
.*s to the sed expression so that appending a$(rm -ri /)to the line in the php script won’t cause you to evaluate it.)