trapping error for a grep in for a loop

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting trapping error for a grep in for a loop
# 1  
trapping error for a grep in for a loop

How can I trap and print "cannot find the pattern" when the grep is unable to find the specified pattern in the file using the for loop below ?

Any help would be appreciated.

bash3.4> cat test_file


for x in `grep -n "mango" test_file |cut -d":" -f1`
echo $x
# 2  
There might be other better ways to do it, but this works too...

x=`grep -n "mango" test_file |cut -d":" -f1`
if [[ -z $x ]]; then
   echo "cannot find the pattern"
   return 1;
   echo "match found"
for y in $x
   echo $y

# 3  
Grep will return 1 if it can't mind a match, but the problem is you're piping it thought "cut", which doesn't fail. If you said

for x in `grep -n "mango" test_file`
echo $x
echo $?

Then the "exit code" from the for loop will be 1
# 4  

for x in `grep -n "mango" test_file |cut -d":" -f1`
if [ -n "$x" ];then
echo $x String is not empty
echo $x String is empty

I hope this help!
# 5  
First of all Let me thank you guys for posting the solutions so quickly.
I have tried each one of the code that was posted.

although arunsoman80 solution I liked apprently it does not work because of the return 1 and "-z" switch in the if statement.

scottn solution makes me twist the logic.

research3 solution fits perfect !

whatever it is, all you guys seem to be real knowlegeble and seem to have a good grip on unix

Thanks once again.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #72
Difficulty: Easy
The 'Unix philosophy' is a computer design philosophy based on creating small, modular utilities that do one thing and do them well.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trapping the error during copy

I have a requirement: During copy command for example: cp -rf <sourceDir> <destinationDir> this command may fails for many reasons like: 1. source or destination directory does not exist 2. destination directory does not have sufficient space 3. directories are not mounted ... Or may... (3 Replies)
Discussion started by: ambarginni
3 Replies

2. Shell Programming and Scripting

Error Trapping

Hi, I have one shell script as below while read SegList do if test -s ${SourceFile_Path}/${Segment_List_Temp} then ls -r -1 ${FTP_Path}/${SegList}.DAT.${Datelist}.GZ|cut -d '.' -f2>>${SourceFile_Path}/${List_Temp} echo "IF above statment Fail I want to Create Emtpy File How to Trapp... (3 Replies)
Discussion started by: samadhanpatil
3 Replies

3. Shell Programming and Scripting

curl error trapping in ksh

I hope that I can trap curl errors, and have my shell script error out and quit if curl has any sort of problem. For example, I have the following command in my shell script: curl --trace -n -v --ftp-ssl --user user:password -o /tmp/file.txt Works great, except... (2 Replies)
Discussion started by: prestonatwork
2 Replies

4. Shell Programming and Scripting

How to continue in the loop after trapping signal

hi all.... plz tel me how can i solve's the situation (just a sample!!).. $ cat sigtrap #!/usr/bin/perl $SIG{'INT'} = 'ABORT'; sub ABORT { print "\nStop the loop?? (y/n) : "; chop($ch=<STDIN>); if ($ch =~ //) { ... (3 Replies)
Discussion started by: tprayush
3 Replies

5. Shell Programming and Scripting

Searching and FTP error trapping

This is gonna sound dumb but... 1 It seems that I cannot use the search function here properly. In researching to find a solution to an FTP error trapping issue, I go to the search option in the forum and use FTP as a search term and ask it to select all forums to search in..... I get no... (2 Replies)
Discussion started by: Bartman
2 Replies

6. Shell Programming and Scripting

rcp error trapping in rsh

I am writing a program which is something like below: rsh host1 "rcp file dest:directory" I am running this script from a machine host2. host1 has rlogin configuration for host2. but, dest machine has no rlogin configuration for host1 and fails on remote calls. Could anyone tell me how... (2 Replies)
Discussion started by: vvejendla
2 Replies

7. Shell Programming and Scripting

Error Trapping

Hi, Can anybody tell me how to error trap an empty line. If i am asked for a password and I hit enter without entering any text, how do i display an error? Thanks Kev (6 Replies)
Discussion started by: kev112
6 Replies

8. UNIX for Dummies Questions & Answers

ftp error trapping

I have written a UNIX script that will automatically ftp a file to a server. The problem is when I missed enter information w/in the .txt file that contains the userid/password and what file to transfer, I had no way of capturing the failuer of the file transfer. I verified w/in the script that the... (1 Reply)
Discussion started by: dhawkjrscripter
1 Replies

9. UNIX for Dummies Questions & Answers

ftp error trapping

Hi I'm hoping I could get some help on the following. I'm writing a script which will in turn create an ftp script then excecute it. eg echo "user $user $pass" > $script echo "cd $remote_dir" >> $script echo "bi" >> $script echo "mput $file" >> $script echo "bye" >> $script ftp -n -i $ip... (1 Reply)
Discussion started by: Bab00shka
1 Replies

10. Shell Programming and Scripting

sql error code trapping

Hello #!bin/ksh sqlplus -s system/manager < |grep '^ORA' |uniq select * from kk; set echo on show spool on end; / EOF save sh results ORA-00942: table or view does not exist (3 Replies)
Discussion started by: xiamin
3 Replies

Featured Tech Videos