Shell.@esc_cmd
— Macro@esc_str -> String
Help you escape special characters for the shell.
Examples
julia> files = ["temp file 1", "temp file 2"]
2-element Array{String,1}:
"temp file 1"
"temp file 2"
julia> filelist = esc`$files.txt`
"'temp file 1.txt' 'temp file 2.txt'"
julia> Shell.run("touch $filelist")
julia> Shell.run("rm $filelist")
Be careful, the escape treat space separated terms individually. Put them into a varible to get properly escaped.
Examples
julia> esc`temp file 0.txt`
"temp file 0.txt"
julia> file = "temp file 0.txt"
"temp file 0.txt"
julia> esc`$file`
"'temp file 0.txt'"
- Not working for
cmd
in Windows because it treats single quotes differently.
Shell.run
— Methodrun(cmd::AbstractString; shell=SHELL, capture=CAPTURE, chomp=CHOMP,
dryrun=DRYRUN, source=SOURCE)
Run your command string in shell.
Examples
julia> using Shell
julia> Shell.run(raw"echo $SHELL", capture=true)
"/bin/zsh"
julia> Shell.run(raw"for i in bu fan; do echo $i; done")
bu
fan
julia> files = ["temp file 1", "temp file 2"]
2-element Array{String,1}:
"temp file 1"
"temp file 2"
julia> filelist = esc`$files.txt`
"'temp file 1.txt' 'temp file 2.txt'"
julia> Shell.run("touch $filelist")
julia> Shell.run("ls > 'temp file 0.txt'")
julia> Shell.run("cat 'temp file 0.txt' | grep temp")
temp file 0.txt
temp file 1.txt
temp file 2.txt
julia> Shell.run("rm 'temp file'*")
- You should properly escape all special characters manually.
- use
dryrun=true
to check the command to be run without actually running. - To capture output, set
capture=true
. - To avoid escaping
$
everytime, you can use raw string, likeraw"echo $PATH"
- You can change the default shell (
zsh
in linux andcmd
in windows) usingsetshell("other_shell")
. - In Windows, shell should be
cmd
orpowershell
.
Shell.setchomp
— Methodsetchomp(chomp::Bool)
Set whether chomp the output (default is true).
Shell.setiscapture
— Methodsetiscapture(capture::Bool)
Whether capture the rc file (e.g. .zshrc
) before run script (default is true).
Shell.setisdryrun
— Methodsetisdryrun(dryrun::Bool)
Whether dryrun the rc file (e.g. .zshrc
) before run script (default is true).
Shell.setissource
— Methodsetissource(source::Bool)
Whether source the rc file (e.g. .zshrc
) before run script (default is true).
Shell.setshell
— Methodsetshell(shell::AbstractString)
Specify which shell to use (Windows defaults to cmd
, and zsh
otherwise).