Reading file and return value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading file and return value
# 1  
Old 02-08-2010
Reading file and return value

Hi guys,

I am newbie in unix, so I need your help.
I need to right unix function code that can read sample.txt. If sample.txt contains sentence such as "Error due to syntax." then it will return value.

I tried below code, but I got errors.

Code:
 
if [ $errcheck == 1 ] 
   then
     echo " There are errors in the file"
else
     echo " There are no errors in the file"
fi
 
errtext="Error due to syntax"
 
errcheck ()
{
grep "Error due to syntax" sample.txt 
 
if [ $? == $errtext ]
   then
      return 1
   else
      return 0
    fi
}

Thanks in advance.
# 2  
Old 02-08-2010
You could try a script like this (replace /path/to/file with the actual path to the file you are grepping for errors):

Code:
#!/bin/bash

no_errors=`grep -c "Error due to syntax" /path/to/file`

if test $no_errors -gt 0;
        then
                echo "There are errors in the file"
        else
                echo "There are no errors in the file"
fi

# 3  
Old 02-08-2010
Quote:
Originally Posted by cmf1985
You could try a script like this (replace /path/to/file with the actual path to the file you are grepping for errors):

Code:
#!/bin/bash
 
no_errors=`grep -c "Error due to syntax" /path/to/file`
 
if test $no_errors -gt 0;
        then
                echo "There are errors in the file"
        else
                echo "There are no errors in the file"
fi

Thank you very much for your instant answer. Smilie
# 4  
Old 02-08-2010
The error in your script is that you evaluata a variable 'errcheck' instead of makeing a function call. So you should write

Code:
if errcheck
   then
     echo " There are errors in the file"
else
     echo " There are no errors in the file"
fi
 
errtext="Error due to syntax"
 
errcheck ()
{
grep "Error due to syntax" sample.txt 
 
if [ $? == $errtext ]
   then
      return 1
   else
      return 0
    fi
}

or much shorter:

Code:
if grep "Error due to syntax" sample.txt >/dev/null
then
     echo " There are errors in the file"
else
     echo " There are no errors in the file"
fi



---------- Post updated at 13:04 ---------- Previous update was at 13:03 ----------

Oops, cmf1985 answered the question already! :-)
# 5  
Old 02-08-2010
Thank you for your answer, too. I will try to implement them to my codes.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh Script, Reading A File, Grepping A File Contents In Another File

So I'm stumped. First... APOLOGIES... my work is offline in an office that has zero internet connectivity, as required by our client. If need be, I could print out my script attempts and retype them here. But on the off chance... here goes. I have a text file (file_source) of terms, each line... (3 Replies)
Discussion started by: Brusimm
3 Replies

2. Shell Programming and Scripting

Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command. . /opt/app/scripts/cdc_migration.sh But it fails with the below error when I try it this way /opt/app/scripts/cdc_migration.sh /opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
Discussion started by: svajhala
1 Replies

3. UNIX for Dummies Questions & Answers

Reading Xml file and print the values into the text file in columnwise?

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (4 Replies)
Discussion started by: sravanreddy
4 Replies

4. UNIX for Advanced & Expert Users

File command return wrong filetype while file holds group separator char.

hi, I am trying to get the FileType using the File command. I have one file, which holds Group separator along with ASCII character. It's a Text file. But when I ran the File command the FileType is coming as "data". It should be "ASCII, Text file". Is the latest version of File... (6 Replies)
Discussion started by: Arpitak29
6 Replies

5. Shell Programming and Scripting

How can i make the current shell return from the middle of a script reading?

I am using the popular bash shell. Under the current interactive shell, i run the script like: ". ./myscript.txt" . After the current shell has finish the script, the shell will continue to work as I did previously. Actually I want the shell can return from the middle of the scripts it is... (1 Reply)
Discussion started by: Bill Zhao
1 Replies

6. UNIX for Dummies Questions & Answers

Search for String within File and Return File Name

How do I search for a string within a file and return the names of the file that contain the string? I would like to search directories and sub-directories. (4 Replies)
Discussion started by: bggibson
4 Replies

7. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

8. Programming

reading the return value from S_ISDR

how to read the return value of S_ISDIR macro. It works fine when used in if loop, but how to print the returned value. Thanks bishweshwar (3 Replies)
Discussion started by: bishweshwar
3 Replies

9. Shell Programming and Scripting

Get the return log file

I have an execution in my shell script when run return a xxx.log file how wil i be able to get the name of the file and read it then test the string written in the file by the programs... Anyone can help me please... Am new to shell programming...... Lutchumaya (1 Reply)
Discussion started by: Lutchumaya
1 Replies

10. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies
Login or Register to Ask a Question