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 ...
- 12-09-2009 #1Linux 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!
- 12-11-2009 #2Linux Engineer
- 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:
Please note that it is easier to read code and data when it is surrounded by CODE tags.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
Best wishes ... cheers, drlWelcome - 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 )
- 12-14-2009 #3Linux 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


Reply With Quote