Redirecting Command Result to File
When running a command, the output is usually visible through the command line interface. However, it's also possible to pipe the command's result and error logs to files.
Recommendation
In order to have parseable and easy to read content in your files, use your command with the --json
option. This is what a command with the --json
option might look like:
ugs env list --json
Redirecting a command's result (stdout)
To redirect the result of a command to a file, add 1> res.json
to the end of your command. When your command succeeds, its result will be redirected to the res.json
file.
Redirecting a command's error output (stderr)
To redirect the error output of a command to a file, add 2> logs.json
to the end of your command. Any error during the execution of your command will end up in the logs.json
file.
Example
Here's an example of what a command with stdout and stderr piping might look like:
ugs env list --json 1> res.json 2> logs.json
On success, a file named res.json
is generated. Here's an example of what that file might contain:
[
{
"id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"projectId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"name": "production",
"isDefault": true,
"createdAt": "2022-07-27",
"updatedAt": "2022-12-22",
"archivedAt": null
}
]
On failure, a file named logs.json
is generated. Here's an example of what that file might contain:
[
{
"Message": "You are not logged into any service account. Please login using the 'ugs login' command.",
"Type": "Error"
}
]
Notes
The file names provided as example res.json
and logs.json
are arbitrary names. You may change them to fit your needs.
Although the example uses both result piping and error piping at the same time, you may also use only one or the other.
When using piping, no output will be logged on the command line interface.