Simple ksh script problem

This is a search script that I have developed. It finds the records that I look for. However the only thing I want the program to do now is to display a message such as 'Not Found' to appear when a record is not found. So far when a search doesn't display a record, the screen is blank.

compfile=~/sample_data.txt
clear
bold=`tput smso`
offbold=`tput rmso`
echo $bold
tput cup 3 1; echo "UsedComputersRUs Menu- Search Computer"
echo $offbold
tput cup 4 1; echo "======================================"
tput cup 5 1
echo "Enter computer to search for: "
tput cup 5 31
read compdate
echo
grep -i $compnumber $compdate $model $manufacturer $price $compfile | tr ':' ' '
echo
echo "Press ENTER to continue..."
read continue

after the grep statement:

if [ $? -eq 1 ]
then
  echo "No Matching Records Found"
fi

I tested it acouple of times and still. nothing appears when, a search does not match a record.

The following changes were done to my script, but still I dont get any message to appear once a search does not equal a record.

compfile=~/sample_data.txt
clear
bold=`tput smso`
offbold=`tput rmso`
echo $bold
tput cup 3 1; echo "UsedComputersRUs Menu- Search Computer"
echo $offbold
tput cup 4 1; echo "======================================"
tput cup 5 1
echo "Enter computer to search for: "
tput cup 5 31
read compdate
echo
grep -i $compnumber $compdate $model $manufacturer $price $compfile | tr ':' ' '
echo 
if [ $? -eq 1 ]
then
echo "No Matching Records Found"
fi
echo "Press ENTER to continue..."
read continue

Why the colored echo ? It always returns true.

When you remove that echo, you would still face the problem.

The reason why the echo "No Matching Records Found" wont happen is because all results are piped to tr and the actual exit status gets overwritten before the if statement.

Look at this post for a solution on how to catch an intermediate exit status.

Vino

Once I remove the |tr ':" everything works fine. However the program is required to output the records without the colon. Since this is how each records is divided in the text file.

#!/bin/ksh
........
exec 3>&1
eval $(exec 4>&1 >&3 3>&-
  {
   grep -i $compnumber $compdate $model $manufacturer $price $compfile 2>&1 4&-; echo "exitCode=$?"; >&4
   } | tr ':' ' '
);

if [ ${exitCode} -eq 1 ]
then
echo "No Matching Records Found"
fi
echo "Press ENTER to continue..."

if [ $? -eq 1 ]

Could please tell were to specifically put the could you gave. I have no idea where to put the code.

compfile=~/sample_data.txt
clear
bold=`tput smso`
offbold=`tput rmso`
echo $bold
tput cup 3 1; echo "UsedComputersRUs Menu- Search Computer"
echo $offbold
tput cup 4 1; echo "======================================"
tput cup 5 1
echo "Enter computer to search for: "
tput cup 5 31
read compdate
echo
grep -i $compnumber $compdate $model $manufacturer $price $compfile
if [ $? -eq 1 ]
then
echo "No Matching Records Found"
fi
echo 
echo "Press ENTER to continue..."
read continue

It was pretty clear from vgersh99's post where to place his solution.

Anyway, here it is.

#! /bin/ksh
compfile=~/sample_data.txt
.
.
read compdate
echo

exec 3>&1
eval $(exec 4>&1 >&3 3>&-
  {
   grep -i $compnumber $compdate $model $manufacturer $price $compfile 2>&1 4&-; echo "exitCode=$?"; >&4
   } | tr ':' ' '
);

if [ ${exitCode} -eq 1 ]
then
echo "No Matching Records Found"
fi
echo "Press ENTER to continue..."

read continue

I get same error. That is way asked where to place the new code. I was testing it before an I keep on getting the same error. I don't understand what the error message states.

Try this..

#! /bin/ksh
compfile=~/sample_data.txt
.
.
read compdate
echo

exec 3>&1
eval $(exec 4>&1 >&3 3>&-
  {
   grep -i $compnumber $compdate $model $manufacturer $price $compfile 2>&1 4&-; echo "exitCode=$?"; >&4
   } | tr ':' ' '
);

if [[ ${exitCode} -eq 1 ]]
then
echo "No Matching Records Found"
fi
echo "Press ENTER to continue..."

read continue

Same error message

UsedComputersRUs Menu- Search Computer

Enter computer to search for: sony

grep can't open 4
/studes/LAC220/g1ae020/sample_data.txt 016 18-12-2005 Sony Centrino-1.6Ghz 2000.00
/studes/LAC220/g1ae020/sample_data.txt 017 18-01-2006 Sony Centrino-1.6Ghz 5000.00
/studes/LAC220/g1ae020/sample_data.txt 020 22-02-2006 Sony Centrino-1.6Ghz 2000.00
/studes/LAC220/g1ae020/sample_data.txt 022 03-04-2005 Sony Centrino- 1.6Ghz 3500.00
/studes/LAC220/g1ae020/sample_data.txt 023 26-06-2006 Sony Centrino- 1.6Ghz 2345.00
./search.txt[26]: -: not found
exitCode=127
Press ENTER to continue...

fix this line ...

grep -i $compnumber $compdate $model $manufacturer $price $compfile 2>&1 4>&-; echo "exitCode=$?"; >&4

This is the output the I get from below code. However I still dont get the "No Matching Records Found" when a record does not exist.

UsedComputersRUs Menu- Search Computer

Enter computer to search for: sony

016 18-12-2005 Sony Centrino-1.6Ghz 2000.00
017 18-01-2006 Sony Centrino-1.6Ghz 5000.00
020 22-02-2006 Sony Centrino-1.6Ghz 2000.00
022 03-04-2005 Sony Centrino- 1.6Ghz 3500.00
023 26-06-2006 Sony Centrino- 1.6Ghz 2345.00
exitCode=0
Press ENTER to continue...

#! /bin/ksh
compfile=~/sample_data.txt
clear
bold=`tput smso`
offbold=`tput rmso`
echo $bold
tput cup 3 1; echo "UsedComputersRUs Menu- Search Computer"
echo $offbold
tput cup 4 1; echo "======================================"
tput cup 5 1
echo "Enter computer to search for: "
tput cup 5 31
read compdate
echo
exec 3>&1
eval $(exec 4>&1 >&3 3>&-
  {
   grep -i $compnumber $compdate $model $manufacturer $price $compfile 2>&1 4>&-; echo "exitCode=$?"; >&4
   } | tr ':' ' '
);

if [[ ${exitCode} -eq 1 ]]
then
echo "No Matching Records Found"
fi
echo "Press ENTER to continue..."
read continue

I figured it out grep -i $compnumber $compdate $model $manufacturer $price $compfile 2>&1 4>&-; echo "exitCode=$?" >&4;