Need help to create multiple file using shell script

HI,

i created the below script to create the multiple files, iam not getting the required output, Please advice.

#!/bin/sh
v_date=$1 # argument will come as daymonthyear eg : 151112
v_day=`echo $v_date | cut -c 1-2`
v_mon=`echo $v_date | cut -c 3-4`
v_year=`echo $v_date | cut -c 5-6`
if [ $v_mon -eq 1 -o $v_mon -eq 3 -o $v_mon -eq 5 -o $v_mon -eq 7 -o $v_mon -eq 8 -o $v_mon -eq 10 -o $v_mon -eq 12 ];then
v_last_day=31
elif [ $v_mon -eq 4 -o $v_mon -eq 6 -o $v_mon -eq 9 -o $v_mon -eq 11 ];then
v_last_day=30
elif [ $v_mon -eq 2 ];then
v_last_day=28
fi
if [ $v_day -le 9 ];then
touch SkyOffer_Details_0{$v_day..9}$v_monv_year.txt
touch SkyOffer_Details_{10..$v_last_day}$v_mon$v_year.txt
else
touch SkyOffer_Details_{$v_day..$v_last_day}$v_mon$v_year.txt
fi

Hint : touch SkyOffer_Detail_{8..30}1112.txt this command creates the multiple files as below, but i want to do it as dynamically, so iam going for a script

SkyOffer_Details_81112.txt
SkyOffer_Details_91112.txt
SkyOffer_Details_101112.txt
SkyOffer_Details_111112.txt
..............
..............
...........
SkyOffer_Details_291112.txt
SkyOffer_Details_301112.txt

required output :

if argument is 081112, i want 0 byte files as

SkyOffer_Details_081112.txt
SkyOffer_Details_091112.txt
SkyOffer_Details_101112.txt
SkyOffer_Details_111112.txt
..............
..............
...........
SkyOffer_Details_291112.txt
SkyOffer_Details_301112.txt

script execution : when i executed the above script as

./script_name 081112

executed Output : scripts is creating the zero byte file as
SkyOffer_Detail_{8..30}1112.txt

but i want the output as

SkyOffer_Details_081112.txt
SkyOffer_Details_091112.txt
SkyOffer_Details_101112.txt
SkyOffer_Details_111112.txt
..............
..............
...........
SkyOffer_Details_291112.txt
SkyOffer_Details_301112.txt

COuld anyone help me to solve this.

Thank you.

Jagadeesh

Try this..

change variables accordingly.

for i in {8..30}
do
K=$(printf "%02d" $i)
touch SkyOffer_Details_"$K"1112.txt
done
1 Like

Thank you for your replay, i will be knowing the values 8, 30 dynamically, its not working

its creating only 1 file as

touch SkyOffer_Details_081112.txt
for i in {$v_day..$v_last_day}
do
  K=$(printf "%02d" $i)
  touch SkyOffer_Details_"$K"1112.txt
done

For variables use..

for ((i=$v_day;i<=$v_last_day;i++))
do
K=$(printf "%02d" $i)
touch SkyOffer_Details_"$K"1112.txt
done
1 Like

In bash, you could simplify that script:

v_day=${1:0:2}
v_mon=${1:2:2}
v_year=${1:4:2}
months=( 0 31 28 31 30 31 30 31 31 30 31 30 31 )
v_last_day=${months[$v_mon]}
while [ "$v_day" -le "$v_last_day" ]
  do echo touch SkyOffer_Details_$((v_day++))$v_mon$v_year.txt
  done

You could save another line : while [ "$v_day" -le "${months[$v_mon]}" ]