1 Command - 2 Behaviours and a wrong false message


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 1 Command - 2 Behaviours and a wrong false message
# 1  
Old 01-10-2015
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):
Code:
...
	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:
Code:
./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):
Code:
		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/...rs_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.
# 2  
Old 01-10-2015
Try changing:
Code:
check=$(echo "$1" | tr -d [:digit:])

to:
Code:
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.

Last edited by Don Cragun; 01-10-2015 at 01:41 AM.. Reason: Add PS.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 01-10-2015
Edit: too late, I didn't refresh my browser ...

You should quote the character class operator:

Code:
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.
This User Gave Thanks to jlliagre For This Post:
# 4  
Old 01-11-2015
Yahaah Smilie
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! Smilie

Thank you guys!

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

Would this also apply to the [[:space:]] ?
Code:
SEARCH="$( $GREP $OPT ^[[:space:]]${VARNAME}= "$CONFFILE"|$GREP -v ^#)"

As in, should i also place single quotes there?
# 5  
Old 01-11-2015
Quote:
Originally Posted by sea
Yahaah Smilie
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! Smilie

Thank you guys!

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

Would this also apply to the [[:space:]] ?
Code:
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:
Code:
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:
Code:
SEARCH="$( $GREP $OPT '^[[:space:]]\${VARNAME}=' "$CONFFILE"|$GREP -v '^#')"

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

What is wrong with my awk command?

The below code usually works when the value for the COLUMN variable is numerical. but im in a situation where the number of fields in a file is not definitive. it changes. but what is static is that the value i want to retrieve from the log is 3 fields from the last field. which is what i... (9 Replies)
Discussion started by: SkySmart
9 Replies

2. Solaris

User account get locked due to strange behaviours

I am facing strange problem where after three failed login attempt user password must be locked. Actually what is happening, when I take the putty session of the server & enter user name on the prompt at the login prompt & then press enter to enter the password at this time when I checked the... (10 Replies)
Discussion started by: sb200
10 Replies

3. Solaris

After command setenv auto-boot false machine's console not displaying

hi Alll, I want to install solaris 10 on exixting solaris mahcine. I tried for OK boot cdrom-install but it gave me fatal error - disk not bootable , boot command disable so i tried with ok setenv auto-boot? false Ok reset-all after this server rebooted automatically and... (2 Replies)
Discussion started by: sunray
2 Replies

4. Shell Programming and Scripting

What's wrong with the following command?

Hi all, I'm trying to run the following command to get all files in one directory to another with the files' timestamps preserved, cp -p /logs/dvgbiau/batch/* /logs/dvgbiau/tmp_batch Note that ./batch and ./tmp_batch are two sub-directories under /logs/dvgbiau. The error was,... (1 Reply)
Discussion started by: isaacniu
1 Replies

5. UNIX for Dummies Questions & Answers

what is wrong with this command?

Hello, I try to using the below command to find out all the datafiles under "sja" direcotory. $ xargs -i find {} -type f -ls < sja /bin/ksh: sja: cannot open so can you tell me what is wrong? Thanks Jerry (3 Replies)
Discussion started by: GreatJerry
3 Replies

6. UNIX for Advanced & Expert Users

what is wrong with this find command

i am trying to find the files which are more than 100MB and it was created 10 days ago. find /lola/loaded -size +102400 -mtime -10 -print | xargs ls -ltr -rw-rw-r-- 1 lola_adm gdrmp 82054170 Jun 23 06:17 /lola/loaded/ILMEMBER20090622.txt -rw-rw-r-- 1 lola_adm gdrmp 652080494 Jun 24... (3 Replies)
Discussion started by: sudhiroracle
3 Replies

7. Shell Programming and Scripting

Is anything wrong with this command

Hi All, can anyone tell me what is wrong with this command. tail -f /opt/olr-logs/PaymentGateway.log | grep "DEBUG - Start! AkhtarPaymentGateway - generateChecksum" | awk '{print $13}' | sed 's/,//g'>> abc But I found nothing in the file abc Please do help me.or Provide me some... (8 Replies)
Discussion started by: akhtar.bhat
8 Replies

8. Shell Programming and Scripting

Whats wrong with the mv command

I am sorry guys I have figured out the error myself. (0 Replies)
Discussion started by: dsravan
0 Replies
Login or Register to Ask a Question