Testing for non-zero length string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Testing for non-zero length string
# 1  
Old 12-07-2011
Testing for non-zero length string

Hello, can someone please explain to me why this happens:

Code:
myserver#echo "$nothing"

myserver#if [ -z ${nothing} ]; then echo "nothing is a zero length string"; fi
nothing is a zero length string
myserver#if [ -n ${nothing} ]; then echo "nothing is also a non-zero length string, apparently"; fi
nothing is also a non-zero length string, apparently

Surely an undefined variable cannot be both zero-length and non-zero-length at the same time? I always have problems with the -n test flag - does it actually work? Smilie

I am using bash shell on a Solaris 9 server.

TIA,

jon
# 2  
Old 12-07-2011
It's a string test. Put double quotes round the string wherever you mention it. Without the quotes it is a syntax error in most Shells.
Code:
echo "$nothing"
if [ -z "${nothing}" ]; then echo "nothing is a zero length string"; fi
if [ -n "${nothing}" ]; then echo "nothing is also a non-zero length string, apparently"; fi

# 3  
Old 12-08-2011
Ah! OK I thought the curly braces had that effect. Thanks!
# 4  
Old 12-08-2011
Curly braces delimit the name of the variable. Avoids ambiguous substitution. Most scripters use them out of habit.
# 5  
Old 12-08-2011
OK nothing more than that. Great thanks again.
# 6  
Old 12-08-2011
Since you're using bash, you could avoid this problem in the first place by using the shell keywords [[ and ]] instead. All around better. See here for more info: wiki.bash-hackers.org/syntax/ccmd/conditional_expression
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Testing the length of a string

Hello, Unix-Forums! Is there a command that can check how long a user-entered string is? Please don't give me a code, just the name of the command (playing around yourself is much more fun than just pasting code) edit: I'm sorry, first hit of the forum search gave me the answer. (1 Reply)
Discussion started by: intelinside
1 Replies

2. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies

3. Programming

regarding string length

Helo, I have character array of sixe 128 char filename now I have one problem that when I enter filename as nothing I got value as " " ",`\0` " . when I find this string length ( " ",`\0`) as 1(one). actually I want to make this length as zero. so what should I do (10 Replies)
Discussion started by: amitpansuria
10 Replies

4. Shell Programming and Scripting

need help testing for length of variable

Hello, I'm new to shell scripting and need a little help please. I'm working on a script that asks the user to input a name that can be 1 to 12 alphanumeric characters and can have dots(.) dashes(-) and spaces. I want to test that the answer is valid and if not make the user try again. I have no... (4 Replies)
Discussion started by: wlewis
4 Replies

5. Shell Programming and Scripting

testing length

I've never tested file length with shell before and I'm having problems. What I'm doing is testing $filename and $filename.bak to see if there is a difference. so I use 'diff' for that and send it to an output file 'dif.txt'. Now I wanna see if the length of 'dif.txt' is zero or not. that's where... (1 Reply)
Discussion started by: astonmartin
1 Replies

6. UNIX for Dummies Questions & Answers

length of string

Hi lets say i have a variable output="string" how can you find the length of the string contained in this variable? i guess "wc" cannot be used. its only for files. (8 Replies)
Discussion started by: silas.john
8 Replies

7. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies

8. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies

9. UNIX for Dummies Questions & Answers

length of the string

Hi all, pls help me in finding the length of the given string, do we need to write a code seperately or is there any command?? pls help. (3 Replies)
Discussion started by: vasikaran
3 Replies

10. Shell Programming and Scripting

Testing the last character in a string

Hi In the shell scripted I'm trying to write! I would like to test the last character in a string. The string is a path/directory and I want to see if the last character is a '/'. The string (path/directory) is inputted by a user. If the '/' character isn't present then I want to be able to... (11 Replies)
Discussion started by: dbrundrett
11 Replies
Login or Register to Ask a Question