ttファイルからリストを読み込み、変数としてfooループでPythonスクリプトを実行するシェルスクリプト

ttファイルからリストを読み込み、変数としてfooループでPythonスクリプトを実行するシェルスクリプト

txtファイルから変数を読み取り、forループでその変数を使用してPythonファイルを実行するシェルスクリプトを実行しようとしています。

  1. このコードは、var1、var2、var3....var 100など、少なくとも100個の変数を持つtxtファイルを読み取ります。

  2. このコードは、ディレクトリ(Rep1、Rep2)を変更して、各ディレクトリRepOut_Rep1.outから別のファイルを読み込みます。

  3. このコードはPythonコードを実行して別のディレクトリからエントリを取得します。

    python /home/PhytonFiles/phytonFold.py  -s /home/mySFiles/$var -o $RepOut_Rep1.out -n summaryFile.txt
    

次のコードを書きましたが、うまくいかないようです。

input="MyList.txt"
while IFS= read -r file; 
do
# printf '%s\n' "$file"
  
# run the script for the following directories (Rep1, Rep2. ...)
  for f in Rep1 Rep2
  do
  cd ${f}

#  pull the output file 
  outFile=RepOut_${f}.out

# create a summary folder at the end of the run
  summaryFile=summary_${f}_$file

# run the python file, get the inputs from /home/mySFiles/ with the variable $file
  phyton /home/PhytonFiles/phytonFile.py  -s /home/mySFiles/$file -o $outFile -n $summaryFile 

done 
done < "$input"

Pythonの実行行で使用されている変数が正しいかどうかはわかりません。私はどこに間違って行くことができますか?

ご協力ありがとうございます。

ビルカン

ベストアンサー1

実際にあなたが望むことをするのを妨げるものがあります。

  1. さらに、9行目からRep1にcdしてからforループが続くと、まだRep1にあるため、Rep2にcdできません。

したがって、フォルダ構造

./
  ./Rep1
  ./Rep2

Pythonスクリプトを実行したら、親フォルダに戻ります。しかし、正直なところ、cdコマンドを使用する理由がわかりません。 forループにフルファイルパスを作成するだけです。

  1. また、Pythonのスペルを誤って入力したため、Python行が実行されていない可能性があります。

  2. 次の変数ファイルを意味する場合:

VAR1 = a VAR2 = b VAR3 = c ...

その後、ファイルを読み込むよりもファイルをインポートしたいのですが、スクリプトに基づいて、ファイルは実際にはbash変数の変数ファイルではなくファイル名フォルダであると推測します。

file1.txt
file2.txt
file3.txt
...

この場合、ファイル名にスペースがあるかどうかはわからないため、常に$ fileを引用符で囲む必要があります。スペースがある場合、bashはPython行に次の内容を印刷します。

phyton /home/PhytonFiles/phytonFile.py  -s /home/mySFiles/some file name.txt -o $outFile -n $summaryFile 

これにより、Pythonが次のオプションを使用できると考えることができます。

-s /home/mySFiles/some
-o $outFile
-n $summaryFile

そしてこれらの主張は

file
name.txt

上記の変更を含むスクリプトのバージョンは次のとおりです。 :)

#!/bin/bash

input="MyList.txt"
while IFS= read -r file;
do
# printf '%s\n' "$file"

  # run the script for the following directories (Rep1, Rep2. ...)
  for f in Rep1 Rep2
  do

  # pull the output file
  outFile="RepOut_${f}.out"
  printf "\noutFile:\t%s\n" "$outFile"

  # create a summary folder at the end of the run
  summaryFile="summary_${f}_$file"
  printf "summaryFile:\t%s\n" "$summaryFile"

  # run the python file, get the inputs from /home/mySFiles/ with the variable $file
  echo python /home/PhytonFiles/phytonFile.py  -s "/home/mySFiles/$file" -o "$outFile" -n "$summaryFile"
  done
done < "$input"

また、この種の問題に関する良いヒントがありますが、Python行の前に「echo」という単語を置くことです。そのまま実行すると、行の各繰り返しがどのような様子かプレビューできます。

正しいと思われる場合は、最初から「echo」を削除して再実行すると、実際にコードが実行されます。

頑張ってください。

おすすめ記事