output = `ls` #(this is a comment) create variable out of command
if output.include? "Downloads" #if statement to see if command includes 'Downloads' folder
print "there appears to be a folder named downloads in this directory."
else
print "there is no directory called downloads in this file."
end
< h2 id = " script-add-to-your.bashrc-or。bash_profile-rlxx">脚本(添加到你的.bashrc或.bash_profile中) . sh > . sh
# capture the output of a command so it can be retrieved with ret
cap () { tee /tmp/capture.out; }
# return the output of the most recent command that was captured by cap
ret () { cat /tmp/capture.out; }
使用< h2 id = " usage-bji3 " > < / h2 >
$ find . -name 'filename' | cap
/path/to/filename
$ ret
/path/to/filename
如果您不将文件导出到任何地方,而只打算在本地使用它,则可以让Terminal设置函数声明。你必须在~/.bashrc文件或~/.profile文件中添加函数。在第二种情况下,你需要从Edit>Preferences>yourProfile>Command启用Run command as login shell。
创建一个简单的函数,比如:
get_prev() # preferably pass the commands in quotes. Single commands might still work without.
{
# option 1: create an executable with the command(s) and run it
#echo $* > /tmp/exe
#bash /tmp/exe > /tmp/out
# option 2: if your command is single command (no-pipe, no semi-colons), still it may not run correct in some exceptions.
#echo `"$*"` > /tmp/out
# option 3: (I actually used below)
eval "$*" > /tmp/out # or simply "$*" > /tmp/out
# return the command(s) outputs line by line
IFS=$(echo -en "\n\b")
arr=()
exec 3</tmp/out
while read -u 3 -r line
do
arr+=($line)
echo $line
done
exec 3<&-
}