1 Command - 2 Behaviours and a wrong false message

Heyas

As there was a splitting issue with TUI 0.6.6-x focused around tui-status since the 'solaris' (not really but for that purpose) update, the urge was given to do this first.

Well, the splitting 'bug' should be fixed, i hope.
But i've found a new one.

When i start the very same script in two diffrent ways, it one time works, the other time it dont.

One script is called by the WM named 'Awesome':
LUA (rc.lua):

...
	terminal = "lxterminal"
	term_cmd = terminal .. " -e "
...
	 { "BG: NASA iotd", term_cmd .. awful.util.getdir("config") .. "/scripts/nasaBackground-new2.sh",  },
	  

And the other time i call it like:

./scripts/nasaBackground-new2.sh

So basicly, i call it two times the same way.
Right?

During this test phase, i check if the first argument is numeric, by this (bash):

		check=$(echo "$1" | tr -d [:digit:])
		if [[ -z "$check" ]] && [[ ! -z "$1" ]] 		# && echo $1 | grep -q [0-9]
		then	echo "all numbers:: $1" > /dev/zero
		else	echo "First argument $1 ($check) is not numeric!"
			exit 1
		fi

But see this (wide!):
https://sea.fedorapeople.org/review/tui/screenshots/1-app-2-behaviours\_4_small.jpg
I also checked wether there was a leading or tailing space, by changing to ~ "first argument .$1. is not numeric", there is not.

While writing the post, i've added the ($check) is not numeric , and reran the script, for some reason i dont understand or see in the code, it seems than when the script is started by 'lua' - the interface, the values are bogus: "$check" = "$1"

Any idea why?

  • or how i could fix this please?

Thank you in advance.

Try changing:

check=$(echo "$1" | tr -d [:digit:])

to:

check=$(echo "$1" | tr -d '[:digit:]')

Double quotes should also work as well as single quotes in this example. (I generally prefer single quotes when quoting constant strings.)

PS
I should have said why the quotes are needed...
The unquoted argument [:digit:] is a pathname bracket expression that changes to a list of files with the single character names : , d , i , g , and t if any such files are present in the directory where you run this command. If you run it in a directory where there are no files matching this pattern, the unchanged pattern is passed to tr as an operand. So, the reason it works sometimes and fails sometimes is the contents of the directory where it is run; not the script used to invoke it.

1 Like

Edit: too late, I didn't refresh my browser ...

You should quote the character class operator:

check=$(echo "$1" | tr -d "[:digit:]")

In your script, the shell is processing the unquoted [:digit:] argument before passing it to the tr command so looks in the current directory for a file with a name composed of a single character in the range ":digt". If there is none it keeps the argument unchanged and the script works. If there is a match, it replaces the argument with this single character defeating the numerical character removal attempt.

1 Like

Yahaah :slight_smile:
With that knowledge i could fix the other toggle of the script working or not...
But that was in tui-printf, which tui-status called, but thank to you guys i knew what i needed to search for! :b:

Thank you guys!

---------- Post updated at 07:18 ---------- Previous update was at 07:16 ----------

Would this also apply to the [[:space:]] ?

SEARCH="$( $GREP $OPT ^[[:space:]]${VARNAME}= "$CONFFILE"|$GREP -v ^#)"

As in, should i also place single quotes there?

Yes. Without quotes, if there are any files with filenames in the directory where your script is run that match the filename matching pattern ^[[:space:]]${VARNAME}= , you would have the same problem. This is probably less likely than in your previous example. Assuming that you want ${VARNAME} to be expanded in the regular expression you pass to grep , you want double quotes around the expression:

SEARCH="$( $GREP $OPT "^[[:space:]]${VARNAME}=" "$CONFFILE"|$GREP -v '^#')"

If you want to look for the literal string ${VARNAME}= immediately after a character of class space at the start of a line, you need to use single quotes and escape the dollar sign:

SEARCH="$( $GREP $OPT '^[[:space:]]\${VARNAME}=' "$CONFFILE"|$GREP -v '^#')"
1 Like