ksh: what does x in front of something mean?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh: what does x in front of something mean?
# 1  
Old 10-26-2010
ksh: what does x in front of something mean?

hi,

i have stuff like

Code:
if [[ x$ip = x127.0.0.1 ]]; then

what does the x mean?

also, what does -z variable mean in an if statement>

thanks
# 2  
Old 10-26-2010
x doesn't have special meaning it is here just in case $ip is empty

-z means if empty (z stand for a zero length)
This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 10-26-2010
Quote:
Originally Posted by ctsgnb
it is just in case $ip is empty

-z means if empty (z stand for a zero length)
sorry, i am confused, if ip is empty and we did the check, it would fail anyway, so why would we worry about this x stuff?

thanks
# 4  
Old 10-26-2010
remove the x , make a test with an empty ip and see what happen ...



---------- Post updated at 10:09 PM ---------- Previous update was at 10:07 PM ----------




if $ip is empty :
Code:
[[ x$ip = x127.0.0.1 ]]
[[ x = x127.0.0.1 ]] ===> false

now if $ip is empty without x :
Code:
[[ $ip = 127.0.0.1 ]]
[[  = 127.0.0.1 ]] ===> Error

Note that this would not happen using double quote
Code:
[[ "$ip" = "127.0.0.1" ]]
[[ "" = "127.0.0.1" ]] ===> false

Ok course these behaviours may depends on your system OS and SHELL implementation, but this is what "could" be expected

Last edited by ctsgnb; 10-26-2010 at 05:17 PM..
This User Gave Thanks to ctsgnb For This Post:
# 5  
Old 10-26-2010
Too lazy to use ", safe if $ip is blank but not if it is "x x"!

Code:
if [[ "$ip" = "127.0.0.1" ]]; then

-z is empty string, see man test.
This User Gave Thanks to DGPickett For This Post:
# 6  
Old 10-26-2010
great answers guys, thanks
# 7  
Old 10-26-2010
if you remove the x, and $ip is empty, you get
Code:
if [[  = x127.0.0.1 ]]; then

which leads to an error.
the same with x and empty $ip:
Code:
if [[ x = x127.0.0.1 ]]; then

no errors here.

You can as well enclose $ip with double-quotes so if $ip is empty, you still have an argument to the left of the =:
Code:
if [[ "" = x127.0.0.1 ]]; then

or you can use that -z, but it only test for zero string, you can't use this if you need to check different IPs:
Code:
if [[ -z $ip ]]; then

says "if string $ip is zero-length, then..."
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Linux

How to add \ in front of $ in a script with vi?

Please help to provide command in vi to add \ in front of $ so it does not interpret next character. rm interfaces/DART/WEB-INF/classes/DART/util/TotalDisconnectUtil$1.class rm interfaces/DART/WEB-INF/classes/DART/util/TotalDisconnectUtil$2.class rm... (3 Replies)
Discussion started by: ywu081006
3 Replies

2. Shell Programming and Scripting

Terminal to the front no matter what

Is there a way to bring the terminal script to the front? I am running this script through OMCEdit which is then running it through Terminal. I have some dialog boxes (using osascript) and the dialog boxes are not coming to the front...Terminal bounces and I have to click on Terminal to see the... (1 Reply)
Discussion started by: mainegate
1 Replies

3. Shell Programming and Scripting

ksh - adding a dynamic value to the front of a line

Hi Forum, Im trying to write some code to read a file, take a certain dynamic value and write it back to the file at the front of every line. For example, file.txt is: SIMPLE=HELLO CONFIDENTIAL=false SENDER=false REQUIRED=true FEEDBACK=false UPDATE=false REQUIRED=false MAPPING=true... (9 Replies)
Discussion started by: ocelot
9 Replies

4. Shell Programming and Scripting

Appending the last few columns to the front

Hi consider this as the first line 00010015 MORGAN STANLEY & CO INCORPORATED N 110 INVESTAR 1 0001OT NJ 201-830-5055 01-Jan-1974 00:00:00 1 01-May-2008 00:00:00 05-Jun-2008 13:34:18 0001 - From SMSRun1_GIDQA02 Consider this as the second line 00010015 MORGAN STANLEY... (3 Replies)
Discussion started by: ragavhere
3 Replies

5. Shell Programming and Scripting

sh, ksh: command to remove front spaces from a string?

dear pro-coders, is there any command out there that takes out the front spaces from a string? sample strings: 4 members 5 members 3 members but it has to be like so: 4 members 5 members 3 members (3 Replies)
Discussion started by: pseudocoder
3 Replies

6. UNIX for Advanced & Expert Users

Front end on Unix

Hi, I would like to develop a user interface on Solaris. Can anybody throw some light on currently available software utilities/ packages..? Thanks in Advance .. JS (4 Replies)
Discussion started by: shibz
4 Replies
Login or Register to Ask a Question