shanenin Posted April 25, 2005 Report Share Posted April 25, 2005 (edited) I am tring to understand what a "zero lenght" or non zero length string" string would be. That seems to be a string with no characters, but then it would be nothingexample[ -n $TEST ][ -z $VAR ] Edited April 25, 2005 by shanenin Quote Link to post Share on other sites
jcl Posted April 25, 2005 Report Share Posted April 25, 2005 "" Quote Link to post Share on other sites
shanenin Posted April 25, 2005 Author Report Share Posted April 25, 2005 (edited) is that meant to check if that is a set variable?In my mind these should be outputting 0 for the first one but 1 for the second one.shane@mainbox shane $ gg=""; [ -z $gg ]; echo $?0shane@mainbox shane $ gg=""; [ -n $gg ]; echo $?0 Edited April 25, 2005 by shanenin Quote Link to post Share on other sites
jcl Posted April 25, 2005 Report Share Posted April 25, 2005 (edited) is that meant to check if that is a set variable?Among other things.In my mind these should be outputting 0 for the first one but 1 for the second one.After variable substitution $ gg=""; [ -n $gg ]; echo $?becomes$ [ -n ]; echo $?and 'test' is defined such that if there's only one argument the exit status is 0 if that argument is non-null and 1 otherwise. -n is treated as a regular argument rather than operator in this case. Not sure if this is documented but it's POSIX behavior. It will work if you quote '$gg' in the test to ensure that you get at least the empty string$ gg=""; [ -z "$gg" ]; echo $?0$ gg=""; [ -n "$gg" ]; echo $?1 Edited April 25, 2005 by jcl Quote Link to post Share on other sites
shanenin Posted April 25, 2005 Author Report Share Posted April 25, 2005 would test -z $VAR be mainly used to check if a variable is already set? . Quote Link to post Share on other sites
jcl Posted April 25, 2005 Report Share Posted April 25, 2005 would test -z $VAR be mainly used to check if a variable is already set? . Dunno, I don't do much shell scripting. Quote Link to post Share on other sites
shanenin Posted April 25, 2005 Author Report Share Posted April 25, 2005 (edited) is that meant to check if that is a set variable?Among other things.sorry you already answered that(I missed it)I am sure you already knew these are equal expressionstest -z $var[ -z $var ] Edited April 25, 2005 by shanenin Quote Link to post Share on other sites
jcl Posted April 25, 2005 Report Share Posted April 25, 2005 is that meant to check if that is a set variable?Among other things.sorry you already answered that(I missed it)[/q]I think I added that in an edit while you were posting. One of these days I'll learn to write my posts all at once instead of doings edits for half an hour Quote Link to post Share on other sites
shanenin Posted April 26, 2005 Author Report Share Posted April 26, 2005 Among other things.I found one of those other things. #!/bin/bashRESPONSE=while [ -z "$RESPONSE" ];do  echo "Enter the name of the directory where your files are located:\c "  read RESPONSE  if [ ! -d "$RESPONSE" ]; then    echo "ERROR: please enter a directory pathname."    RESPONSE=  fidoneaccording to the book, my output should beEnter the name of the directory where your files are located:but I am gettingEnter the name of the directory where your files are located:\cI thought that the "\" worked as quoting. So that would disable a special character? the letter "c" does not seem to be a special character Quote Link to post Share on other sites
jcl Posted April 26, 2005 Report Share Posted April 26, 2005 (edited) Unix uses backslash for two purposes: to disable special interpretation of some characters (quotation marks, spaces, etc) and to enable special interpretation of other characters. For example, '\n' is often interpreted as a newline character and \t as a (horizontal) tab.In your example, '\c' is supposed to prevent echo from printing a trailing newline. However, that's a non-standard feature and support varies from shell to shell and platform to platform. bash and GNU echo will enable backslash sequence processing if you pass the '-e' option to echo.echo -e "Enter the name of the directory where your files are located:\c "Officially if you need formatted output you're supposed to use the 'printf' utility. printf was created because "irreconcilable differences in the various versions of echo extant" made it impossible to standardize any formatting options for echo. Edited April 26, 2005 by jcl Quote Link to post Share on other sites
shanenin Posted April 26, 2005 Author Report Share Posted April 26, 2005 so \c must not be supported in bash, is that what you are saying? Quote Link to post Share on other sites
jcl Posted April 26, 2005 Report Share Posted April 26, 2005 so \c must not be supported in bash, is that what you are saying? The echo built-in ignores it by default, but it is supported via -e. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.