0

I have the following bash script use case that I hope is possible. There's command which allows me to execute it with a couple of arguments.

unique_command -n <integer> -f <foldername>

The creation of the script involves looping the integer value decreasingly and have a hard stop at a certain number. e.g. the integer starts from 100 and hard stop at 10. I've included a variable DECREMENT_VALUE as a means of setting the interval between each integer loop. Below is a section of what I've come up so far:

main_loop.sh

#!/bin/bash
## --- Variable Assignment --- ##
FOLDER_NAME=folder1
START_NUM=100
STOP_NUM=10
DECREMENT_NUM=5    

## --- Script --- ##
COUNT=${START_NUM}
while [ $COUNT -gt ${STOP_NUM} ]; do
  echo "unique_command -f ${FOLDER_NAME} -n $COUNT"
  COUNT=$(($COUNT - ${DECREMENT_NUM}))
done

output

$ ./test.sh
unique_command -f folder1 -n 100
unique_command -f folder1 -n 95
unique_command -f folder1 -n 90
unique_command -f folder1 -n 85
unique_command -f folder1 -n 80
unique_command -f folder1 -n 75
unique_command -f folder1 -n 70
unique_command -f folder1 -n 65
unique_command -f folder1 -n 60
unique_command -f folder1 -n 55
unique_command -f folder1 -n 50
unique_command -f folder1 -n 45
unique_command -f folder1 -n 40
unique_command -f folder1 -n 35
unique_command -f folder1 -n 30
unique_command -f folder1 -n 25
unique_command -f folder1 -n 20
unique_command -f folder1 -n 15

However, I have multiple folder names and different corresponding integer value for each folder. I want to take one step further to automate the script. The idea is to have a parameter file that contains the following folder name and its corresponding integer so that it can be executed within the loop without the user manually changing the variables assigned. Note: This parameter file has spaces in between a particular folder and its corresponding integer value, and a newline for each folder-integer pair

parameter_file.txt

folder1  100
folder2  995
folder3  200

How can I feed the values from this parameter file into the main_script, executing the command and looping the integer value for each iteration?

Expected output

$ ./test.sh
unique_command -f folder1 -n 100
unique_command -f folder1 -n 95
unique_command -f folder1 -n 90
.
.
.
unique_command -f folder1 -n 10
unique_command -f folder2 -n 995
unique_command -f folder2 -n 990
unique_command -f folder2 -n 985
.
.
.
unique_command -f folder2 -n 10
unique_command -f folder3 -n 200
unique_command -f folder3 -n 195
unique_command -f folder3 -n 190
.
.
.
unique_command -f folder3 -n 10

Edit: unique-command is just an example which takes in 2 arguments. This is not an actual bash command.

moonyaero
  • 1
  • 1
  • What `bash` version are you using? I'm on 5.0.17 and my `command` has neither `-f` nor `-n`. – Ted Lyngmo Nov 04 '20 at 08:08
  • The command is a software-specific command that allows me to add arguments to it. It is not the default bash "command" per-say. The problem still remains the same though. Apologies if there's any confusion in my writeup. – moonyaero Nov 04 '20 at 08:25
  • The duplicate explains most of it. You can also use `seq` to generate the sequences, like [this](https://pastebin.com/pirPWP1L) – Ted Lyngmo Nov 04 '20 at 08:37

0 Answers0