处理多个文件名的 Unix 脚本
我正在编写一个脚本,用于对文件中的文本进行折叠、排序和计数。我需要设计该程序,以便如果在命令行上给出多个文件名,它会一个接一个地单独处理每个文件名。我想我可以写一个循环,但我对这些还不太了解,所以如果可能的话想尝试其他选择。是否可以添加其他选项,以便可以在命令行中输入多个文件名?
如果测试 $# -lt 1 然后 echo "用法: $0 输入至少一个 DNA 文件名" 出口 菲 如果测试-r $* 然后 折叠 -w3 $* |排序| uniq-c|排序-k1,1nr -k2 否则
echo“用法:$*必须可读” 出口 菲尼娜
I'm writing a script that will fold, sort and count text in a file. I need to design the program so that, if it is given multiple filenames on the command line, it processes each one separately, one after the other. I think I could write a loop but I don't know that much about those yet so if possible would like to try other options. Are there other options that I can add to this so more than one file name can be entered in the command line?
if test $# -lt 1
then
echo "usage: $0 Enter at least one DNA filename"
exit
fi
if test -r $*
then
fold -w3 $* | sort | uniq -c | sort -k1,1nr -k2
else
echo "usage: $* must be readable"
exit
fi
Nena
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
for
循环在这里是合适的。以下形式用于迭代位置参数:这相当于更详细的版本:
for
loop will be appropriate here. The following form is used to iterate over positional arguments:This is equivalent to a more verbose version:
您可以使用
while
循环和shift
来逐一迭代命令行参数,如下所示:You can use a
while
loop andshift
to iterate through the command line arguments one by one as: