-1

In a bash script the user enters an 8-digit number after being prompted for input.

echo "Enter an 8-digit number."
read NUM

After this a function is executed that generates a single-digit random number.

main() {
GEN="$(shuf -i 0-9 -n 1)"
echo "$GEN"
}
main

What I need to know how to do is, I need the number that is randomly generated to be compared with each individual integer from the user input. So first the 1's place is compared, then the 10's place compared, then the 100's, and so on and so forth until all 8 digits have been compared.

My question is, how can I compare the generated number with a specific place value in the user input and once that number has been matched, make the function compare with the next place value?

(EDIT)

This script is what I need to be able to do with each individual place value in the user input in sequence.

#!/bin/bash
clear

echo "Enter a 3-digit number."
read Num

main() {

Gen=$(shuf -i 0-999 -n 1)
echo "$Gen"

if [ "$Gen" == "$Num" ]
then
echo "Match found!: $Gen"
sleep 3
clear
exit 0
fi

}


while true
do
main
done
  • If you're asking how to extract the ith character from a string, bash supports the syntax ${NUM:$i:1} – steeldriver May 25 '16 at 1:03
  • Can you explain what you mean by "ith character" please? – Yokai May 25 '16 at 2:05
  • First character: i=0, second character: i=1 and so on – steeldriver May 25 '16 at 2:19
  • So if I wanted to compare the generated number with each place value in this number: 12345678, how would I use this syntax? – Yokai May 25 '16 at 2:40
  • Is it for learning purposes or functionality? – Jacob Vlijm May 25 '16 at 19:56
1

The bash shell supports the syntax ${s:$i:1} to refer to the ith character of string s (strictly speaking, a substring of length 1 starting at position $i), so you can do something like

Num=13427598

for ((i=0;i<${#Num};i++)); do 

  Gen=$(shuf -i 0-9 -n 1)

  if (($Gen == ${Num:$i:1}))
  then 
    echo "$Gen matches at position $i"
  else 
    echo "$Gen doesn't match"
  fi

done

If you want to loop over random digits until you find a match, you could do something like

for ((i=0;i<${#Num};i++)); do 

  while :
  do
    Gen=$(shuf -i 0-9 -n 1)

    n=$((${#Num}-i-1)) # the string index, starting from least significant digit

    if (($Gen == ${Num:$n:1}))
    then
      echo "Matched $Gen at position $((10**i))" 
      break
    fi

  done

done

For efficiency, you might want to consider replacing $(shuf -i 0-9 -n 1) with $((RANDOM % 10)) which uses the shell built-in RANDOM in place of external function shuf, unless you believe that the distribution of integers produced by shuf is important to your application: the final script is then

#!/bin/bash

read -p "Enter a number: " Num

for ((i=0;i<${#Num};i++)); do 

  while :
  do
    Gen=$(shuf -i 0-9 -n 1)

    n=$((${#Num}-i-1)) # the string index, starting from least significant digit

    if (($Gen == ${Num:$n:1}))
    then
      echo "Matched $Gen at position $((10**i))" 
      break
    fi

  done

done

and testing it we get

$ ./number.sh 
Enter a number: 16283690    
Matched 0 at position 1
Matched 9 at position 10
Matched 6 at position 100
Matched 3 at position 1000
Matched 8 at position 10000
Matched 2 at position 100000
Matched 6 at position 1000000
Matched 1 at position 10000000
  • line 17: syntax error near unexpected token `done' The extra "if" causes the error. – Yokai May 25 '16 at 3:20
  • Did you run that code before posting? – Yokai May 25 '16 at 3:21
  • I have updated the question to be more clear on what I am asking. I feel like what you have here is close but it is still not what I am looking for. I'll keep testing things in the meantime. – Yokai May 25 '16 at 3:41
  • @Yokai no I ran the correct code ;) - thanks for noticing the extra if which I have now corrected, as well as updating with a suggested answer for your modified question – steeldriver May 25 '16 at 14:46
  • I have been playing around with this and what you posted works but it only generates for the first place value in the user input. With the number 12345678, I need it to compare with 1, and then with 2, and then with 3, and then with 4, etc. But what it is doing is printing the first number and just adding a 0 each time instead of matching and ignoring the previous place value. I cannot help but assume I cannot explain clearly enough what it is I am trying to do. – Yokai May 25 '16 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.