0

I'm currently having simple syntax issues with the following bash shell script. I'm not sure what the syntax is for nesting an if statement into a while statement, or if it's even possible with bash shell scripting (new to all things linux):

#!/bin/bash

myCombo=$((RANDOM%99999+10000));
echo ${myCombo};

myCracker=00000;

while [ (($myCracker<=99999)) ]; do
    if [ $myCracker -eq myCombo ]
    then
        echo "The combination is " ${myCracker} " !"
    else [ $myCracker = $myCracker + 1 ]
    fi
done;

2 Answers 2

2

There were quite a few things wrong with your loop. I've made a number of improvements below:

while (( myCracker <= 99999 )); do
    if (( myCracker == myCombo )); then
        echo "The combination is $myCracker !"
        break
    else
        (( ++myCracker ))
    fi
done

As you're using bash, you can make use of (( arithmetic contexts )), which don't need enclosing in [. Within them, variable names are expanded, so do not require prefixing with $.

Note that your original logic will either loop indefinitely or never echo, which is probably a bug. If myCracker == myCombo is ever true, myCracker won't be incremented so the loop will never terminate. The break statement deals with this.

I left the else branch in deliberately to show the syntax but you could also remove it entirely:

while (( myCracker++ <= 99999 )); do
    if (( myCracker == myCombo )); then
        echo "The combination is $myCracker !"
        break
    fi
done

The break is still useful as it prevents the loop from continuing unnecessarily.

Sign up to request clarification or add additional context in comments.

3 Comments

[I added a break as it seems so appropriate, rollback if you don't like it] This really calls for a for loop :).
Thank you so much for that. This cleared up MANY syntax questions I had about Bash, and I feel as though Bash will come much easier for me with this new knowledge.
@gniourf_gniourf thanks, I was just in the process of adding something similar!
1

You can also use extended tests instead of arithmetic contexts if you do simple comparisions. They should be a little faster, and are more common.

while [[ $myCracker -le 99999 ]]; do
    if [[ $myCracker -eq $myCombo ]]; then
        echo "The combination is ${myCracker}!"
        break
    else
        ((myCracker++))
    fi
done

5 Comments

This adds even more knowledge for my skill-bag. Thanks so much for helping out! Looks like I was trying to mix some of this syntax together into a big messy cluster without knowing fully well the syntax rules for certain operations.
I'd suggest sticking with extended tests for everything, and use arithmetic contexts only for arithmetic operations, including incrementing by one. This is because extended tests are more common and handle both numbers (-eq) and strings (=).
[[ implicitly introduces an arithmetic context for both the right- and left-hand sides of the numerical operators. This causes problems with variables that evaluate to strings (instead of numbers) since they get evaluated a second time and end up being 0. Compare foo=bar; [[ $foo -eq 0 ]]; echo $? to bar=5; foo=bar; [[ $foo -eq 0 ]]; echo $? to foo=bar; [ $foo -eq 0 ]; echo $?.
No, you're wrong. one="text" two="$one" three="other text" [[ "$one" = "$two" ]]; echo $? # 0 [[ "$one" = "$three" ]]; echo $? # 1
You should use -eq for arithmetic comparisions, and "=" (or "==", which acts the same) for strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.