I wrote the following bash script to print the top and bottom rows of a CSV file as a table.
#!/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)
By default, I set the number of top and bottom rows to be shown at 10. The script runs fine without the -n
flag. When I specify the flag, however, my echo
shows that num
has been set correctly, but I get the following errors:
tail: option requires an argument -- 'n' Try 'tail --help' for more information. head: option requires an argument -- 'n' Try 'head --help' for more information.
It seems that num
isn't being seen by either tail
or head
. Even if I stick an echo
right before that final command, I can see that num
is set correctly, but clearly something is wrong. Why am I receiving these errors?
PS I use this CSV file for my testing.
Prompted by Cyrus' helpful advice, I get the following in debug mode (where ht
is the name of my script):
./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.
Where is that extra trailing -n
coming from?!
没有评论:
发表评论