【问题标题】:put two concatenatend var in a command - shell bash将两个连接变量放在一个命令中 - shell bash
【发布时间】:2016-05-25 08:32:53
【问题描述】:

我的脚本有问题。我有以下脚本:

while read line; do

    IFS=' '                                                         
                                                                    #sostituisco in IFS il carattere di default con quello di spazio
    i=0
    for campo in $line; do
        (( i++ ))
        if [[ $i -eq 1 ]] ; then                                    
            nome="$campo"
        elif [[ $i -eq 2 ]] ; then                                   
            cognome="$campo"
        fi
    done
    nome_file=$nome\_$cognome                                       
    echo $nome_file 
    j=$(find /home/ubuntu/Scrivania/Contatti -name 'nome_file*' | wc -l)
    echo $j     

done < $RUBRICA

文件 RUBRICA 包含这样的行:

andrea bargnani 6956959388 2632634643 2012/05/19

chris bosh 87654323234 78675432334 2014/06/16

zlatan ibrahimovic 2937485929 1938472639 2003/06/30

andrea mantovani 3402948586 0459687124 2015/01/25

andrea mantovani 3476589456 0451234567 2016/07/05

andrea mantovani 3478765434 67654334567 2011/09/10

marco polo 735636 36546456 2011/09/10

它在输入中使用一个文件 RUBRICA,并且对于每一行,将第一列保存在 var nome 中,将第二列保存在第二个 var cognome 中。所以,对于第一行,我将拥有

nome=andrea
cognome=bargnani

在 for 循环之后,脚本将两个带下划线的 var 连接到一个名为 nome_file 的新 var 中。然后我在命令中使用这个 var (nome_file)。问题是我不知道如何将此 var 放入命令中。语法是什么?

【问题讨论】:

    标签: string variables syntax find


    【解决方案1】:

    你可以像这样使用$nome_file

    j=$(find /home/ubuntu/Scrivania/Contatti -name "$nome_file*" | wc -l)
    

    ...但是如果 $RUBRICA 中有很多行,for 循环会很慢。

    改为使用awk,例如:

    awk '{printf "%s_%s\n", $1, $2}' $RUBRICA | while read nome_file; do
        echo $nome_file
        j=$(find /home/ubuntu/Scrivania/Contatti -name 'nome_file*' | wc -l)
        echo $j     
    done
    

    【讨论】:

    • 我试试你的第一个命令。它工作得很好,谢谢你是个天才。现在我尝试第二个
    • 我什至尝试了你的第二个建议,终端称我为 thi 错误:awk: line 1: runaway string constant "%s_%s\n, $ ...
    • 哎呀,错字了。我遗漏了结束"。固定。
    猜你喜欢
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 2022-10-19
    • 2011-12-19
    • 1970-01-01
    相关资源
    最近更新 更多