Home > Software design >  Shell Script: Directory is not created during for loop
Shell Script: Directory is not created during for loop

Time:03-30

I'm running a shell script for some testing and I'm having a weird issue. The script is meant to run tests and then grab the logs and move them to folders. Here's my code:

#!/bin/bash

# Variables
arg_1=$1        # $1 is argument PATH
arg_2=$2        # $2 is argument NAME

count=1

for i in 32 64 128
do
    # Case statement to go between different tests
    case $count in
        1) # test 1 (32MB test) ;;
        2) # test 2 (64MB test);;
        3) # test 3 (128MB test);;
        *) break ;;

    # Make directories
    mkdir ${2}_${i}MB    # Specific MB test
    mkdir ${2}_lat       # Latency logs
    mkdir ${2}_times     # Timings
   
    # Move logs here 
    for j in {1..10}
    do
        mv lat_log_$j ${2}_lat
    done

    for j in {1..5}
    do
        mv times_$j ${2}_times
    done
    
    # Move files
    mv ${2}_lat ${2}_times ${2}_${i}MB
    mv ${2}_${i}MB $2

    # Increment Count
    ((count  ))
done

So here's my problem: in the initial for loop when i = 32 is ran, the specific directory ${2}_32MB isn't being created. However, subsequent loops make the files just fine. So ${2}_64MB and ${2}_128MB are created normally. I'm not sure what the issue is :(

CodePudding user response:

Try adding 16 before 32 in for loop. Best guess its just skipping?

CodePudding user response:

If the directory $2 doesn't exist when the script starts running, then in the first iteration of the loop mv ${2}_${i}MB $2 will rename the directory you are trying to find to $2.

Then in later iterations, when that destination directory already exists, it will instead move a directory to be inside of $2.

  • Related