Score:0

How to add a string to a number

gb flag

I have been recently trying to create a script that can convert a binary number to a decimal. This is what I've got so far:

#!/bin/bash

echo "Specify no of digits"
read digits

if [[ $digits == 1 ]]; then
        echo "Enter 1 st digit"
        read 1
elif [[ $digits == 2 ]]; then
        echo "Enter 1 st digit"
        read 1
        echo "Enter 2 nd digit"
        read 2
elif [[ $digits == 3 ]]; then
        echo "Enter 1 st digit"
        read 1
        echo "Enter 2 nd digit"
        read 2
        echo "Enter 3 rd digit"
        read 3
elif [[ $digits > 3 ]]; then
        echo "Enter 1 st digit"
        read 1
        echo "Enter 2 nd digit"
        read 2
        echo "Enter 3 rd digit"
        read 3
        for digitno in {4..$digits};
        do
                echo "Enter $digitno th digit"
                read $digitno
                ($nodigits++)
        done
echo "$4"
else
        echo "Please enter a valid no of digits. Type './binary_decoder.sh'"
        exit 1
fi

It's a pretty long script, I know. But please try and take the time to examine this script.

If you look at any of the read lines inside the if conditional, you will see that the variables to which the read statements are assigning the numbers to are themselves numbers. With Bash syntax, that will not work. I want for the variables to be like n1, n2, n3, n4... and so on. But, if you see inside the elif [[ $digits > 3 ]]; then statement, you can see that there is a for loop that allows for an infinite no of digits to be decoded. Now, I do not know any way to add the string n to the number in the variable $digitno. But I was wondering that if any of you can maybe figure out how to add the string n to the $digitno variable.

Any help will be appreciated.

FedKad avatar
cn flag
Google for "bash arrays".
pzkpfw avatar
us flag
This is not an Ubuntu question, it belongs on [Stack Overflow](https://stackoverflow.com/).
Zanna avatar
kr flag
@pzkpfw bash scripting is on topic here
Score:1
hr flag

You can add a string to a number using simple concatenation:

$ i=3
$ echo n$i
n3

however that doesn't help much with your real goal here, which seems to be how to assign an indeterminate number of user inputs to indexed variables.

As you've already discovered, you can't use variables named 1, 2, 3 etc. in a read command. Aside from the fact that bash variable names must at least begin with an alphabetic character or an underscore, the expansions $1, $2, $3 and so on are reserved for the shell's positional parameters.

If you really want to use $1 ... $n in your script, you can actually do so using the set shell builtin. Note that whereas POSIX only requires support for parameters up to $9, bash supports an arbitrary number (although for indexes above 9 you will need to use braces to disambiguate between, for example, ${10} as the 10th positional parameter and $10 as the concatenation of $1 with literal 0). For example:

#!/bin/bash

set --
while : ; do
  read -n1
  case $REPLY in
    [01]) set -- "$@" "$REPLY"
    ;;
    *) break
    ;;
  esac
done

for ((i=1; i<=$#; ++i)); do
  printf 'Digit #%d = %d\n' "$i"  "${!i}"
done

The user enters a sequence of 0 and 1 characters, terminating the sequence by hitting any other character (including newline):

$ ./bin2dec
1011010110
Digit #1 = 1
Digit #2 = 0
Digit #3 = 1
Digit #4 = 1
Digit #5 = 0
Digit #6 = 1
Digit #7 = 0
Digit #8 = 1
Digit #9 = 1
Digit #10 = 0

Alternatively, you could do essentially the same with a user-defined array:

#!/bin/bash

arr=()
while : ; do
  read -n1
  case $REPLY in
    [01]) arr+=("$REPLY")
    ;;
    *) break
    ;;
  esac
done

for ((i=0; i<${#arr[@]}; ++i)); do
  printf 'Digit #%d = %d\n' "$i"  "${arr[i]}"
done

Note the different indexing; although both arrays are zero-based, the zeroth element of $@ is reserved for the name of the script file.

Samanway Karjee avatar
gb flag
My 'Real goal' actually isn't to assign a variable with the name as a number. If you look at the code that i uploaded, you can see that i am trying to use a for loop that can make and assign different values and names an infinite number of times. So I was hoping that i could make a code that could add a string to a number some thing like this: `read ["n" + $x]` and if x's value was 1, it would assign the user inputed value to the variable n1 and so on and forth.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.