LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-19-2007, 12:50 PM   #1
new_2_unix
LQ Newbie
 
Registered: Oct 2007
Posts: 26

Rep: Reputation: 15
how to read more than ten arguments of user input in a script


hi,

in a shell script we can refer to the arguments entered by the user on the command line simply with a $1, $2, $3 ... representing the first, second, third ... arguments entered by the user.

however, this works only if there are less than 10 arguments, because for argument 10 - the script interprets $10 as the value that $1 has and then append it with a 0.

in other words, if have a script which simply does an echo of $1, $2 and so on... the output would be like this

Code:
$./myscript foo1 bar2 ... ... arg9 arg10
> Argument1: foo1
> Argument2: bar2
...
...
> Argument10: foo10
Is there a way to tell the shell to interpret '10' as one number instead of evaluating $1 and then appending it with a 0? any guidance will be helpful. thanks!
 
Old 11-19-2007, 12:52 PM   #2
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie stretch
Posts: 1,599

Rep: Reputation: 97
Hya,

Is shift what you want??

cheers
 
Old 11-19-2007, 01:05 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
kaz2100 is right, but you can also enclose the positional parameter in brackets
Code:
${10}
 
Old 11-19-2007, 01:42 PM   #4
new_2_unix
LQ Newbie
 
Registered: Oct 2007
Posts: 26

Original Poster
Rep: Reputation: 15
i tried to do this with shift, it will work but then i also need a for loop that keeps on shifting the arguments to the left one by one (may be i'm wrong), i tried doing this but for some reason it wasn't working out properly. i could read the 10th argument, but then it kept on repeating it for the arguments after that, which means something is wrong with my loop. but the ${10} solves my problem completely, so i'll use that for now... but will try the shift later on. thanks to both of you!
 
Old 11-19-2007, 02:29 PM   #5
new_2_unix
LQ Newbie
 
Registered: Oct 2007
Posts: 26

Original Poster
Rep: Reputation: 15
on one system the ${10} works, but on another it results in a "bad substitution" error... is there a way to resolve this discrepancy? thanks!
 
Old 11-19-2007, 04:18 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by new_2_unix View Post
on one system the ${10} works, but on another it results in a "bad substitution" error... is there a way to resolve this discrepancy? thanks!
Yes, I remember it did not work - for example - in the Bourne shell /bin/sh on Unix systems and the only way to parse a number of arguments greater than 9 was by means of the shift command. You can try a for loop or even better
Code:
until [ -z "$1" ] ; do
   echo "$1"
   shift
done
I hope this helps.
 
  


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
Read list of files as arguments in a script garr0n Programming 10 10-25-2007 11:19 PM
shell script to read input from csv ip addresses? kr0m3 Programming 3 07-21-2007 09:51 AM
Shell script user input tuckermaddox Linux - Newbie 5 08-12-2004 04:14 AM
Masking user input in sh script? RMSe17 Linux - Software 3 07-20-2004 11:17 PM
User input using a BASH script... causticmtl Programming 5 07-13-2003 10:59 PM


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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration