0

For testing i create at my Linux-System the file list.txt. In there are numbers like:

4 1 5 2 3

(sometime there are more or less numbers)

How do i run these numbers with fallocate?

fallocate -l 1stnumberG file1
fallocate -l 2ndnumberG file2
fallocate -l 3rdnumberG file3
....

i think about something like

declare -i counter ; counter=1 ; for i in "'seq 1 $(wc -w list.txt | cut -c 1)'" ; do fallocate -l ${i}G "file$counter" ; (( ++ counter )) ; done
fallocate: unerwartete Anzahl an Argumenten

declare -i counter ; counter=1 ; for i in "'seq 1 $(wc -w list.txt | cut -c 1)'" ; do fallocate -l ${i}G "file$counter" ; (( ++ counter )) ; done < $(cat list.txt) 
V: $(cat list.txt): Mehrdeutige Umlenkung.

but i didt work....

4
  • Instead of just going "it doesn't work", split the task into pieces, and try to figure out which parts do work, and how (and why) the others fail. Then fix them one by one. E.g. does the for-loop loop over the correct numbers? Maybe try with just echo $i? If not, what does it loop over and what does that tell you? Does the innermost command substitution (wc | cut) print the right thing? If not, consider what the commands there do individually, and if that's what you want here.
    – ilkkachu
    Commented Jul 14 at 22:56
  • echo $i say 'seq 1 5'. dont know the right way how to give fallocate the numbers from list.txt
    – user447274
    Commented Jul 14 at 23:10
  • What does the fact that echo $i returns the text of the command tell you? That Bash didn't execute what it sees as a text. You already used command substitution, which is a way to execute text as a command. Also drop the single quotes, otherwise Bash will look (and most probably fail) for command literally named seq 1 5, with spaces, if substitution is used outside the quotes. Commented Jul 15 at 5:02
  • um, that < $(cat list.txt) looks like it was stiched together from random parts. Command substitution (essentially) puts the output of the command on the command line, so ls file.txt is the same as ls $(echo file.txt), and I don't think you had a filename in list.txt. If you want to redirect the contents of the file to the loop, you just need < list.txt. But there's nothing in the loop to read stdin anyway. I might suggest reading e.g. mywiki.wooledge.org/BashGuide or another guide on shell scripting.
    – ilkkachu
    Commented Jul 15 at 8:55

1 Answer 1

2

If you're trying to loop over a sequence of whitespace separated numbers in a single line of list.txt then one way to do so in the bash shell would be to read them into an indexed array:

read -a numbers <list.txt

Then you can do something like

for i in "${!numbers[@]}"; do 
  echo fallocate -l "${numbers[i]}" "$(printf 'file%02d' "$((i+1))")"
done
fallocate -l 4 file01
fallocate -l 1 file02
fallocate -l 5 file03
fallocate -l 2 file04
fallocate -l 3 file05

Some other shells have similar array read capabilities with minor syntax differences e.g. zsh read -A.

2
  • works fine, but if there are 10 or more numbers, the files are numberd with file1, file2 ... up to file9 and than file10, thats oke. but is there a way to work with file01, file02 ... up to file09 and than file10 ?
    – user447274
    Commented Jul 15 at 12:07
  • @user447274 you can use printf to format numbers with padding - please see revised answer Commented Jul 15 at 12:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .