0

i am trying to run linux command to run python script:

for f in /mnt/data/service/fmriprep/geht/sub-P*; do (python 2_correlation.py -i "$f" -o /mnt/data/service/corr_graph/correlation_P*.csv ) 

my python script takes as input file in -i directory and return as -o a new .csv. I expect to return correlation_P* for each folder P* (P001, P002, P*) to this path (/mnt/data/service/corr_graph/correlation_P*.csv with the same P001,P002, P*), but stucked.

0

    2 Answers 2

    2

    For each iteration of the loop, you need to extract the digits after "P" and use them in the csv file name:

    in_dir=/mnt/data/service/fmriprep/geht out_dir=/mnt/data/service/corr_graph for dir in "$in_dir"/sub-P*; do num=${dir##*P} python 2_correlation.py -i "$dir" -o "$out_dir/correlation_P${num}.csv" done 
    6
    • Thanks! it should be "$in_dir": python 2_correlation.py -i "$in_dir" -o "$out_dir/correlation_P${num}.csv" done?CommentedOct 10, 2018 at 18:56
    • You tell me: is the argument to -i supposed to be "/mnt/data/service/fmriprep/geht" or "/mnt/data/service/fmriprep/geht/sub-P001"? The former is "$in_dir" the latter is "$dir"CommentedOct 10, 2018 at 18:59
    • -i supposed to be an /mnt/data/service/fmriprep/geht/sub-P001, so $dir isCommentedOct 10, 2018 at 19:09
    • I have one more question i also have .html files in in_dir, how could i filter it (i dont need to iterate iover them)?CommentedOct 10, 2018 at 20:02
    • before the num= line, put [ -d "$dir" ] || continue to validate that $dir is a directory.CommentedOct 10, 2018 at 20:06
    0

    Great thanks to @glenn jackman, you raised my skill. So command which worked for me is:

    for dir in /mnt/data/service/fmriprep/geht/sub-P*; do if [ -d "$dir" ]; then num=${dir##*P} && python 2_correlation.py -i "$dir" -o "/mnt/data/service/corr_graph/correlation_P${num}.csv";fi done 

      You must log in to answer this question.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.