Need help to create multiple file using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help to create multiple file using shell script
# 1  
Old 11-29-2012
Linux 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.

Code:
#!/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
Code:
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
Code:
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
Code:
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

Last edited by Scott; 11-29-2012 at 01:26 AM.. Reason: Please use code tags
# 2  
Old 11-29-2012
Try this..

change variables accordingly.

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

This User Gave Thanks to pamu For This Post:
# 3  
Old 11-29-2012
Quote:
Originally Posted by pamu
Try this..

change variables accordingly.

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


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

its creating only 1 file as
Code:
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


Last edited by Scott; 11-29-2012 at 02:23 AM.. Reason: PLEASE use code tags
# 4  
Old 11-29-2012
For variables use..

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

This User Gave Thanks to pamu For This Post:
# 5  
Old 11-29-2012
In bash, you could simplify that script:
Code:
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]}" ]

Last edited by RudiC; 12-01-2012 at 05:28 AM.. Reason: Save another line .
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create file from script shell

Hello all :) Here is my code i try to complete: address1="$(ssh root@$machine -x "lxc-info -n $machine-worker1 -H -i")" if //ifthe file addrfile does not exist then create the file addrfile echo "$address1">"$addrfile" fi "$address1">"$addrfile" How, can i... (4 Replies)
Discussion started by: chercheur111
4 Replies

2. Shell Programming and Scripting

Create a UNIX script file with multiple commands

Hi Good morning all, I want to create script file with multiple commands. For ex: pmrep connect is one of the command to connect to repository. pmrep objectexport is another command to export objects to a file. These commands should run sequentially.But when i try to execute this, the first... (4 Replies)
Discussion started by: SekhaReddy
4 Replies

3. UNIX for Dummies Questions & Answers

How to Create excel file(.csv) using shell script?

Hi, i have shell script which compiles n number of test cases and execute them one by one. i want to create report in excel through script in which two columns namely "test id" and "release".second column have two subcolumns namely compiles and excutes. so i want first column should display test... (15 Replies)
Discussion started by: RamMore123
15 Replies

4. Shell Programming and Scripting

How to create a shell script to read a foldername from a text file and go to the folder?

Hi, I am trying to write a shell script which can read folder names from a text file and then go to the folder and picks up a xml file and write on my sipp script so that I can run the sipp script. For example: I have a text file called thelist.txt where I have provided all the folders... (7 Replies)
Discussion started by: pm1504
7 Replies

5. Programming

help need in the perl script that create one xml file form multiple files.

Hi every one, Please excuse me if any grammatical mistakes is there. I have multiple xml files in one directory, I need to create multiple XML files into one XML file.example files like this</p> file1:bvr.xml ... (0 Replies)
Discussion started by: veerubiji
0 Replies

6. Shell Programming and Scripting

Create shell script to extract unique information from one file to a new file.

Hi to all, I got this content/pattern from file http.log.20110808.gz mail1 httpd: Account Notice: close igchung@abc.com 2011/8/7 7:37:36 0:00:03 0 0 1 mail1 httpd: Account Information: login sastria9@abc.com proxy sid=gFp4DLm5HnU mail1 httpd: Account Notice: close sastria9@abc.com... (16 Replies)
Discussion started by: Mr_47
16 Replies

7. Shell Programming and Scripting

Shell script to create multiple OpenSSL Certificates

I need to create a script that will generate a bunch of OpenSSL Certificates signed by my own CA. The certificates being generated are for testing purposes only. But what I need is the following Root CA 512 768 1024 1280 1536 1792 2048 4096 I need basically 64 combinations. Each... (4 Replies)
Discussion started by: krisarmstrong
4 Replies

8. Shell Programming and Scripting

A shell script for create a a file in the respective path

Hello forum members, I have to create a out file in the current path./aaa/bbb/ccc/hhh. i am writing script below. ###script Begins##### #!/bin/ksh echo "Weclome" if then echo "Hello" rm -rf $aaa/bbb/ccc/hhh #clean the exsisting o/p file echo "no... (2 Replies)
Discussion started by: rajkumar_g
2 Replies

9. Shell Programming and Scripting

how to create csv file using shell script

I have a file in multiple directory which has some records in the following format File: a/latest.txt , b/latest.txt, c/latest.txt -> Name=Jhon Age=27 Gender=M Street=LA Road Occupation=Service I want to generate a csv file from the above file as follows File: output.csv -> ... (9 Replies)
Discussion started by: rjk2504
9 Replies

10. Shell Programming and Scripting

Need a Shell script to create Multiple User Accounts

Hi All, Am New to shell scripting , Can u please Help me to Create a shell script which Creates Multiple Users (say up to 250 users) ,am using Rehat server 5 enterprise Edition .. I am really in need of this script So tat i can save time and effort for this Job .. KIndly help me Please ... (1 Reply)
Discussion started by: rksubash
1 Replies
Login or Register to Ask a Question