The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
List certain file in a folder and make list andrisetia UNIX for Dummies Questions & Answers 1 09-15-2008 02:21 PM
How to read arguments to make command vikashtulsiyan Shell Programming and Scripting 3 06-12-2008 05:35 AM
i want to list all command line arguments except naree Shell Programming and Scripting 3 03-31-2008 12:11 PM
Bash Shell - # of arguments DNAx86 Shell Programming and Scripting 1 01-10-2008 01:23 PM
Make new arguments for echo command Panteras Shell Programming and Scripting 0 11-29-2007 03:43 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-02-2009
jolecanard jolecanard is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 36
Make a list in bash out of arguments

Hello,

I have a very stupid/simple problem, but for some reason I cannot figure out...and I need your help!

I am writting a bash scrip that should be executed using "my_script X Y Z T" where X Y Z and T can be any string, but there can be any number of arguments.

I want my script to do a list from these inputs:

Code:
#!/bin/bash
 
i=1
while [ $i -le $# ]
do
list=$list" ./myfile."$i
i=`expr $i + 1`
done
echo $list

Using the above script, I got:
./myfile.1 ./myfile.2 ./myfile.3 ./myfile.4
instead of:
./myfile.X ./myfile.Y ./myfile.Z ./myfile.T


Thanks for your help
  #2 (permalink)  
Old 07-02-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,415
$# will give you the no of arguments passed to the script
so instead of while loop use for loop

Code:
list=
for i in $* ; do
list=$list" ./myfile."$i
done
echo "$list"

  #3 (permalink)  
Old 07-02-2009
jolecanard jolecanard is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 36
Thanks for your answer!!

I will try that right now. However, there is one more thing: in reality I want to skip the first argument. I know I can use a "if" condition so that if $i == 1, then I do nothing.
Is there a nicer way to force the "for" loop to start with i=2 ?
  #4 (permalink)  
Old 07-02-2009
mglenney mglenney is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 85

Code:
list=
shift
for i in $* ; do
list=$list" ./myfile."$i
done
echo "$list"

  #5 (permalink)  
Old 07-02-2009
jolecanard jolecanard is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 36
Perfect! Thanks!!
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0