LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 11-25-2012, 12:35 PM   #1
LBP74
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Rep: Reputation: Disabled
Question Need help numbering my arguments


I have to write a program that precedes each argument with the line number.

while [ "$#" -ne 0 ]
do

echo $# :"$1"
shift

done
This is what I have but the numbering is in reverse order

]$ prargs a b c d
4 :a
3 :b
2 :c
1 :d
This is my output, I have tried different things but nothing works
 
Old 11-25-2012, 02:37 PM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hello LBP74, welcome to LQ,

your shift command decrements the number of arguments. And this are your linenumbers, decremented from 4 down to 1.

You could as an alternative set a variable which you increment in each step of the loop.

this would work:
Code:
#!/bin/bash

a=1
while [ "$#" -ne 0 ]; do
        echo "$a":$1
        let a=a+1
        shift
done
Markus

Last edited by markush; 11-25-2012 at 02:59 PM. Reason: code inserted
 
Old 12-05-2012, 04:05 PM   #3
LBP74
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by LBP74 View Post
I have to write a program that precedes each argument with the line number.

while [ "$#" -ne 0 ]
do

echo $# :"$1"
shift

done
This is what I have but the numbering is in reverse order

]$ prargs a b c d
4 :a
3 :b
2 :c
1 :d
This is my output, I have tried different things but nothing works
I actually figured out this problem

i=1
while [ "$#" -ne 0 ]
do

echo $i :"$1"
i=$((i + 1))
shift

done
~
I just had to put in a variable to help with the counting of the lines!
 
  


Reply



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
[SOLVED] page numbering in LaTeX edgjerp Linux - Software 2 09-26-2010 04:01 AM
numbering the lines punj33 Programming 9 07-21-2009 06:39 PM
OpenOffice numbering Q dianat100 Linux - Software 2 11-06-2005 12:58 PM
Inode numbering zaheer031 Linux - Newbie 1 10-20-2004 07:47 AM
Drive re-numbering naismith Linux - Hardware 1 03-19-2004 09:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:48 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration