简体   繁体   中英

Printing to multiple files with shell script for loop variable

None of the files are showing up, I tried putting the file to be written to in quotes but just no files are being written all I am getting is one file called person.txt

   #!/bin/sh
    cut -f 1 $1 > temp1.txt
    cut -f 2-3 $2 > temp2.txt
    for ((i=3;i<103;i++)); do
            cut -f $i $1 > temp3.txt
            paste temp1.txt temp2.txt temp3.txt > $HOME/Desktop/Plots/person$iPlot.txt
    done

Without having tested it, a problem in your script is

 $HOME/Desktop/Plots/person$iPlot.txt

Bash is not going to substitute your variable i as you are expecting, but tries to resolve a variable iPlot instead. As this variable has not been assigned any value, you'll end up with person.txt . This happens as bash tries to resolve variables as entire words separated eg by space or period.

To make sure that your variable i is used, try

$HOME/Desktop/Plots/person${i}Plot.txt

instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM