分割命令cut -d中的分隔符忽略参数

3

我编写了一个脚本,可以获取可变数量的参数:

test.sh -i <input1> <input2> ... -o <output1> <output2> ...

我将参数解析如下:

我正在按照以下方式解析参数:

while [ $# -gt 1 ]; do
        TMP=$(echo "$@" | cut -d '-' -f 2)  #i <input1> <input2>
        TMP1=$(echo "$TMP" | cut -d ' ' -f 1)  #i
        CNT=$(echo "$TMP" | wc -w)  #3
        set -x
        case "$TMP1" in
            i)
                INPUTS=$(echo "$TMP" | cut -c 3-)
                shift "$CNT"
                ;;
            o)
                OUTPUTS=$(echo "$TMP" | cut -c 3-)
                shift "$CNT"
                ;;
        esac
done

每次都能正常工作,但文件名中包含“-”的文件除外。

例如:

./test.sh -i file1.txt file-2.txt -o out1.txt out-2.txt

有没有办法让cut命令忽略文件名中出现的分隔符?


我不知道 getopts。我得检查一下我正在使用的机器上是否可用。 - H_squared
为什么不从 $# 开始倒计时,在对每个参数运行 [ -f "$arg" ] 前,将其分割成“-”分隔的参数,然后不是在 $@ 上运行 cut 过滤器,而是在 "$input" 上运行它——$input 是您从所有“非文件”参数构造的新数组变量。如果这个问题以后没有解决,我会发布一个示例。 - hmedia1
1
getopts 对于需要多个参数的选项并不是很有效。 - chepner
2个回答

2

您不需要进行所有这些字符串操作;每个参数已经是一个单独的单词。

while (( $# > 0 )); do
  case $1 in
    -i) shift
        while [[ $# -gt 0 && $1 != -* ]]; do
            inputs+=( "$1" )
            shift
        done
        ;;
    -o) shift
        while [[ $# -gt 0 && $1 != -* ]]; do
            outputs+=( "$1" )
            shift
        done
        ;;
    *) echo "Unrecognized option $1"
       exit 1
       ;;
  esac
done

这段代码可以进行一些重构,以避免重复检查参数是否已用完。
for arg in "$@"; do
    case $1 in
      -i) mode=input; continue ;;
      -o) mode=output; continue ;;
    esac
    case $mode in
      input) input+=("$arg") ;;
      output) output+=("$arg") ;;
      *) echo "Unknown mode: $mode"
         exit 1
         ;;
    esac
done

你的解决方案简单易懂。我似乎把事情复杂化了一些。 - H_squared

0
这里有一种可能对某些人有益的替代方法。
事实上,参数解析总是一个权衡,因此将其量身定制到应用程序中是有好处的。这是一种相当通用的解决方案,允许在参数中进行一些错误检查和混乱。
它非常简单,但我已经添加了一些示例输出和注释,并为了可读性和兼容性而避免了复杂的方法来节省一两行代码(特别是在 if 语句中)。 示例用法:
bash #> touch file-1 file3 file4 file-8 file7
bash #> argparse -i file-1 file3 file4 -c -k --q --j -r -t -o file-8 file7

输出

输入文件:file-1 file3 file4
输出文件:file-8 file7
参数为:c k q j r t
正在处理参数“c” 正在处理参数“k” 正在处理参数“j”

脚本:

#!/bin/bash
#argparse

#Assign arrays

until [[ $# < 1 ]]; do
#ignore args "-i" and "-o", and tell the script to check for files following
    if [ "$1" == "-i" ] ; then unset output ; input=1 ; shift 
    elif [ "$1" == "-o" ] ; then unset input ; output=1 ; shift 
    fi
#Add input and output files to respective arrays
    if [ -f "$1" ] ; then 
        if [[ $input == 1 ]]; then 
            infiles+=($1)
        elif [[ $output == 1 ]]; then 
            outfiles+=($1)
        fi
    else
#Add args to array
        arg="$(echo "$1" | sed 's/-//g')"
        args+=($arg)
    fi
    shift
done

#Some debug feedback
echo -e "Input files: ${infiles[@]}\nOutput files: ${outfiles[@]}\nArgs are: ${args[@]}\n"

#Simulate actually "doing" something with the args
for arg in "${args[@]}" ; do 
    case $arg in
        "c") echo "Doing action for argument \"c\"" ;;
        "k") echo "Doing action for argument \"k\"" ;;
        "j") echo "Doing action for argument \"j\"" ;;
        *) ;;
    esac
done

更新/编辑:我刚意识到,原帖并没有要求解析除了-i-o之外的实际参数。但无论如何,这对某些人在某个时候仍可能有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接