Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 ?
Location: Geneva - Switzerland ( Bordeaux - France / Montreal - QC - Canada)
Distribution: Slackware 14.2 - 32/64bit
Posts: 609
Rep:
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.
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
}
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?
$ 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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.