使用“set”命令的简单 bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7487569/
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
Simple bash script using "set" command
提问by alf
I am supposed to make a script that prints all sizes and file-names in the current directory, ordered by size, using the "set" command.
我应该制作一个脚本,使用“set”命令打印当前目录中的所有大小和文件名,按大小排序。
#!/bin/bash
touch /tmp/unsorted
IFS='@'
export IFS
ls -l | tr -s " " "@" | sed '1d' > /tmp/tempLS
while read line
do
##set probably goes here##
echo >> /tmp/unsorted
done < /tmp/tempLS
sort -n /tmp/unsorted
rm -rf /tmp/unsorted
By logic, this is the script that should work, but it produces only blank lines. After discussion with my classmates, we think that the "set" command must go first in the while loop. The problem is that we cant understand what the "set" command does, and how to use it. Please help. Thank you.
按逻辑,这是应该工作的脚本,但它只产生空行。和同学讨论后,我们认为在while循环中必须先执行“set”命令。问题是我们无法理解“set”命令的作用以及如何使用它。请帮忙。谢谢你。
回答by Karoly Horvath
ls -l | while read line; do
set - $line
echo
done | sort -n
or simply
或者干脆
ls -l | awk '{print , }' | sort -n
回答by Spencer Rathbun
Set manipulates shell variables. This allows you to adjust your current environment for specific situations, for example, to adjust current globbing rules.
Set 操作 shell 变量。这允许您针对特定情况调整当前环境,例如,调整当前的 globbing 规则。
Sometimes it is necessary to adjust the environment in a script, so that it will have an option set correctly later on. Since the script runs in a subshell, the options you adjust will have no effect outside of the script.
有时需要在脚本中调整环境,以便稍后正确设置选项。由于脚本在子 shell 中运行,因此您调整的选项在脚本之外不会产生任何影响。
This linkhas a vast amount of info on the various commands and options available.
此链接包含有关各种可用命令和选项的大量信息。

