Few doubts on Linux scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Few doubts on Linux scripting
# 1  
Old 07-26-2010
Few doubts on Linux scripting

1) I have to create 3 scripts which create different output files. Script one creates datafiles, script2 creates control files and script3 creates flg files. The business wants the same timestamp to be attached to all the files created by these scripts.

Script1- Create data files - Data files name format-> xxxx.01122010_223048.dat
Script2- Create Control files - Control files name format -> yyyy.01122010_223048.ctl
Script3 - Create flg files- flg files name format-> zzzz.01122010_223048.flg


How can I define a variable which gives the timestamp once and then can be used in all the three scripts?

These scripts are called from an intial script as follows
./$POS_SCRIPT/dw_postodw_pharmacysplit.ksh
./$POS_SCRIPT/dw_postodw_controlfile.ksh
./$POS_SCRIPT/dw_postodw_cpfiles.ksh

How do I check the return status of the previous script before starting the next If the return status is not success, I need to write that to a logfile and exit in the succeeding scripts.
-----------------------------------------

2)I have an input file(named common_files) which contains a set of table names. I need to search the current directory to check if the filename contain the table names from the input file. Move all those files to another directory.

For example the filename is of the format XXXX_<tablename>_timestamp. If the tablename matches from input file, those files should be moved to another directory.

Can I do the grep and move with a single command in my scripting? I got stuckup as below.
Code:
if [[ -s common_files]]
then
while read line 
do 
 
'grep -l $line * | mv 
done <common_files 
fi

--------------------------------------
3)I have another input file which contains a list of pharmacyids(This is an input file contains around 450 pharmacy ids). I need to search a set of flatfiles(119 files) to split them based on the 450 ids.Means each of these 119 files need to be split into 450 files based on the presence of the pharmacy ids. The postion of pharmacy id in the 119 files vary from file
to file.I need to search for the id as a whole word and copy the matching lines to outputfiles. So a maximum of 119 * 450 output files may be generated.

INPUT FILE -> a file contains a list of 450 pharmacyids
flat FILE -> 119 files whcih may conatin the 450 pharmacy ids , but position of ids vary
OUTPUT FILES->the files created from flat files by copying the matching lines for each id.

The below one is my script. Can somebody review and give suggestions on logic and performance?(I heard 'cat' and grep used together will slow down the performance especially this may be executed 119 * 450 times).
Code:
for pharmacyf in `ls`
do
$tablename=` $pharmacyf | cut –f3 -d'.' `
 
while read pharmacyid
do
cat $pharmacyf | grep -w $pharmacyid >> $POSOUT/ODS.POS.$pharmacyid.$tablename.$CURRENT_DATE
 
done<inputfile
done


Thanks
Maya

Moderator's Comments:
Mod Comment Use code tags and maybe indention please.

Last edited by zaxxon; 07-26-2010 at 05:16 AM..
# 2  
Old 07-26-2010
Those are some long questions Smilie
I'll attempt the first. It sounds fairly simple.
You need to get the current time stamp in your initial script and pass that as an argument to your 3 scripts.
Code:
TIMESTAMP=`date +%H%M%S`
#Or whichever format you prefer it in
./$POS_SCRIPT/dw_postodw_pharmacysplit.ksh $TIMESTAMP
./$POS_SCRIPT/dw_postodw_controlfile.ksh $TIMESTAMP
./$POS_SCRIPT/dw_postodw_cpfiles.ksh $TIMESTAMP

In each of these 3 scripts, you would write:
Code:
TIMESTAMP=$1

That should give you the common time stamp throughout all 3 scripts.

Regarding your second sub question, error handling should be done within the files itself. If you encounter an error, you could choose to exit with a specific error code (You can have an error code to error text mapping somewhere).

Something like:
Code:
exit 1

You can handle the return status best in your initial script itself.

Code:
TIMESTAMP=`date +%H%M%S`
#Or whichever format you prefer it in
RETURN_CODE=`$POS_SCRIPT/dw_postodw_pharmacysplit.ksh $TIMESTAMP`
if [ $RETURN_CODE -ne 0 ]
then
#ECHO ERROR TO LOG FILE
fi RETURN_CODE=`$POS_SCRIPT/dw_postodw_controlfile.ksh $TIMESTAMP` if [ $RETURN_CODE -ne 0 ] then
#ECHO ERROR TO LOG FILE
fi RETURN_CODE=`$POS_SCRIPT/dw_postodw_cpfiles.ksh $TIMESTAMP` if [ $RETURN_CODE -ne 0 ] then
#ECHO ERROR TO LOG FILE
fi

# 3  
Old 07-29-2010
Thank you so much.

Maya
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on bash scripting in Linux

Could anyone help me with the below scripting task. my requirement is as below 1. I have two directories /var/tmp/dir1 & /var/tmp/dir2 once i run the script test.sh it should ask as below. Please choose the below path 1 (or) 2 1. /var/tmp/dir1 2. /var/tmp/dir2 else. exit 2. once i... (4 Replies)
Discussion started by: praveenkumar.v
4 Replies

2. Shell Programming and Scripting

Practice Linux Scripting

hey everyone, does anyone here have any good sites, or book recommendations that give you practice scripts to write? It's hard for me to think of scripts that I should write for practice. I've written a small backup script, but I'd rather have a source that gives me ideas, and practice for... (1 Reply)
Discussion started by: Lost in Cyberia
1 Replies

3. UNIX for Dummies Questions & Answers

Linux kernel modules makefiles doubts

This query is regarding the makefiles of linux kernel modules. I saw at some sites on net it is suggesting to include the following path: KERNEL_SOURCE := /usr/src/linux... while at some places it is askibg to include /lib/modules path: KERNEL_SOURCE := /lib/modules/2.6.27-7-generic/build... (0 Replies)
Discussion started by: rupeshkp728
0 Replies

4. Solaris

Unix scripting doubts

i hav a file like this -rwxr-xr-x 1 root controlm 4141 Nov 13 2006 /opt/scripts/prod/fvaauditlos.ksh -rwxr-xr-x 1 root controlm 3958 Nov 13 2006 /opt/scripts/prod/fvaexpchbk1.ksh -rwxr-xr-x 1 root controlm 6471 Nov 13 2006 /opt/scripts/prod/fvaexpchbk2.ksh... (24 Replies)
Discussion started by: p_satyambabu
24 Replies

5. Shell Programming and Scripting

shell scripting doubts

Hello All, I am working in a reputed software firm,currently working on testing platform (manual and automation).But I am very much intersted in unix and shell scripting,I feel that I am good at beginner level of shell scripting,still I want to practise shell scripting to become excel in it.I... (1 Reply)
Discussion started by: maheshglm
1 Replies

6. Programming

Doubts about timers in linux kernel

Hi , I am trying to learn timers in linux kernel. I am trying to write a program where I can configure a timer to tick in every 5 seconds and a function should thus exicute in every five seconds. I tried one program with the help of linux/timer.h headerfile but I couldnt get the... (4 Replies)
Discussion started by: iamjayanth
4 Replies

7. Shell Programming and Scripting

Unix Scripting on Linux 9.

I am NEW to Unix. I studied a few Unix Shell Commands 8 years ago in IBM AIX machines. I want to learn/brush up Unix shell commands, therefore I just installed Red Hat Linux 9 in a P3 machine. But I do not know where to start Unix Scripting in that Linux. I chose Desktop / Personnel while ... (2 Replies)
Discussion started by: risuresh
2 Replies

8. Red Hat

qmail configuration in redhat linux - doubts

hi, How to Install & configure the qmail in redhat linux? and also how to get the free qmail package? I know the sendmail configuration in redhat linux. What is the features of qmail compared with sendmail? Regards, Jones (3 Replies)
Discussion started by: jones_linux
3 Replies

9. Programming

Doubts About C Compiler In Unix/linux

As all of you know whenever a program "say a.c" is complied in linux using the gcc or the cc compiler..it shows the list of errors ( if the program contains any).. i want to modify the compier script so as to list the no of errors only and not the description about the error..like" parse error " ... (1 Reply)
Discussion started by: vrs
1 Replies
Login or Register to Ask a Question