Computing.Net > Forums > Programming > bash script doesnt give same output

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

bash script doesnt give same output

Reply to Message Icon

Name: Tejo Prayaga
Date: July 13, 2003 at 07:20:09 Pacific
OS: SunOS tejo 5.6 Generic_10
CPU/Ram: SPARC
Comment:

Hi,

I have a bash shell script, which is behaving wierd. Sometimes it prints the
output as expected, and sometimes it doesn't produce any output at all.

Its functionality, in brief is
. scan a text file,
. pick up lines based on a keyword, and select a specific field from that line
. the first entry found, describes what that file is meant for
. leave all those fields, which contain unwanted keywords
. finally make a unique list of these descriptions

The script, most of the times, is giving the intended output, but sometimes not.
Try to execute this script, number of times in succession, may be try 10
times...; and obsorve the output.

Could anyone kindly let me know, why this script is behaving like this. There
may be some other options to do this with 'awk' and 'sed', but I specifically
want to know why this is behaving like this.

I am enclosing both the text file and the script.

Thanks a lot in advance.

Regards,
Tejo.
----------------

Here are the details of the bash version and OS I am running.
> GNU bash, version 2.05b.0(2)-release (sparc-sun-solaris2.5.1)
> Copyright (C) 2002 Free Software Foundation, Inc.

OS specification:
> SunOS tejo 5.6 Generic_105181-33 sun4u sparc SUNW

This is how one can invoke the script.
bash$ ./getRequiredInfo.sh -o REPORT.DAT mydescr.txt; cat REPORT.DAT

# here comes the script BEGIN
#! /bin/bash
# set -vx;
usage_help() {
echo "usage: $0 [var2] ... [varN]";
echo
echo -e "out-file is:"
echo -e "\toutput file, into which the report will be written"
echo
echo -e "varN is:"
echo -e "\tdescription file";
exit 1;
}


# check if we have write permission
validate_outfile() {

[ -r $REPORT ] && [ ! -w $REPORT ] && { \
echo "Specified file, $REPORT cannot be written..";
echo "Exiting..";
exit 1;
}
}


noInfo(){
echo "No required information ...";
echo;
}

# min 3 params required, -0, outfilename and one desciption file
[ $# -lt 3 ] && { usage_help; }

REPORT=
while getopts o: param
do case "$param" in
o) REPORT="$OPTARG"
validate_outfile;
;;

[?]) usage_help;
exit 1;
;;
esac
done

shift $(($OPTIND - 1))
VAR_LIST=$*;
KEYWORD=keyword
EXCLUDE='unwanted1 unwanted2 unwanted3'

echo | cat > $REPORT;
for file in $VAR_LIST
do
/bin/rm -f .t1;
echo "Processing $file ...";


# get the descriptions
# remove duplicate descriptions
grep $KEYWORD $file | cut -d',' -f4- | \
cut -d'(' -f2 | cut -d')' -f1 | cat > .t1;


# detect if no $KEYWORD is not found in the file
# and skip the next steps in the loop
! [ -s .t1 ] && { noInfo; continue; }

# first entry will be title specification
# dump it into the report
echo -ne "Title:" | cat >> $REPORT;
head -1 .t1 | cat >> $REPORT;

# now remove, the first line, as we have already taken care of
# it as title specification, and dump remaining lines into temp
# file
let noOfLines=`wc -l .t1 | awk '{print $1}'`;
noOfLines=`expr $noOfLines - 1`;
tail -$noOfLines .t1 | tee | cat > .t1;

# remove unwanted entries from the output
for word in $EXCLUDE
do
grep -v $word .t1 | tee | cat > .t1;
done;

# remove duplicates
sort -u .t1 | tee | cat > .t1;

# dump in the file
echo -e "Required descriptions :" | cat >> $REPORT;
cat .t1 >> $REPORT;
done;

/bin/rm -f .t1 .t2;

echo
echo "Done"
# END

--here comes the sample text file, which needs to be scanned--
keyword.1 , type1 , accesstype , fixed(Title Specification, description1, description2)
keyword.3 , type1 , accesstype , fixed(unwanted1 description)
keyword.4 , type1 , accesstype , fixed(description1)
keyword.5 , type1 , accesstype , fixed(unwanted1 description)
keyword.6 , type1 , accesstype , fixed(unwanted1 description)
keyword.7 , type1 , accesstype , fixed(description11)
keyword.8 , type1 , accesstype , fixed(description11)
keyword.9 , type1 , accesstype , fixed(description12)
keyword.10 , type1 , accesstype , fixed(description0)
keyword.11 , type1 , accesstype , fixed(unwanted2 description)
keyword.12 , type1 , accesstype , fixed(description23)
keyword.13 , type1 , accesstype , fixed(description23)
keyword.14 , type1 , accesstype , fixed(description24)
keyword.15 , type1 , accesstype , fixed(description)
keyword.16 , type1 , accesstype , fixed(unwanted3 description)
keyword.17 , type1 , accesstype , fixed(description12)
keyword.18 , type1 , accesstype , fixed(description123)
keyword.19 , type1 , accesstype , fixed(description)
keyword.20 , type1 , accesstype , fixed(unwanted3 description)
keyword.21 , type1 , accesstype , fixed(description345)
keyword.22 , type1 , accesstype , fixed(description456)
keyword.23 , type1 , accesstype , fixed(description)
keyword.24 , type1 , accesstype , fixed(unwanted2 description)
keyword.25 , type1 , accesstype , fixed(description)
keyword.26 , type1 , accesstype , fixed(description)
keyword.27 , type1 , accesstype , fixed(unwanted1 description)
-- text file end--



Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


How to write Morse Code recovery my files in ghos...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: bash script doesnt give same output

Bash script to start application www.computing.net/answers/programming/bash-script-to-start-application/18255.html

Bash script help www.computing.net/answers/programming/bash-script-help/10720.html

Need Help with Bash script www.computing.net/answers/programming/need-help-with-bash-script/16687.html