如何只使用脚本bash进入新目录

时间:2017-04-11 12:59:23

标签: bash shell

#!/bin/bash

#cicle 1
for fname in *.xlsx *csv
    do
            dname=${fname%.*}

            [[ -d $dname ]] || mkdir "$dname"
            mv "$fname" "$dname"
    done

    # In questo ciclo per ogni gene entra nella cartella e lancia i programmi di getChromosomicPositions.sh per avere
    # le posizioni nel genoma, e getHapolotipeStings.sh per avere le varianti

#cicle 2
    for geni in */; do
            cd $geni
            z=$(tail -n 1 *.csv | tr ';'  "\n" | wc -l) # geuarda nel file csv quante colonne ci sono e trasferisce il riusltato al programma getChromosomicPositions
            cd ..
            cp getHGSVposHG19.sh $geni
            cp getChromosomicPositions.sh $geni
            cp getHaplotypeStrings.sh $geni
            cd $geni
            pippo=$(basename $(pwd))
            export z
            export pippo
            ./getChromosomicPositions.sh *.csv
            export z
            ./getHaplotypeStrings.sh *.csv
            cd ..
    done

我有2个cicle的脚本,

我希望在第2章做的工作只为在cicle 1中创建的新目录编译程序。

我的意思是:

我有这个文件的主要目录:

pippo.xlsx pippo.csv

caio.xlsx caio.csv

topolino(目录)

minny(目录)

paperino(目录)

在第1章中,使导演pippo和caio

我希望在第2条我希望它出现在新目录(pippo,caio)中,并将cicle的所有工作仅限于新目录pippo和caio,而不是旧的directorys topolino,minny和paperino

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以使用新创建的目录创建一个数组,然后遍历此数组:

#!/bin/bash
set -o errexit # Exit the script in case of error

# Pre-declare and empty array.
declare -a new_directories;

# Save other scripts path for later use.
getHGSVposHG19="${pwd}/getHGSVposHG19.sh"
getChromosomicPositions="${pwd}/getChromosomicPositions.sh"
getHaplotypeStrings="${pwd}/getHaplotypeStrings.sh"

# Create missing directories
for fname in *.xlsx *csv
do
    dname=${fname%.*}
    if [ ! -d $dname ]
    then
        mkdir "$dname";
        new_directories+=("${dname}");
    fi

    mv "$fname" "$dname";
done

# Initialize only new directories
for geni in "${new_directories[@]}"
do
    pushd "${geni}"; # Add to directory stack

    export pippo="$(basename $(pwd))";
    export z=$(tail -n 1 *.csv | tr ';'  "\n" | wc -l)

    "${getChromosomicPositions}" *.csv;
    export z; # Is the previous script modifying z?

    "${getHaplotypeStrings}" *.csv;
    popd; #Remove from directory stack
done

我也冒昧地清理你的脚本,因为我不能按原样运行它,它可能有一些错别字/错误......

What is pushd and popd?