Find the answer to your Linux question:
Results 1 to 3 of 3
Problem 1: changing == to = in the if statements below will ALWAYS result in the if block not being executed. == is NOT the same as = despite all ...
  1. #1
    Linux Newbie
    Join Date
    Mar 2005
    Location
    de_dust
    Posts
    115

    Bash not bahving

    Problem 1: changing == to = in the if statements below will ALWAYS result in the if block not being executed. == is NOT the same as = despite all the documentation I can find.

    Problem 2: the if block on the '$doit' test will NEVER be executed regardless of satisfying the conditions via the 'read' command above


    packages="test";
    doit="false";

    if (( $# >= 2 )); then
    if (( "${1:0:1}" == "y" )); then
    doit="true";
    fi
    fi

    if (( "$doit" == "false" )); then
    echo "Continue?";
    read ans;

    if (( "${ans:0:1}" != "y" )); then
    echo NO!
    exit
    fi
    fi

    echo YEAH!

  2. #2
    drl
    drl is online now
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    I changed your code and added 2 debugging tools: the "set" commands near the top. I recommend that you add these two to your original script, and then compare the results to running this version.

    For the issue of "==" I suggest you read the man page for bash and look for the string "==". You should be able to find it in 2 places, one for strings, and one for arithmetic comparisons.

    I didn't test every combination of inputs, but it seemed to run correctly on the ones I did test:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate debugging.
    
    echo
    set -o nounset
    set -x
    
    packages="test";
    doit="false";
    
    if (( $# >= 2 ))
    then
      if [[ "${1:0:1}" == "y" ]]
      then
        doit="true";
      fi
    fi
    
    if [[ "$doit" == "false" ]]
    then
      echo "Continue?";
      read ans;
    
      if [[ "${ans:0:1}" != "y" ]]
      then
        echo NO!
        exit
      fi
    fi
    
    echo YEAH!
    
    exit 0
    Please note that it is easier to read code and data when it is surrounded by CODE tags.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    if you want to use [ to test for integers, quote your variables
    Code:
    if [ "$#" -gt 2 ];then
     .....
    fi

Posting Permissions

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