Issue
I am trying to write a bash script which will fill a file in a for loop, such that if I run the script with argument 3 it will create the file with 3 lines.
This is the script that I came up with. But it creates the file with only one line.
#!/bin/bash
for i in $1
do
cat > $PWD/http_file$1.csv <<EOF
host_$i,8080,app_$i,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
EOF
cat $PWD/http_file$1.csv
done
Solution
for i in 3
: bash sees this as looping over all space-delimited options after in
. In this case, there is only one element, so the loop only runs once.
You can generate a list of numbers via seq
:
echo $(seq 1 3)
> 1 2 3
TL;DR: Use for i in $(seq 1 $1)
Answered By - jeremysprofile Answer Checked By - Candace Johnson (WPSolving Volunteer)