Command to find the color of output text of last executed command



Linux command to find the color of output text of last executed command

I have one small question.

how to find the color of output text of last command executed.

suppose i execute a command.
$ itkadmin config -nwid ora2gchild -set nd.baseMomSchemaFile -edit
### its output things in green when success and red when some has went wrong
$ file not updates

I want a command that can find the color of the output of last command executed and
then i can continue if green and exit if red , with the output of last command on screen ,
so that user can check and correct the error.

I wanted the commands to run in shell script.

Thank You in Advance.

I don't know of a way to capture color on CLI.
But you could issue a command: "echo $?" and it will display the previous commands' exit code. Generally 0 is for success (green) and non zero value is for error (red).
I have tried this echo $? , many times the command executes with errors and output for correcting the command, still the answer is 0

So waiting for some more answers.

Command to find the color of the output text of earlier command.
I don't think there are any tools that will capture the colour of the output from a previous command. Usually you'd use the exit status of a script to determine the state it ended in.

As asdqwe pointed out (above):
Most programs exit with a status of 0 for success and any other value for an error.
Some programs might have different exit codes for different states.

What is this program/script that you are using (i.e. the program that generates the coloured text?) Is it something you have created yourself? Or something you have downloaded from elsewhere? Is it possible for you to modify this program in any way?

The reason I ask is because if the program is displaying red text to indicate that the program ended because of an error-condition, but it is also returning 0 (success), then that behaviour is wrong IMO.

In order for this program/script to be useful inside another script, the program/script that generates the coloured output should be modified so that when it exits with green text, it should also return 0 to indicate a successful run. If it exits with red text, it should return some other value to indicate that an error occurred. You could have different return codes for different error states.

That way, you can run the program in your script and determine the state the program ended in without having to worry about the colour of the text. You instead check the value returned by the program when it exits. The colour of the text merely makes the programs output more easily understandable to the user. But for scripting purposes, the return value/exit-state/exit-code of the program is more useful!
Thank You JasKinasis

suppose i execute a command.

$ itkadmin config -nwid ora2gchild -set nd.baseMomSchemaFile -edit

### its output things in green when success and red when some has went wrong

$ file not updates

itk is a software which is installed on Centos only and then we use commands with in itk like itkadmin to update many files .
and these commands can only be used to update those files, as we do not know names of files.
OK, I still don't know what this itk program is though and have not found any information about it online. The only software I could find called itk is a medical imaging library, not sure if that's the one you are talking about. Can you provide any more detail on where this itkadmin program comes from? I just thought if we knew exactly what software we were dealing with, there might be other options available to catch/deal with errors.

Anyway, I have found this thread on stack overflow that might help you:

Apparently, the 'script' command will start a new shell and run a specified command/script and will write the output from the shell session to a typescript file. If colourised output is enabled in the shell, the escape codes for the colour will be written to the typescript file.

So you could call your script via the script command and then parse each line in the typescript file and search for the colour code for red (01;31m will probably suffice). For any lines that contain the red colour code (or any other colours you need to detect), you will then need to determine the filename and then do whatever you need to do to that file - Append the filename to a list of failed files, run another script etc...

So as a simple, contrived example:
script -c 'grep Keyword --color ~/' OUTPUT
The above would start a new shell and run a grep command which will search for any files in my home directory that contained the word 'Keyword'. Output from the command is written to a typescript file called OUTPUT. And because colourised output was enabled in grep, any instances of 'Keyword' will be highlighted in a different colour in the shell and the escape sequence used to generate that colour will be written to the typescript file. As a sanity check, by opening the typescript file in a text editor, you should be able to see the escape sequences used to generate the colours.

So that would be one option. Not sure if that is any good for you?!
You might need to read the man/info pages for the script command to see if you can get it to do what you want!

Bearing the above information in mind, perhaps this will do the trick:
script -c 'itkadmin config -nwid ora2gchild -set nd.baseMomSchemaFile -edit' OUTPUT
Once that has completed, you need to run a command/script to open the OUTPUT file, find any lines containing the escape-codes for the colours you are looking out for. Once you've found a line containing the escape-code you'll need to determine the name of the file that the command failed for and then do whatever you need to do in the event of a failure!
Last edited: