1

I am writing a script that takes a 3-digit number and checks if they are unique and produces files with each digit, for example if I run "myscript 123" it will generate three files: file1, file2, and file3 BUT if I ran "my script 121" it will generate file1 and file2

I need help to look at the argument and compare each digit, could anyone give me a hand?

This is what I have written so far:

if [ $1 -gt 99 ] && [ $1 -lt 1000 ]
then
   echo "Your three digit number: $1"
else
   echo "please restart and enter a 3-digit number"
   exit
fi
Henk Langeveld
  • 8,088
  • 1
  • 43
  • 57
PhillToronto
  • 93
  • 1
  • 2
  • 13

3 Answers3

2

My biggest challenge though is to check $1 and see what each digit is, I am clueless

There are several ways to retrieve each characted from a string. For example, you can split them up into an array with the help of fold:

$ IN="123"
$ DIGITS=($(echo $IN | fold -w1))
$ echo ${DIGITS[0]}
1
$ echo ${DIGITS[1]}
2

Or you can use variable substring extraction (see docs):

$ IN="123"
$ echo ${IN:0:1}
1
$ echo ${IN:1:1}
2

Or, you can use let to perform basic modulo operations:

$ IN="123"
$ let A="IN % 10"  # get last digit (3rd)
$ ((IN /= 10))     # divide by 10 (which discards last digit)
$ let B="IN % 10"  # get last digit (2nd)
$ ((IN /= 10))     # divide by 10 (which discards last digit)
$ let C="IN % 10"  # get last digit (1st)
$ echo $A
3
$ echo $B
2
$ echo $C
1
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
  • Thanks @Shawn Just to claryfy, my 3 digit number being allocated at $1: echo ${$1:0:1} to display the first digit echo ${$1:1:1} to display the second digit echo ${$1:1:2} to display the third digit? – PhillToronto Aug 03 '12 at 18:40
  • oops, sorry, the 3rd digit would be echo ${$1:1:2} ? – PhillToronto Aug 03 '12 at 18:43
  • Amazing, the third one will allow me to do it within my limited knowledge, thank you so much :) – PhillToronto Aug 03 '12 at 18:46
  • for variable substring extraction, the first field is the var name, the second the position, and the third the length to extract -- so you want `${1:2:1}` for the third char. (I've edited the post to link to some docs which you might find useful. – Shawn Chin Aug 03 '12 at 18:46
  • @PhillToronto You're welcome. Good luck with your assignment. – Shawn Chin Aug 03 '12 at 18:47
  • Do you really need `${$1:2:1}`? `${1:2:1}` seems to do the trick for me – another.anon.coward Aug 03 '12 at 18:48
0

You can use the modulo and divide operators to extract the digits, then just compare them. Or you can use a combination of "echo", "head, and "tail" to extract the digits. For example:

digit2=$(echo $1|head -c 2|tail -c 1)
Jason
  • 1,059
  • 9
  • 13
0

This will just generate the file names, and I'm addressing this from a different angle. We're talking about digits here. So you will create no more than ten files, max. So, instead of isolating each digit, we just look for each one and see if it's present in the number given:

function files_for_digits {
    [[ $1 != [0-9][0-9][0-9] ]]  && 
        echo "specify a three digit number" >&2 && return 1

    typeset i
    for i in {0..9}; do
        [[ $1 == *$i* ]] && echo file$i
    done
}
Henk Langeveld
  • 8,088
  • 1
  • 43
  • 57