+ Reply to Thread
Page 1 of 10 1 2345 ... Last
Results 1 to 25 of 241
  1. Senior Member ChooseLife's Avatar
    Join Date
    Feb 2011
    Location
    runlevel 3
    Posts
    926

    Certifications
    BCSc Network Security, VCP, MCSA:Sec, CCNA:Sec, GIAC GSEC, Sec+, ITIL-f
    #1

    Lightbulb Linux question of the day

    Let's try playing this...

    Question #1: Why was the echo command executed?

    Code:
    $ if [ false ]; then echo "That was a true statement"; fi
    I will post the answer in 24 to 48 hours depending on the number of responses this thread gets...
    Reply With Quote Quote  

  2. SS -->
  3. Junior Member
    Join Date
    Feb 2013
    Posts
    6
    #2
    The [] implement the test command. The test command sees false as a string not a command. Because it sees it as a string, the test command tests to see if it is non-zero length. Because it has length, the test command returns a true. Therefore the echo command is executed. You could make this work right by just leaving the brackets off.
    Reply With Quote Quote  

  4. Junior Member
    Join Date
    Feb 2013
    Posts
    6
    #3
    Keep these questions coming. It makes for great studying.
    Reply With Quote Quote  

  5. 1337sauce
    Join Date
    Jul 2011
    Location
    Ze South
    Posts
    1,539

    Certifications
    BS, Linux+, Security+, LPIC-1, MCSE Server 2012, MCSE Desktop, MCSA Server 2008, MCTS 70-[415,681], MCTS 74-409, VCA-DCV, Novell CLA/DCTS/CNS, HDI CSR
    #4
    I enjoy using the test parameters for the conditional expressions in loops, i.e.

    #!/bin/bash
    file=/path/to/file.txt

    if [ -f $file ]; then
    echo "$file exists, exiting"
    else
    echo "$file does not exist, creating"
    touch $file
    echo "$file created, exiting script"
    fi
    Reply With Quote Quote  

  6. Senior Member
    Join Date
    Feb 2012
    Posts
    2,426
    #5
    Quote Originally Posted by LinuxInAlaska View Post
    The [] implement the test command. The test command sees false as a string not a command. Because it sees it as a string, the test command tests to see if it is non-zero length. Because it has length, the test command returns a true. Therefore the echo command is executed. You could make this work right by just leaving the brackets off.
    Interesting.. I will have to look closer. When I looked at it, I thought that it was because/bin/false returns a non-zero exit code which is evaluated to true.
    Reply With Quote Quote  

  7. Senior Member ChooseLife's Avatar
    Join Date
    Feb 2011
    Location
    runlevel 3
    Posts
    926

    Certifications
    BCSc Network Security, VCP, MCSA:Sec, CCNA:Sec, GIAC GSEC, Sec+, ITIL-f
    #6
    Quote Originally Posted by paul78 View Post
    Interesting.. I will have to look closer. When I looked at it, I thought that it was because/bin/false returns a non-zero exit code which is evaluated to true.
    I was about to post the answer but now that there are different perspectives, I don't want to spoil it, let's keep the conversation going...
    Reply With Quote Quote  

  8. 1337sauce
    Join Date
    Jul 2011
    Location
    Ze South
    Posts
    1,539

    Certifications
    BS, Linux+, Security+, LPIC-1, MCSE Server 2012, MCSE Desktop, MCSA Server 2008, MCTS 70-[415,681], MCTS 74-409, VCA-DCV, Novell CLA/DCTS/CNS, HDI CSR
    #7
    The expression resulted in an exit status of 0, resulting in the echo
    Reply With Quote Quote  

  9. Senior Member ChooseLife's Avatar
    Join Date
    Feb 2011
    Location
    runlevel 3
    Posts
    926

    Certifications
    BCSc Network Security, VCP, MCSA:Sec, CCNA:Sec, GIAC GSEC, Sec+, ITIL-f
    #8
    Quote Originally Posted by paul78 View Post
    Interesting.. I will have to look closer. When I looked at it, I thought that it was because/bin/false returns a non-zero exit code which is evaluated to true.
    That is actually a good thought, inconsistencies between C-style and bash-style true/false can be quite messy.

    Can you prove it for this example though? What happens if you replace false with true?
    Reply With Quote Quote  

  10. Senior Member ChooseLife's Avatar
    Join Date
    Feb 2011
    Location
    runlevel 3
    Posts
    926

    Certifications
    BCSc Network Security, VCP, MCSA:Sec, CCNA:Sec, GIAC GSEC, Sec+, ITIL-f
    #9
    Quote Originally Posted by lsud00d View Post
    The expression resulted in an exit status of 0, resulting in the echo
    Which expression,
    Code:
    false
    or
    Code:
    [ false ]
    ?

    Reply With Quote Quote  

  11. Junior Member
    Join Date
    Feb 2013
    Posts
    6
    #10
    Check the "info coreutils test invocation" page, it explains what is happening in two different locations. The info page explains that if there is only one argument in the expression it will return true unless the argument is null and then it will return false. Also it says that if the argument is a string alone, it treats the test expression just like "-n String​"
    Reply With Quote Quote  

  12. Senior Member
    Join Date
    Feb 2012
    Posts
    2,426
    #11
    Ahh. I assume then that my premise only works if command substitution was used. I.e. 'false instead of false.

    @chooselife- I would have to agree with @linuxinalaska then. To your other question about using/bin/true- that command also returns exit code of zero as I recall so the echo is not executed is command substitution was used in the expression.
    Reply With Quote Quote  

  13. 1337sauce
    Join Date
    Jul 2011
    Location
    Ze South
    Posts
    1,539

    Certifications
    BS, Linux+, Security+, LPIC-1, MCSE Server 2012, MCSE Desktop, MCSA Server 2008, MCTS 70-[415,681], MCTS 74-409, VCA-DCV, Novell CLA/DCTS/CNS, HDI CSR
    #12
    Quote Originally Posted by ChooseLife View Post
    Code:
    [ false ]
    It can be [ abc ] or [ 123 ]...because it's ([ ended by ]) aliased to test which is simply evaluating it as a string, which returns exit status 0 and executes the echo
    Reply With Quote Quote  

  14. Senior Member ChooseLife's Avatar
    Join Date
    Feb 2011
    Location
    runlevel 3
    Posts
    926

    Certifications
    BCSc Network Security, VCP, MCSA:Sec, CCNA:Sec, GIAC GSEC, Sec+, ITIL-f
    #13
    Quote Originally Posted by paul78 View Post
    @chooselife- I would have to agree with @linuxinalaska then. To your other question about using/bin/true- that command also returns exit code of zero as I recall so the echo is not executed is command substitution was used in the expression.
    Yep. The other way to look at it if the 0s and 1s were reversed in this case, then
    Code:
    $ if [ true ]; then echo "That was a true statement"; fi
    would produce the opposite result, but they both do the same
    Reply With Quote Quote  

  15. Senior Member ChooseLife's Avatar
    Join Date
    Feb 2011
    Location
    runlevel 3
    Posts
    926

    Certifications
    BCSc Network Security, VCP, MCSA:Sec, CCNA:Sec, GIAC GSEC, Sec+, ITIL-f
    #14
    Quote Originally Posted by lsud00d View Post
    It can be [ abc ] or [ 123 ]...because it's ([ ended by ]) aliased to test which is simply evaluating it as a string, which returns exit status 0 and executes the echo
    You got it
    Reply With Quote Quote  

  16. Senior Member ChooseLife's Avatar
    Join Date
    Feb 2011
    Location
    runlevel 3
    Posts
    926

    Certifications
    BCSc Network Security, VCP, MCSA:Sec, CCNA:Sec, GIAC GSEC, Sec+, ITIL-f
    #15
    So, the correct answer is:
    Quote Originally Posted by LinuxInAlaska View Post
    The [] implement the test command. The test command sees false as a string not a command. Because it sees it as a string, the test command tests to see if it is non-zero length. Because it has length, the test command returns a true. Therefore the echo command is executed. You could make this work right by just leaving the brackets off.
    It is so complete that I have nothing to add to it

    The reason for this question is that like lsud00d said, it is often used in cases like if [ -f $file ]; and so people could forget that it does not work as "if [ boolean-expr ]" but rather as "if test expression" which by default does a string evaluation.

    P.S. And like LinuxInAlaska said, this would work correctly:


    Code:
    $ if false ; then echo "That was a true statement"; fi
    Reply With Quote Quote  

  17. 1337sauce
    Join Date
    Jul 2011
    Location
    Ze South
    Posts
    1,539

    Certifications
    BS, Linux+, Security+, LPIC-1, MCSE Server 2012, MCSE Desktop, MCSA Server 2008, MCTS 70-[415,681], MCTS 74-409, VCA-DCV, Novell CLA/DCTS/CNS, HDI CSR
    #16
    Very interesting! There are so many little things about linux to remember...I find myself revisiting topics often.

    For instance, I'm still iffy on single/double quotes and ticks when using them in scripts. I get it right most of the time but I need to do better on remembering which to use when
    Reply With Quote Quote  

  18. Senior Member ChooseLife's Avatar
    Join Date
    Feb 2011
    Location
    runlevel 3
    Posts
    926

    Certifications
    BCSc Network Security, VCP, MCSA:Sec, CCNA:Sec, GIAC GSEC, Sec+, ITIL-f
    #17
    Ok, here's a simple one... If you certainly know the answer, let the others think about it first


    Question #2: Suppose there is a TrashCan directory that you want to empty. Why is the following approach bad? How would you fix it?

    Code:
    $ cd TrashCan; rm -rf *
    Reply With Quote Quote  

  19. Senior Member
    Join Date
    Feb 2012
    Posts
    2,426
    #18
    Good one.. usually, you only need to do that wrong once to realize why...
    Reply With Quote Quote  

  20. 1337sauce
    Join Date
    Jul 2011
    Location
    Ze South
    Posts
    1,539

    Certifications
    BS, Linux+, Security+, LPIC-1, MCSE Server 2012, MCSE Desktop, MCSA Server 2008, MCTS 70-[415,681], MCTS 74-409, VCA-DCV, Novell CLA/DCTS/CNS, HDI CSR
    #19
    I cringe at rm -rf....never made that mistake, but mannnnn I don't think you can hose something that easily doing anything else in the world!
    Reply With Quote Quote  

  21. Senior Member boredgamelad's Avatar
    Join Date
    Feb 2012
    Location
    Orange County, CA
    Posts
    361

    Certifications
    CISSP, CCNA, Network+, Security+, Project+, some CIWs
    #20
    My favorite rm -rf joke is that it stands for "read mail really fast".

    I like the idea behind this thread! I am a bit of a Linux newbie so I will be checking back here often.
    Last edited by boredgamelad; 02-08-2013 at 04:04 PM.
    Reply With Quote Quote  

  22. linux addict marco71's Avatar
    Join Date
    Jan 2008
    Location
    Eastern Europe
    Posts
    149

    Certifications
    Novell CLA, Cisco CCNA/CCNA:Sec, MCP (2003srv), ITIL v3f, some win/lin/net Brainbench certs (expired), B.S./M.S. in Comp.Sci
    #21
    Quote Originally Posted by ChooseLife View Post

    Question #2: Suppose there is a TrashCan directory that you want to empty. Why is the following approach bad? How would you fix it?

    Code:
    $ cd TrashCan; rm -rf *
    Second command, rm -rf * , (because of ; separator), will always be executed, no matter if first command fails or not (for example, if TrashCan is a hidden folder, then cd TrashCan fails, shoud be cd .TrashCan)... and will erase all current folder if cd command fails
    correct would be to use && separator for the two commands, in this way, second command is executed only when first one returns true:
    Code:
    $ cd TrashCan && rm -rf *
    [/QUOTE]
    An opposite to && is the || separator; in this case second command is executed only when first one fails

    EDIT:
    sorry, didnt see your first thought ... I should have let the others to think at it first
    Last edited by marco71; 02-08-2013 at 06:56 PM.
    Reply With Quote Quote  

  23. Junior Member
    Join Date
    Feb 2013
    Posts
    6
    #22
    I agree with Marco71 on why it is a bad idea and what can go wrong. I would take a different route to perform the delete and just do

    Code:
    rm -rf TrashCan/*
    If you fat finger the name TrashCan, it would simply come back with a "No such file or directory" error.
    Reply With Quote Quote  

  24. 1337sauce
    Join Date
    Jul 2011
    Location
    Ze South
    Posts
    1,539

    Certifications
    BS, Linux+, Security+, LPIC-1, MCSE Server 2012, MCSE Desktop, MCSA Server 2008, MCTS 70-[415,681], MCTS 74-409, VCA-DCV, Novell CLA/DCTS/CNS, HDI CSR
    #23
    I'm a fan of absolute paths for stuff like this
    Reply With Quote Quote  

  25. Senior Member ChooseLife's Avatar
    Join Date
    Feb 2011
    Location
    runlevel 3
    Posts
    926

    Certifications
    BCSc Network Security, VCP, MCSA:Sec, CCNA:Sec, GIAC GSEC, Sec+, ITIL-f
    #24
    Ok, so the correct answer is:

    Quote Originally Posted by marco71 View Post
    Second command, rm -rf * , (because of ; separator), will always be executed, no matter if first command fails or not (for example, if TrashCan is a hidden folder, then cd TrashCan fails, shoud be cd .TrashCan)... and will erase all current folder if cd command fails
    One more case where cd may fail is insufficient rights to access the target folder.

    Quote Originally Posted by marco71 View Post
    correct would be to use && separator for the two commands, in this way, second command is executed only when first one returns true:
    Code:
    $ cd TrashCan && rm -rf *
    Absolutely. And while cd can be avoided altogether as @LinuxInAlaska suggested, this approach can be generalized to any set of chained commands. I wanted to highlight this case because of a potential habit of mis-using a semicolon when in fact the sequence of command is logically conditioned.
    Last edited by ChooseLife; 02-09-2013 at 09:59 AM.
    Reply With Quote Quote  

  26. Senior Member ChooseLife's Avatar
    Join Date
    Feb 2011
    Location
    runlevel 3
    Posts
    926

    Certifications
    BCSc Network Security, VCP, MCSA:Sec, CCNA:Sec, GIAC GSEC, Sec+, ITIL-f
    #25
    Question #3. So we established that one safe way to clean up a directory is to execute:
    Code:
    $ cd TrashCan && rm -rf *
    Now suppose TrashCan's permissions do not allow the user access its content.
    Is there anything wrong with the following line?
    Code:
    $ sudo cd TrashCan && rm -rf *
    Reply With Quote Quote  

+ Reply to Thread
Page 1 of 10 1 2345 ... Last

Social Networking & Bookmarks