Simple ksh script problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple ksh script problem
# 1  
Old 09-09-2005
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.

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 | tr ':' ' '
echo
echo "Press ENTER to continue..."
read continue

# 2  
Old 09-09-2005
after the grep statement:

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

# 3  
Old 09-09-2005
I tested it acouple of times and still. nothing appears when, a search does not match a record.
# 4  
Old 09-09-2005
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.

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 | tr ':' ' '
echo 
if [ $? -eq 1 ]
then
echo "No Matching Records Found"
fi
echo "Press ENTER to continue..."
read continue

# 5  
Old 09-09-2005
Quote:
Originally Posted by Warrior232
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
# 6  
Old 09-09-2005
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.

Last edited by Warrior232; 09-10-2005 at 01:11 PM..
# 7  
Old 09-09-2005
Code:
#!/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 ]

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script (simple problem)

I want to find and replace string from files present in one directory. user will input the string to be searched and to replace . Here is my program but Not working echo "Enter Old domain name:" read old echo "Enter New domain name:" read new grep -rl '$old' /var/www/ | xargs sed -i... (4 Replies)
Discussion started by: sunny2802
4 Replies

2. Shell Programming and Scripting

how to execute ksh simple shell script without creating .sh file

please help me to execute a simple shell script like for i in `ls echo $i done . i dont want to create a new sh file to execute it. Can i just type and execute it ? because I always this kind of simple for loops . Please help . Thanks (7 Replies)
Discussion started by: Sooraj_Linux
7 Replies

3. Shell Programming and Scripting

[newb] simple script, big problem

Warning! I'm ridiculously new at all this, so pardon my ignorance... I have a very simple script which is intended to search a hosts file when given a partial hostanme or ip address. The if the partial hostname/ip given is unique, the script automatically logs the user in to that host. If... (6 Replies)
Discussion started by: itomb
6 Replies

4. Shell Programming and Scripting

Simple bash script problem

#!/bin/bash cd /media/disk-2 Running ./run.sh it's not changing directory.Why? (6 Replies)
Discussion started by: cola
6 Replies

5. UNIX for Dummies Questions & Answers

simple script with while loop getting problem

Hello forum memebers. can you correct the simple while program. #! /bin/ksh count=10 while do echo $count count='expr$count-1' done I think it will print 10 to 1 numbers but it running for indefinite times. (2 Replies)
Discussion started by: rajkumar_g
2 Replies

6. Shell Programming and Scripting

Simple AWK script problem.

Hi all, I have set up a simple awk script to calculate the average of values that are printed out a number of times per second (the number of time the printing occurs varies). The data is of the format shown below: 1 4.43 1 3.65 1 2.45 2 7.65 2 8.23 2 5.65 3 4.65 3 6.21 .. .. 120... (4 Replies)
Discussion started by: omnomtac
4 Replies

7. Shell Programming and Scripting

one simple shell script problem

Hi everyone, I am facing to one shell script problem, which is as following Write a shell script that: Takes a number of arguments. For each argument, print out all files in the current directory that contain this substring in their name. I know I need to use grep for the second... (7 Replies)
Discussion started by: shaloovia
7 Replies

8. Shell Programming and Scripting

Simple script problem

Hi everyone - I am sure this is a really simple problem but I'm a total noob at Linux scripting: I wanted to create a script that allows me to compare the current week number to the contents of a text file in my home directory: VAR1='date +%V' VAR2='cat /home/fred/file.txt' ... (6 Replies)
Discussion started by: FiniteRed
6 Replies

9. Shell Programming and Scripting

need help with simple awk/ksh script

I need help finding out why this script wont run. The chmod is okay, but i get an error saying that I need '&&' on line 5. (18 Replies)
Discussion started by: tefflox
18 Replies

10. UNIX for Dummies Questions & Answers

simple shell script problem

hi all. i have a little problem. im basically reading input from the user from the keyboard into the variable "phonenumber". I want to do a little error check to check if the user doesnt enter anything in for the value phonenumber. i had this: read phonenumber if then ..... else ........ (2 Replies)
Discussion started by: djt0506
2 Replies
Login or Register to Ask a Question