bashdoublefloating

Is there a way to change floating to whole number in for loop in bash


I have a bash loop that I run to copy 2 files from the hpc to my local drive recursively over the processors and all the timesteps. On the hpc the timesteps are saved as

1 2 3

whereas the bash loop interprets it as

1.0 2.0 3.0

probably because of the 0.5 increment. Is there a way to get the $j to be changed to whole number (without the decimal) when running the script?

Script I use:

for i in $(seq 0 1 23)
do
    mkdir Run1/processor$i
    for j in $(seq 0 0.5 10);
    do
        mkdir Run1/processor$i/$j
        scp -r xx@login.hpc.xx.xx:/scratch/Run1/processor$i/$j/p Run1/processor$i/$j/
        scp -r xx@login.hpc.xx.xx:/scratch/Run1/processor$i/$j/U Run1/processor$i/$j/
    done
done

Result:

scp: /scratch/Run1/processor0/1.0/p: No such file or directory

The correct directory that exists is

/scratch/Run1/processor0/1

Thanks!


Solution

  • well, yes!

    but: Depending on what the end result is.

    I will assume you want to floor the decimal number. I can think of 2 options:

    1. pipe the number to cut
    2. do a little bit of perl
    for i in $(seq 0 1 23); do
      for j in $(seq 0 0.5 10); do
    # pipe to cut
        echo /scratch/Run1/processor$i/$(echo $j | cut -f1 -d".")/U Run1/processor"$i/$j"/
    # pipe to perl
        echo /scratch/Run1/processor$i/$(echo $j | perl -nl -MPOSIX -e 'print floor($_);')/U Run1/processor"$i/$j"/
      done
    done
    

    result:

    ...
    /scratch/Run1/processor23/9/U Run1/processor23/9/
    /scratch/Run1/processor23/9/U Run1/processor23/9.5/
    /scratch/Run1/processor23/9/U Run1/processor23/9.5/
    /scratch/Run1/processor23/10/U Run1/processor23/10/
    /scratch/Run1/processor23/10/U Run1/processor23/10/
    

    edit :

    Experimented a litle, found another way:

    echo /scratch/Run1/processor$i/${j%%.[[:digit:]]}/U Run1/processor"$i/$j"/