Find the answer to your Linux question:
Results 1 to 7 of 7
Look at this script #!/bin/bash ldc=""; if [ -x /sbin/ldconfig ]; then ldc="/sbin/ldconfig"; fi if test x$ldc = x; then which ldconfig 2>&1>/dev/null; if test $? -eq 0; then ldc=`which ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2004
    Location
    Israel
    Posts
    61

    Help me understanding this bash voodoo (newb question)


    Look at this script

    #!/bin/bash

    ldc="";
    if [ -x /sbin/ldconfig ]; then
    ldc="/sbin/ldconfig";
    fi
    if test x$ldc = x; then
    which ldconfig 2>&1>/dev/null;
    if test $? -eq 0; then
    ldc=`which ldconfig`;
    fi
    fi
    if test x$ldc = x; then
    echo ;
    echo "ldconfig is not available";
    echo "do not forget to run ldconfig manually";
    echo ;
    exit 1;
    fi
    cp /etc/ld.so.conf /tmp/ld.$$.so.conf;
    echo $1 >> /tmp/ld.$$.so.conf;
    echo ;
    echo Running $ldc;
    $ldc -f /tmp/ld.$$.so.conf;
    echo ;
    rm /tmp/ld.$$.so.conf;
    See the line I've marked bold.
    What does test x$ldc = x mean?
    Is it the same as test -z $ldc ?

    Thank you.

  2. #2
    Linux Newbie
    Join Date
    Apr 2005
    Location
    South America
    Posts
    152

    Re: Help me understanding this bash voodoo (newb question)

    Quote Originally Posted by hq4ever
    Look at this script

    What does test x$ldc = x mean?
    Is it the same as test -z $ldc ?
    Likely, it is the same, as basically the test is probing if $ldc is empty (or zero length) by appending it to x, then
    test x$ldc = x returns TRUE in case $ldc is empty, the same as doing
    test -z $ldc as you suggested

    Check out this script, it yields identical results wheter ldc equals a value or is empty

    Code:
    #!/bin/bash
    
    ldc="value"
    if test -z $ldc
    then echo EMPTY
    else echo NOT EMPTY
    fi
    
    if test x$ldc = x
    then echo EMPTY
    else echo NOT EMPTY
    fi
    
    ldc=""
    if test -z $ldc
    then echo EMPTY
    else echo NOT EMPTY
    fi
    
    if test x$ldc = x
    then echo EMPTY
    else echo NOT EMPTY
    fi

  3. #3
    Just Joined!
    Join Date
    May 2004
    Location
    Israel
    Posts
    61
    So why the "x$ldc = x", more sexy ? better readbility of the script ?
    How would you do it ?

    Thnx.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie
    Join Date
    Apr 2005
    Location
    South America
    Posts
    152
    Hehe,yeah, id say it was made to confuse newbies, or to look more obfuscated
    Id definitely use the -z option...

    Hope I was of any help, cheers

  6. #5
    Just Joined!
    Join Date
    May 2004
    Location
    Israel
    Posts
    61
    Thank you very much for the help.

    Cheers,
    Maxim.

  7. #6
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by hq4ever
    So why the "x$ldc = x", more sexy ? better readbility of the script ?
    How would you do it ?
    It's a UNIX technique to prefix a variable with a character instead of just looking at the variable to avoid ambiguities with arguments that begin with "-" that might be interpreted as an option or flag.

    eg.
    Code:
    test $ldc = "somevalue"
    If $ldc contained, say, "-x", you'd get a syntax error when the script ran.

  8. #7
    Just Joined!
    Join Date
    May 2004
    Location
    Israel
    Posts
    61
    Smart.

    Thank you for the clerifications.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •