LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-12-2018, 01:50 AM   #1
BudiKusasi
Member
 
Registered: Apr 2017
Distribution: Artix
Posts: 345

Rep: Reputation: 15
I've made a failing customized grep function


I tried to ease text searches so made a customized grep:

Code:
g () {
if [ -n "$2" ]
then
 i=
 for s in $2
 do
	i="$i --include=*.$s"
 done
 else
	i='--include=*.txt --include=*.ini --include=*.*sh --include=*.c* --include=*.h --include=*.js --include=*.reg'
fi
grep -P -e \'$1\' -r "$i"
}
but I used it then

Code:
$ g sys ini
grep:  --include=*.ini: No such file or directory
if I insert 'echo' to last line for tracking and test:

Code:
echo grep -P -e \'$1\' -r "$i"
then reload the alias file and get to shell,

Code:
$ g sys ini
grep -P -e 'sys' -r  --include=*.ini
it'll echo correctly to become perfectly a grep command line...

Any sincere guide to help me out by which we all have benefits ?
 
Old 02-12-2018, 01:43 PM   #2
NoStressHQ
Member
 
Registered: Apr 2010
Location: Geneva - Switzerland ( Bordeaux - France / Montreal - QC - Canada)
Distribution: Slackware 14.2 - 32/64bit
Posts: 609

Rep: Reputation: 221Reputation: 221Reputation: 221
It's a guess, but the way you build you '$i', you ALWAYS have a leading space... PLUS, as you double quote it, it's sent as ONE argument (even if you have several --include...)

So you might just want to remove the double quotes... (edit: on the 'grep' command line).
Or use an array and double quote array expansion.

(I haven't tried anything, just read your code)...

Also if you have a way not to have a leading space, it would be more clean.
 
Old 02-12-2018, 02:13 PM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Quoting of the --include "glob" is important, so grep expands it, not the shell.
But $i cannot be quoted because you want the shell to split the string into different grep arguments.
Use
Code:
set -f
to prevent the shell from globbing
Code:
...
else
	i='--include=*.txt --include=*.ini --include=*.*sh --include=*.c* --include=*.h --include=*.js --include=*.reg'
fi
set -f
grep -P -e "$1" -r $i
set +f
}
 
Old 02-12-2018, 02:14 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by BudiKusasi View Post
I tried to ease text searches so made a customized grep:

Code:
g () {
if [ -n "$2" ]
then
 i= #nothing set makes it '' null
 for s in $2
 do     i is null
	i="$i --include=*.$s"
        #check your code
        echo "i=$i"
 done
 else
{
        #check your code
        echo "else: i=$i"
	i='--include=*.txt --include=*.ini --include=*.*sh --include=*.c* --include=*.h --include=*.js --include=*.reg'
}
fi
#your only use of grep 
grep -P -e \'$1\' -r "$i"
}
but I used it then

Code:
$ g sys ini
grep:  --include=*.ini: No such file or directory
if I insert 'echo' to last line for tracking and test:

Code:
echo grep -P -e \'$1\' -r "$i"
then reload the alias file and get to shell,

Code:
$ g sys ini
grep -P -e 'sys' -r  --include=*.ini
it'll echo correctly to become perfectly a grep command line...

Any sincere guide to help me out by which we all have benefits ?
where are you running the script? it is looking in the place it is being ran in, due to you have no real path to it?
 
Old 02-12-2018, 05:18 PM   #5
BudiKusasi
Member
 
Registered: Apr 2017
Distribution: Artix
Posts: 345

Original Poster
Rep: Reputation: 15
I did on MSYS2+minGW64 on windows 10

Code:
$ grep --version
grep (GNU grep) 2.26
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3
Thanks for everyone, the new truth I infer is removing pair of double quote for $i is working without being able to send it to stdout or shell display. Because it freeze for awhile as if it scan all required ones.
Anyone know why and how to solve making it shown on shell?

Last edited by BudiKusasi; 02-12-2018 at 05:39 PM.
 
Old 02-12-2018, 05:33 PM   #6
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
edit, sorry wrong post.

Last edited by AwesomeMachine; 02-12-2018 at 05:34 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Grep words made up of a single dot Fiatuno Linux - Newbie 4 05-03-2015 02:48 AM
PHP mail() Function Failing devUnix Programming 4 12-05-2011 01:34 AM
[SOLVED] C++ list regular files function failing inconsistently xtothat Programming 2 03-29-2011 04:09 AM
Code tarball build failing in gnaupps (coreutils, grep) on a FC7 mad34 Linux - General 1 03-18-2010 05:49 AM
undefined reference to a function included in a library i made!!! keos Programming 5 02-21-2004 04:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration