strings in shell script!

hi ppl,
I have a basic doubt as to how shell treats strings.
e.g:given a line of data like this "5","6","45","77","89"
extracting the value from each field to a variable will conatin a string("5") or just the number?
If it conatins "5", how do we perform mathematical operations with that variable?

The best bet is to test it:

# This will work for strings and integers:
echo VARIABLE = ${VARIABLE}
# This will work for integers:
if [ ${VARIABLE} -gt 4 ]; then
  echo "VARIABLE is greater than 4."
else
  echo "VARIABLE is less than 5."
fi

Does the script object to the data or not?

I suppose your problem comes from your assumption that "strings" and "numbers" are different data types like in other programming languages.

But shells do NOT have data types as all. You can create a variable of type "integer", but it will be merely an ordinary string with only digits allowed. On the other hand you could create a string variable and use it as a number as long as its contains only characters which form a number. For instance the following lines are all syntactically legal:

typeset foo="5"        # create a string variable
foo=(( foo + 2 ))      # use it as a number

typeset -i bar=5              # create an "integer" variable
typeset string="abc${bar}def" # use the integer as a string

Notice the second example. If i had tried to add some character to the $bar variable the shell would complain. This is because with "typeset -i" i declared that i want the variable only to contain characters which form an integer. This doesn't stop me from using the variable as a string like you see in the concatenation of $string.

This becomes clear in this example:

typeset -i foo=3          # create an "integer"
foo="${foo}5"             # concatenate this integer like a string
print - "$foo"            # will give "35"

I hope this helps.

bakunin

Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.