strings in shell script!

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions strings in shell script!
# 1  
Old 08-30-2009
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?
# 2  
Old 08-30-2009
The best bet is to test it:
Code:
# 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?
# 3  
Old 09-06-2009
Quote:
Originally Posted by beetel
extracting the value from each field to a variable will conatin a string("5") or just the number?
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:

Code:
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:

Code:
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
This User Gave Thanks to bakunin For This Post:
# 4  
Old 09-06-2009
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to ignore mutiple strings when using shell script?

Hi All, I am trying to use below syntax to find ignore multiple locations while searching for a file. find / -name "$serviceitem" ! -size 0 2>&1 |egrep -v "tmp|docker|WinSxS|Permission|HISTORY|alternatives|bearer11ssl|manifest" I tried to assign all the ignore strings to one variable... (2 Replies)
Discussion started by: sravani25
2 Replies

2. Programming

Python or Shell script to Grep strings from input file and output in csv format

Hi Experts, I am writing a python script to grep string from file and display output in csv file as in attached screenshot https://drive.google.com/file/d/1gfUUdfmQma33tz65NskThYDhkZUGQO0H/view Input file(result_EPFT_config_device) Below is the python script i have prepared as of... (1 Reply)
Discussion started by: as7951
1 Replies

3. Shell Programming and Scripting

create an array which can store the strings from the user input in shell script

I want to create an array which can store the strings from the user input in shell script . example :- I want to store the 5 fruits name in a single array which the user provides . (1 Reply)
Discussion started by: Pkast
1 Replies

4. Shell Programming and Scripting

Find associated strings in a bash shell script

Hi together, unfortunately I am not a shell script guru - the following might touch the depths of awk, substr, split, regexps, where I am still fighting with - but as always the boss needs a fast solution :-( So: I have the following USER/PASSWORD-installation-config-file, from where I want to... (10 Replies)
Discussion started by: Sofie
10 Replies

5. Shell Programming and Scripting

Shell script to replace strings to and from a file

Hello All, I have 2 files 1 ) source file eg asasa 1.2.3.4 adfhsdfsdfasdf zxzxzx 2.3.4.56 dsadasdasdsadasd kjjkjkjk 30.3.4.5 asdsadsadsadsadsad vxcvxcvx 1.2.3.4 qwewqewqeqweqwe 2) patern file 1.2.3.4 A 2.3.4.56 B 30.3.4.5 C I need the source to be changed to asasa A... (2 Replies)
Discussion started by: nitinkgoud
2 Replies

6. Shell Programming and Scripting

Can we pass an array of strings from a Perl Program to a Shell Script?

Hi Folks, The subject is my question: Can we pass an array of strings from a Perl Program to a Shell Script? Please provide some sample code. Thanks ---------- Post updated at 11:52 PM ---------- Previous update was at 11:43 PM ---------- I got it. Its here:... (0 Replies)
Discussion started by: som.nitk
0 Replies

7. Programming

Returning Strings from C program to Unix shell script

Hi, I'm having a requirement where I need to call a C program from a shell script and return the value from the C program to shell script. I refered a thread in this forum. But using that command in the code, it is throwing an error clear_text_password=$(get_password) Error: bash:... (24 Replies)
Discussion started by: venkatesh_sasi
24 Replies

8. Programming

Returning Strings from C program to Unix shell script

Hi, Iam calling a C program from a Unix shell script. The (C) program reads encrypted username/password from a text file , decrypts and returns the decrypted string. Is there any way i can return the decrypted string to Unix shell program. My shell script uses the output of the program to... (11 Replies)
Discussion started by: satguyz
11 Replies

9. Shell Programming and Scripting

How to concatenate two strings or several strings into one string in B-shell?

like connect "summer" and "winter" to "summerwinter"? Can anybody help me? thanks a lot. (2 Replies)
Discussion started by: fontana
2 Replies
Login or Register to Ask a Question