LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-30-2005, 10:19 AM   #1
XST1
LQ Newbie
 
Registered: Feb 2004
Posts: 11

Rep: Reputation: 0
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?
 
Old 01-30-2005, 10:27 AM   #2
Duudson
Member
 
Registered: Dec 2004
Distribution: RHEL3, FC3
Posts: 53

Rep: Reputation: 15
Code:
count=`expr $count + 1`
 
Old 01-30-2005, 10:35 AM   #3
XST1
LQ Newbie
 
Registered: Feb 2004
Posts: 11

Original Poster
Rep: Reputation: 0
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?
 
Old 01-30-2005, 11:04 AM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
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
 
Old 01-30-2005, 11:26 AM   #5
Duudson
Member
 
Registered: Dec 2004
Distribution: RHEL3, FC3
Posts: 53

Rep: Reputation: 15
Code:
#!/bin/bash

count=0
number=$#

while [ $count -lt $number ]
do
        count=`expr $count + 1`
        echo $1
        shift
done
 
Old 01-30-2005, 11:32 AM   #6
Duudson
Member
 
Registered: Dec 2004
Distribution: RHEL3, FC3
Posts: 53

Rep: Reputation: 15
or

Code:
#!/bin/bash

count=0
number=$#

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

        token=${!count}

        echo $token

done
 
Old 01-30-2005, 01:06 PM   #7
XST1
LQ Newbie
 
Registered: Feb 2004
Posts: 11

Original Poster
Rep: Reputation: 0
when we say number=$# what does $# imply? What does it mean?
 
Old 01-30-2005, 01:45 PM   #8
Duudson
Member
 
Registered: Dec 2004
Distribution: RHEL3, FC3
Posts: 53

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scripting problem for importing Firefox files montylee Programming 6 06-03-2005 01:52 AM
shell scripting - using ${variable:=","} Steve Riley Linux - General 7 02-08-2005 05:07 AM
another cgi and shell scripting problem + webserver prinski Programming 6 04-08-2004 04:25 PM
Shell Scripting Problem linuxboy69 Linux - Software 2 03-31-2004 07:36 PM
problem in shell scripting visu Linux - Newbie 4 01-06-2004 04:16 PM


All times are GMT -5. The time now is 10:41 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration