LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shell scripting: variable problem (https://www.linuxquestions.org/questions/programming-9/shell-scripting-variable-problem-284064/)

XST1 01-30-2005 10:19 AM

shell scripting: variable problem
 
I'm writing code that takes the arguments presented to the script and prints them back out again. heres my code:

Code:

#!/usr/bin/bash

count=0
number=$#

while [ $count -lt $number ]
do
        count='expr $count + 1'
       
        token='$'$count
       
        echo $token
       
        shift
done

for some reason count never increses by 1 but instead contains expr $count + 1 and token always prints $expr $count + 1. Because of this, my while loop only executes once then crashes. Anyone have any ideas?

Duudson 01-30-2005 10:27 AM

Code:

count=`expr $count + 1`

XST1 01-30-2005 10:35 AM

that fixed it, but i noticed another thing.... when i do echo $token, it will print out $1 instead of actually printing out the value in $1. Any way to fix that?

homey 01-30-2005 11:04 AM

Try this...
Code:

#!/bin/bash
#Example: ./test 5
count=0
number=$1
while [ $count -lt $number ]
do
  count=`expr $count + 1`
  token=$count
  echo $token
  shift
done


Duudson 01-30-2005 11:26 AM

Code:

#!/bin/bash

count=0
number=$#

while [ $count -lt $number ]
do
        count=`expr $count + 1`
        echo $1
        shift
done


Duudson 01-30-2005 11:32 AM

or

Code:

#!/bin/bash

count=0
number=$#

while [ $count -lt $number ]
do
        count=`expr $count + 1`

        token=${!count}

        echo $token

done


XST1 01-30-2005 01:06 PM

when we say number=$# what does $# imply? What does it mean?

Duudson 01-30-2005 01:45 PM

Quote:

Originally posted by XST1
when we say number=$# what does $# imply? What does it mean?
how many arguments were given


All times are GMT -5. The time now is 10:07 AM.