Bash卷曲扩展与字符串中的空格

时间:2017-04-04 15:06:51

标签: bash scripting

我想使用卷曲扩展来节省一些打字。

我的愿望扩展是:

import {AppServices} from '...';
import {autoinject} from 'aurelia-framework';
@autoinject()
export class SomeViewModel {
    constructor(appServices) {
        this.appServices = appServices;
    }

    callGetInstallationId() {

        this.appServices.getInstallationID().then(function(id) {
            // the ID that is passed here is the one that is `accept`ed.
            // ie. if AppServices.getInstallationID calls accept('123'), then 
            // the '123' will be passed to this function as the first argument
        }, function(e) {
            // this function will be called if AppServices.getInstallationID
            // calls reject.  e will be the Error object, if any, that is passed
            // to the reject method.
        });

    }
}

我试过了:

-e uncore_imc0/cas_count_read/ -e uncore_imc1/cas_count_read/ -e uncore_imc2/cas_count_read/ -e uncore_imc3/cas_count_read/ -e uncore_imc4/cas_count_read/ -e uncore_imc5/cas_count_read/ -e uncore_imc6/cas_count_read/ -e uncore_imc7/cas_count_read/

但这只会扩展为(仅在开头使用-e)

-e uncore_imc{0..7}/cas_count_read/

如果我尝试过:

-e uncore_imc0/cas_count_read/ uncore_imc1/cas_count_read/ uncore_imc2/cas_count_read/ uncore_imc3/cas_count_read/ uncore_imc4/cas_count_read/ uncore_imc5/cas_count_read/ uncore_imc6/cas_count_read/ uncore_imc7/cas_count_read/

{-e, uncore_imc{0..7}/cas_count_read/}

两者都不会扩大。

2 个答案:

答案 0 :(得分:1)

您可以使用printf

printf -- '-e uncore_imc%d/cas_count_read/ ' {0..7}; echo

-e uncore_imc0/cas_count_read/ -e uncore_imc1/cas_count_read/ -e uncore_imc2/cas_count_read/ -e uncore_imc3/cas_count_read/ -e uncore_imc4/cas_count_read/ -e uncore_imc5/cas_count_read/ -e uncore_imc6/cas_count_read/ -e uncore_imc7/cas_count_read/

您还可以将此扩展存储在如下变量中:

printf -v arg -- '-e uncore_imc%d/cas_count_read/ ' {0..7}

答案 1 :(得分:1)

如果这是一个脚本,请不要使用大括号扩展。您只需编写一次代码,因此可读性应该是更高的优先级。改为创建一个数组:

opts=()
for((i=0; i < 8; i++)); do
  opts+=(-e "uncore_imc$i/cas_count_read/")
done

someCommand "${opts[@]}"