LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-25-2012, 01:06 AM   #1
fernfrancis
Member
 
Registered: Feb 2009
Location: Goa(India)-Sharjah(UAE)
Distribution: RHEL,centos,fedora,ubuntu
Posts: 234

Rep: Reputation: 18
help tune bash script


HI Guys

The bash script below runs on a daily basis , i have issue when it comes to the end of the month. in short when i am on the 1 of the next month the script cant find the file which is 30 or 31 file of the previous month.

if anyone can help me tune the script to meet my everyday requirements would be great.


#!/bin/bash
##########################################
############ CONSTANTS###################
#########################################
#06-Feb-2012.txt
#FILE_NAME="`date +%d`-`date +%b`-`date +%Y`".txt
FILE_NAME="`date --date="1 days ago" +%d`-`date +%b`-`date +%Y`".txt
NEWFILE1="`date --date="1 days ago" +%d`-`date +%b`-`date +%Y`"Part1.txt
NEWFILE2="`date --date="1 days ago" +%d`-`date +%b`-`date +%Y`"Part2.txt
SuffixName=$(date +%d%b%Y)
PATH=/home/cruser00/hand/Data
LOG=/var/log/cesar_file.log
DEST=/home/cruser00/handpunch_SHJ
WC=/usr/bin/wc
AWK=/usr/bin/awk
CUT=/usr/bin/cut
BC=/usr/bin/bc
SED=/bin/sed
NOFLINES=$($WC -l $PATH/$FILE_NAME | $CUT -d' ' -f1 | $AWK '{print $1}')
#echo $FILE_NAME
echo "----------start of log for $SuffixName--------------" >> $LOG

if [ -f "$PATH/$FILE_NAME" ];
then
echo "File $FILE_NAME exists" >> $LOG
echo "$FILE_NAME has $NOFLINES number of lines " >> "$LOG"
b=`echo "$NOFLINES % 2"|$BC`
if [ $b -eq 0 ]
then
echo "Given Number is Even "
SPLIT=$(echo "$NOFLINES / 2" | $BC )
SPLITN=$(echo "$SPLIT +1" | $BC)
$SED -n "1,${SPLIT}p" "$PATH/$FILE_NAME" > "$DEST/$NEWFILE1"
$SED -n "${SPLITN},${NOFLINES}p" "$PATH/$FILE_NAME" > "$DEST/$NEWFILE2"
echo " line number 1 -- $SPLIT from $PATH/$FILE_NAME transfered to $DEST/$NEWFILE1 " >> $LOG
echo " line number $SPLITN -- $NOFLINES from $PATH/$FILE_NAME transfered to $DEST/$NEWFILE2 " >> $LOG

echo "----------End of log for $SuffixName--------------" >> $LOG
exit
fi
echo " Given Number is odd"
NEW_NOFLINES=$(echo "$NOFLINES -1" | $BC )
echo $NEW_NOFLINES
SPLIT=$(echo "$NOFLINES / 2" | $BC )
echo " SPLIT = $SPLIT"
SPLITN=$(echo "$SPLIT +1" | $BC)
ADD=$(echo "$NEW_NOFLINES +1" | $BC )
$SED -n "1,${SPLIT}p" "$PATH/$FILE_NAME" > "$DEST/$NEWFILE1"
$SED -n "${SPLITN},${ADD}p" "$PATH/$FILE_NAME" > "$DEST/$NEWFILE2"
echo " line number 1 -- $SPLIT from $PATH/$FILE_NAME transfered to $DEST/$NEWFILE1 " >> $LOG
echo " line number $SPLITN -- $ADD from $PATH/$FILE_NAME transfered to $DEST/$NEWFILE2 " >> $LOG
echo "----------End of log for $SuffixName--------------" >> $LOG
else
echo "File $FILE_NAME does not exists" >> $LOG
echo "----------End of log for $SuffixName--------------" >> $LOG
exit 1
fi
 
Old 06-25-2012, 03:07 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
Hi,

the code you posted is extremely difficult to read (IMHO). I think you're more likely to get a useful response if you trim your script to be as simple as possible but still demonstrating the bug/problem. Also you should place it in [code] tags so that fixed width fonts are used and proper indenting is preserved.

Evo2.
 
1 members found this post helpful.
Old 06-25-2012, 03:33 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,781

Rep: Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567
first, you do not need several date commands:
instead of this:
#FILE_NAME="`date +%d`-`date +%b`-`date +%Y`".txt
you can use:
#FILE_NAME="`date +%d-%b-%Y`".txt
(as an example)

I assume the problem is that you have not used it properly:
FILE_NAME="`date --date="1 days ago" +%d`-`date <you should put 1 day ago here too> +%b`-`date <you should put 1 day ago here too> +%Y`".txt
but would be much better:
FILE_NAME="`date --date="1 days ago" +%d-%b-%Y`".txt
and even better to set:
Code:
YESTERDAY="`date --date="1 days ago" +%d-%b-%Y`"
FILE_NAME="$YESTERDAY".txt
NEWFILE1="$YESTERDAY"Part1.txt
NEWFILE2="$YESTERDAY"Part2.txt
 
Old 06-25-2012, 08:08 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,398

Rep: Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780
I think self-documenting code is easier to read
Code:
date --date="yesterday" +%d-%b-%Y
GNU date takes a bunch of extra useful params like that eg http://www.cyberciti.biz/tips/linux-...rows-date.html
 
Old 06-25-2012, 10:37 PM   #5
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
I see no need to reset your PATH env var and then set vars pointing to the various binaries:
Code:
PATH=/home/cruser00/hand/Data
WC=/usr/bin/wc
AWK=/usr/bin/awk
CUT=/usr/bin/cut
BC=/usr/bin/bc
SED=/bin/sed
I doubt very much that /bin and /usr/bin is not in your PATH env var. It's much cleaner to leave $PATH as is and define a var for your data path:
Code:
dataPATH="/home/cruser00/hand/Data"
and then append $FILE_NAME to this:
Code:
if [ -f "$dataPATH/$FILE_NAME" ];
It also means you can call sed with:
Code:
sed .......
instead of
Code:
$SED .......
I think that would make the code cleaner and a lot easier to read.

Also, since the shell uses all uppercase letters for it's env vars, it's always recommended that you use a different naming convention for your script vars. Instead of:
Code:
FILE_NAME=
use
Code:
File_Name=     # or
FileName=

Last edited by towheedm; 06-25-2012 at 10:43 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 09:46 AM
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 02:40 AM
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 08:37 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration