https://github.com/chen-keinan/go-command-eval
Go-command-eval is an open-source which helps to execute a shell command and evaluate it results in a flexible ways
https://github.com/chen-keinan/go-command-eval
command command-line comparison eval executable golang linux-shell shell shell-command shell-script
Last synced: 11 months ago
JSON representation
Go-command-eval is an open-source which helps to execute a shell command and evaluate it results in a flexible ways
- Host: GitHub
- URL: https://github.com/chen-keinan/go-command-eval
- Owner: chen-keinan
- License: apache-2.0
- Created: 2021-07-08T15:07:49.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-12-02T20:02:15.000Z (about 1 year ago)
- Last Synced: 2025-03-18T09:12:56.843Z (11 months ago)
- Topics: command, command-line, comparison, eval, executable, golang, linux-shell, shell, shell-command, shell-script
- Language: Go
- Homepage:
- Size: 450 KB
- Stars: 3
- Watchers: 1
- Forks: 2
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[](https://goreportcard.com/report/github.com/chen-keinan/go-simple-config)
[](https://github.com/chen-keinan/go-command-eval/blob/master/LICENSE)
[](https://gitter.im/beacon-sec/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)

# go-command-eval
Go-command-eval is an open source lib who evaluate shell command results against eval expr
* [Installation](#installation)
* [Usage](#usage)
* [Contribution](#Contribution)
## Installation
```shell
go get github.com/chen-keinan/go-command-eval
```
## Usage
### one shell command with single result evaluated against eval expression
create shell command which return one result
```go
shCmd:=[]string{"ls /etc/hosts | awk -F \" \" '{print $1}' |awk 'FNR <= 1'"}
```
evaluate command result with eval expression ( ${0} is the result from 1st shell command)
```go
evalExpr := "'${0}' == '/etc/hosts'"
```
### two shell commands with single result each evaluated with eval expression
create two shell commands with one result for each
```go
shCmd := []string{"ls /etc/hosts | awk -F \" \" '{print $1}' |awk 'FNR <= 1'",
"ls /etc/group | awk -F \" \" '{print $1}' |awk 'FNR <= 1'"}
```
evaluate each command result with eval expression
```go
evalExpr := "'${0}' == '/etc/hosts'; && '${1}' == '/etc/group';"
```
### shell command return two results evaluated with IN Clause eval expression
create shell command with return two results
```go
shCmd := []string{"ls /etc | awk -F \" \" '{print $1}' |awk 'FNR <= 2'"}
```
evaluate command result with IN Clause eval expression
```go
evalExpr := "'${0}' IN ('afpovertcp.cfg','aliases')"
```
### shell command result passed as an arg to the following shell command; both results are evaluated against eval expression
create tow shell commands 1st command result passed as an arg to the following shell command
```go
shCmd := []string{"ls /etc/hosts | awk -F " " '{print $1}' |awk 'FNR <= 1'",
"stat -f %A" ${0}}
```
both results are evaluated against eval expression 1st result is evaluated as string
2nd result is evaluated as an Integer
```go
evalExpr := "'${0}' == '/etc/hosts'; && ${1} <= 766"
```
Full code example
```go
package main
import (
"fmt"
"github.com/chen-keinan/go-command-eval/eval"
)
func main() {
shCmd := []string{"ls /etc/hosts | awk -F \" \" '{print $1}' |awk 'FNR <= 1'",
"ls /etc/group | awk -F \" \" '{print $1}' |awk 'FNR <= 1'"}
evalExpr := "'${0}' == '/etc/hosts'; && '${1}' == '/etc/group';"
cmdEvalResult := eval.NewEvalCmd().EvalCommand(shCmd, evalExpr)
if cmdEvalResult.Match {
fmt.Print("commmand result match eval expression")
}
}
```
## Contribution
code contribution is welcome
contribution with passing tests and linter is more than welcome :)