0

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 :(

2
  • I think you should work on simplifying this and making a minimal reproducible example that anyone with a Bash shell can run, and then post the full output from that script and say how it's wrong. I see a mv command moving the directory so you are probably just confused about where the directory got moved to. You can run ls ${2}_32MB at various points in the script to confirm that the directory exists. Put set -ue at the top for better error handling. Commented Mar 30, 2022 at 5:11
  • Please try shellcheck.net before asking for human assistance. The lack of quoting (which the tool will point out and fix) could produce problems if your input parameters contain whitespace or shell metacharacters. Does your problem still reproduce after you fix those things? Commented Mar 30, 2022 at 5:30

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome! It's not the first time I've seen mv cause a bug like this. It is because the command was designed to be ambiguous: you can't actually tell what mv a b is supposed to do by looking at it. Anyway, please click the checkmark to accept my answer.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.