Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要编写一个 bash 脚本,按排序顺序打印出它的命令行参数,每行一个。
我写了这个脚本,它工作正常,但还有其他方法吗?特别是没有将其输出到文件和排序。
#!/bin/bash for var in $* do echo $var >> file done sort file rm file
程序试运行:
$ script hello goodbye zzz aa aa goodbye hello zzz
您可以通过管道对 for 循环进行排序,或者只是执行
printf '%s\n' "$@" | sort
#!/bin/bash for var in "$@"; do echo "$var" done | sort
您想使用$@引号(而不是$*)来容纳带空格的参数,例如
$@
$*
script hello "goodbye, cruel world"
管道摆脱了对临时文件的需要。