codeskyblue / go-sh Goto Github PK
View Code? Open in Web Editor NEWlike python-sh, for easy call shell with golang.
License: Apache License 2.0
like python-sh, for easy call shell with golang.
License: Apache License 2.0
var r []byte
if r, err = sh.Command("echo", "$PATH").Output(); err != nil {
fmt.Println("filesystem anomalies")
return
}
它将直接输出 $PATH
,理想状态下还是希望能够回馈环境变量列表
It is possible to redirect output?
Like: mysqldump dbname | gzip -9 > dump.sql.gz
Or stderr: tar czf archive.tar.gz ./dir/ 2> /tmp/error.log
It is possible to get the stdoutPipe?
just as func (c *Cmd) StdoutPipe() (io.ReadCloser, error)
if so, we can get the output when we exec the commands
Can we do something like this with the following with this plugin
$ > myshell
myshell > mycommand1
myshell > mycommand2 some_sub_command --a some_args
myshell > exit
$ >
Essentially have a shell that has it's own commands?
hi, if command like this, how to write?
nohup ~/dev/golang/myApps/src/gin_sample/gin_sample > /dev/null 2>&1&
my code works fine on foreignground, but can't run as daemon mode
package main
import (
"github.com/codeskyblue/go-sh"
"os"
"syscall"
)
func main() {
session := sh.NewSession()
session.Command("nohup", "/Users/mmc/dev/golang/myApps/src/gin_sample/gin_sample")
session.Stdout = os.NewFile(uintptr(syscall.Stdout), "/dev/null")
session.Stderr = os.NewFile(uintptr(syscall.Stdout), "/dev/null")
session.ShowCMD = true
session.Run()
}
how to run in background?
thank you very much!
Is it possible to execute a shell script as part of other commands with this library?
-Thanks.
Hello!
I have question.
I have this code:
session := sh.NewSession()
session.SetTimeout(time.Second * 30)
session.ShowCMD = true
session.SetDir(repositoryPath)
session.Command(
"rm", "-f", archiveFile, "&&",
"git", "archive", branchName, "--format", "tar", "--output", archiveFile,
)
And debug message:
[golang-sh]$ rm -f /tmp/archive.tar && git archive origin/master --format tar --output /tmp/archive.tar
But second command not executed...
If i remove "rm", "-f", archiveFile, "&&",
Its work, but i need this "&&" operator.
What mistake?
close
I'm trying to figure out, is it possible to work with interactive shell commands?
e.g. as a first step, connect to mongodb:
mongo --quiet --host=localhost blog
then perform arbitrary number of commands, like
db.getCollection('posts').find({status:'INACTIVE'})
and then
exit
UPD
figured it out
go-sh包 ,用到DOCKER操作指令时是无信息返回的。
觉得非常奇怪。
go-sh包所在程序在宿主机上运行,运行docker --help是可以正常获得反馈,
但是使用docker ps -a之类的指令时,是无反馈回来的,
而在cmd面板ssh操作是可以正常反馈的。
是跨容器导致了这个问题吗,有办法解决吗?很头大
diff <(echo "string1") <(echo "string2")
Hi, I am having problems in setting timeout in command and getting stdout at the same time. I used your example, and it works but I can't get output from this command:
c := sh.Command("sleep", "3"); c.Start(); c.WaitTimeout(time.Seocnd)
I can use Output() function to get output but then timeout is not working.
sh.Command("sh", "-c", fmt.Sprintf("oc delete all -n %v %v", namespace, labels)).
Command("sh", "-c", fmt.Sprintf("oc delete service -n %v %v", namespace, labels)).
Command("sh", "-c", fmt.Sprintf("oc delete configmap -n %v %v", namespace, labels)).
Command("sh", "-c", fmt.Sprintf("oc delete secret -n %v %v", namespace, labels)).
Output()
not return
such as this
cat file.txt >> test.txt
// the following code is from pipe.go
func (s *Session) WaitTimeout(timeout time.Duration) (err error) {
select {
case <-time.After(timeout):
s.Kill(syscall.SIGKILL)
return ErrExecTimeout
case err = <-Go(s.Wait):
return err
}
}
func Go(f func() error) chan error {
ch := make(chan error)
go func() {
ch <- f()
}()
return ch
}
if timeout happends, go func() {ch <- f() }(), will never return, since
A receive from an unbuffered channel happens before the send on that channel completes.
Hello,
Can you please make a release with the current master code so it can be used with https://github.com/golang/dep
Thanks
When I run a command, that have Multiline-Output I get an Error:
.\main.go:6:21: newline in string
.\main.go:6:21: syntax error: unexpected newline, expecting comma or )
Line 6 in main.go is the import to this libary.
Hello!
i tried use this:
package main
import (
"github.com/codeskyblue/go-sh"
"log"
"fmt"
)
func main() {
port := uint(1)
session := sh.NewSession()
session.ShowCMD = true
out, err := session.Command("/Users/Ivan/Downloads/redis-3.2.3/src/redis-server", "--port", port).Output()
if err != nil {
log.Fatal("ERROR: ", err)
}
fmt.Println("output is", string(out))
}
And have result:
/usr/local/go/bin/go run /Users/Ivan/Documents/GolangSpace/src/examples/test.go
[golang-sh]$ /Users/Ivan/Downloads/redis-3.2.3/src/redis-server --port
*** FATAL CONFIG FILE ERROR ***
Reading the configuration file, at line 2
>>> 'port'
Bad directive or wrong number of arguments
2016/09/09 15:55:25 ERROR: exit status 1
exit status 1
I need to execute make
from go-sh, but I get this
configure: Setting lib to 'lib' (the default)
configure: Will try -pthread then -lpthread to enable POSIX Threads.
configure: CHECKS for site configuration
checking for gcc... gcc
checking whether the C compiler works... no
i.e. it can't find C compiler, I also checked $PATH and got an empty string
any idea how to make it work?
b, _ := sh.Command("echo", "$?").Output()
fmt.Println(string(b))
Im encountering a issue where a exit status 1 command gives output to the console.
This is a issue for me as i need a specific text to appear in the console rather than the command output (the reason for this is, its used for a monitoring check and expects a certain syntax).
The pseudo example code is:
`
package := "randompackage"
output, err := sh.Command("dpkg-query", "--showformat='${Version}'", "--show", package).Output()
fmt.Println(err)
`
This will result in the following output in the console
dpkg-query: no packages found matching randompackage <------------- This being the commands failing output
exit status 1 <------------- This being the fmt.println
Let me know if you want any other info
Is there anyway to mute the output ?
we may want to execute by admin
account or other account
Support such as "tail -f", "less"?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.