0

I'm running this script:

files=(*)
to_take=$((1*${#files[@]}/10))
x=${#files[@]};
for i in $(seq 1 $to_take);
  do
  #I've tried initially with the random comment
  # r=$((RANDOM%x));
  r=$(shuf -i 1-$((x)) -n 1);
  echo $r;
  # echo ${files[r]};
  echo $i;
done;

However, there's an issue. While echo $i is outputting all i's in range (hence the loop is working), but echo $r is just returning one number. The output is like:

r (the random no, eg: 45)
1
2
3
....
....
to_take

Kindly suggest what I'm doing wrong.

P.S.: I tried this:

r=100
for i in $(seq 1 $to_take); do echo $r; done;

The output is just

100

Instead of

100 to_take times.

What is the mistake I'm making?

Edit

A per comments I've used:

for ((i=0;i<to_take;i++))

This code seemingly works, although I shall confimr soon

Look, the issue in hand is to select "to_take" number of .wav files (voice recordings) from the total list of .wav files for training an Automated Speech Recognition System.

Sayantan
  • 315
  • 5
  • 20
  • What's the value of `to_take`? How many elements end up in the array `files`? – chepner Oct 17 '18 at 19:12
  • Perhaps more importantly, what's the value of `IFS`? – chepner Oct 17 '18 at 19:13
  • file arrray has 4672 files. to_take is 1/10th of the same, i.e. 467. What's IFS gotta do here? – Sayantan Oct 17 '18 at 19:15
  • 1
    `IFS` is what splits strings into words. When you do `for i in $(...)`, `IFS` controls which characters split different items in the string created by `$(...)` into individual values of `i`. (That's also why `for i in $(seq ...)` is generally not a great choice compared to, say, `for ((i=0; i – Charles Duffy Oct 17 '18 at 19:27
  • so basically, it seems to_take being 467 the kine would read: for i in $(seq 1 to 467). I get that it's IFS which s doing the range management thing. – Sayantan Oct 17 '18 at 19:28
  • Also, when you run `echo $r` instead of `echo "$r"`, the contents of `$r` are split on characters in IFS (again); each result of that splitting operation is expanded as a glob, and each result of that globbing operation is then passed as a separate argument to `echo`. Which is why it's much better to write `echo "$r"` instead of `echo $r`. – Charles Duffy Oct 17 '18 at 19:29
  • If `IFS` is unset or set to a non-whitespace character, among other options, than the first iteration of the loop sets `i` to the *entire* output of `seq`, not just the first line. – chepner Oct 17 '18 at 19:29
  • Anyhow, what's the actual intent here? If we knew what you were trying to accomplish, we could probably provide much better code that served the same purpose. Right now, you're telling us what your script's text is, and its behavior in practice, but not telling us what that script would do if it were actually functional. – Charles Duffy Oct 17 '18 at 19:30
  • ...I mean, if you were just taking a random 10% sampling of the files in the current directory, that would be something like, say, `files=( * ); readarray -d '' -t random_files < <(printf '%s\0' "${files[@]}" | shuf -z | head -z -n $(( ${#files[@]} / 10 ))); declare -p random_files` (to be safe with all possible filenames, using a very modern bash and GNU tool extensions). – Charles Duffy Oct 17 '18 at 19:33
  • ...note that in the above, `shuf` is only run *once* -- which is far cheaper/faster than running `shuf` over and over in a loop. – Charles Duffy Oct 17 '18 at 19:35
  • Ok. Let me check this code then, actually. I'll then work on those files in between. Copy these files from current folder to another folder. I'll add that line of code. I'm a nook at Bash..!!Eeh – Sayantan Oct 17 '18 at 19:37
  • If you want to shuffle the order of the files, why not simply use `find` to generate the listing to begin with? – David C. Rankin Oct 17 '18 at 19:38
  • I want to randomly select 10% of files and copy that to a new location – Sayantan Oct 17 '18 at 19:42
  • I tried using my codes to see of it works.. I used specific code to ask for the problem. Not a solution to the issue at hand. Hence, I wish it isn;t marked duplicate. – Sayantan Oct 17 '18 at 19:50

0 Answers0