Linux 使用 shell 命令的返回值作为 shell 脚本中的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13003300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use return value of a shell command as a value in shell script?
提问by erogol
Possible Duplicate:
Capturing multiple line output to a bash variable
可能的重复:将
多行输出捕获到 bash 变量
For example I want to run ls command and take the return list as a value kept in a array in shell script.
例如,我想运行 ls 命令并将返回列表作为保存在 shell 脚本数组中的值。
Something like
就像是
run
跑
#ls
fileA
fileB
fileC
kept this return list in a variable that keeps a array
将此返回列表保存在保存数组的变量中
variable A = ["fileA","fileB","fileC"];
I cannot give the exact notation for code since I do not know how to write shell script. After I learn this, I 'll.
我无法给出代码的确切符号,因为我不知道如何编写 shell 脚本。学完这个,我会的。
回答by Jonathan Leffler
variableA=$(ls)
echo "$variableA"
回答by dearN
#!/bin/bash
variableA=$(ls)
echo $variableA
That should be your shell script assuming that you have bash
这应该是你的 shell 脚本,假设你有 bash
Then all you'd need to do is chmod +x shell_script
to make it executable.
然后你需要做的就是chmod +x shell_script
让它可执行。
If you use ls > contents.file
the result of ls
is saved to a file called contents.file
.
Remember, >
rewrites the entire file while >>
appends to the last line.
如果使用ls > contents.file
,结果将ls
保存到名为contents.file
. 请记住,>
在>>
附加到最后一行时重写整个文件。