bash - bash 标志设置的变量不能被其他命令使用
问题描述
我编写了以下 bash 脚本以将 CSV 文件的顶部和底部行打印为表格。
#!/usr/bin/env bash
# Default argument
num=10
# Get flag values
while getopts ":n:" opt; do
case $opt in
n)
# Get argument values
num=$OPTARG
# Print to check
echo $num
;;
esac
done
column -t -s , <(head -n $((num+1)) $1) <(tail -n $num $1)
默认情况下,我将顶部和底部的行数设置为 10。脚本在没有-n
标志的情况下运行良好。但是,当我指定标志时,我的echo
节目num
已正确设置,但出现以下错误:
tail: option requires an argument -- 'n'
Try 'tail --help' for more information.
head: option requires an argument -- 'n'
Try 'head --help' for more information.
似乎num
没有被tail
或看到head
。即使我echo
在最后一个命令之前右击,我也可以看到它num
设置正确,但显然有问题。为什么我会收到这些错误?
PS 我使用这个 CSV 文件进行测试。
在 Cyrus 的有用建议的提示下,我在调试模式下得到以下信息(ht
我的脚本名称在哪里):
./ht -n 5 sealevels.csv
+ num=10
+ getopts :n: opt
+ case $opt in
+ num=5
+ echo 5
5
+ getopts :n: opt
+ echo 5
5
+ column -t -s , /dev/fd/63 /dev/fd/62
++ head -n 6 -n
++ tail -n 5 -n
head: option requires an argument -- 'n'
Try 'head --help' for more information.
tail: option requires an argument -- 'n'
Try 'tail --help' for more information.
额外的尾随-n
是从哪里来的?!
解决方案
推荐阅读
- python - 如何在 sklearn 中进行多词标记化?
- javascript - LabeledMap 和 Map 相等
- gam - GAM 仪表板 - 有没有办法阻止广告在特定页面或 URL 上显示?
- c# - 如何将 Unity C# 结构发送到 Objective-C 插件?
- javascript - 如何正确使用 intl.formatRelativeTime()
- node.js - MognoDB Nodejs 通过 ID 获取特定的集合文档
- rust - 是否可以等待 &dyn Future?
- ios - 如何从 iOS 14 中的 UIDocumentPickerViewController 获取文件名和文件大小?
- python - 求解输入代数方程python
- python - 将“文件”类型的文件读入熊猫数据框的最简单方法