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
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.
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.
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.
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...
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