Grep behaves diffrent upon printf or echo output

Hello

I want to check whether certain arguments were passed to the script, and when those are, not doing a log entry.
If those arguments are not passed, always do a log entry (new call).

What currently i have is this:

	echo "${@}"|grep -q \\-[Lih] || \
		tui-log -e "$LOG" "\r---- New call $$ ----"

Which upon these 2 commands, produces these lines within the logfiles:

vhs -h ; vhs -L

With echo i have the expected output:

21:42:46         Show Help
21:42:46         Show Logfile
21:42:49         Show Help
21:42:49         Show Logfile

However, as soon i change echo to become printf , it produces this (not wanted):

---- New call 17388 ----
21:44:19         Show Help

---- New call 17423 ----
21:44:19         Show Logfile

In addition i wanted to use [[:space:]]\\-[Lih] , but i assume that is another issue?
I've tried quoting the regex, but still fails.

System:

export LC_ALL=C ; uname -r ; echo "--" ; $SHELL --version
3.16.3-200.fc20.x86_64
--
GNU bash, version 4.2.51(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Any ideas?
Thank you in advance

You only care if 1 of those 3 options are passed? And how did you use printf?

$ echo "$@"
one two three
$ printf "$@"
one$

Clearly the printf statement needs to be different. This will give the same string:

$ printf '%s\n' "$*"
one two three

But keeping the idea of using printf and grep, perhaps you want:

$ echo "$@"
-L -i
$ printf '%s\n' "$@" | grep -c -- -[Lih]
2

Now we know we had 2 of the 3 options...

1 Like

Although the filenames -L -i , and -h are not common, scripts should always assume that they might be present. Therefore, the grep -[Lih] operand should be quoted:

$ printf '%s\n' "$@" | grep -c -- '-[Lih]'
1 Like

Thanks Don (and Neutron too), that thought was actualy in mind, but only as part of a filename.
That just solved my 2nd question, i still was fiddling around, me used the wrong quotes :slight_smile:

EDIT:
Weird, the solved tag didnt put a [solved] prior to the threads title..

You can search on tags like solved, but they don't change the title of your thread. The solved tag is special in that it also has the side-effect of changing the color used when displaying the title of the thread from black to blue.

1 Like