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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk arguments karthikn7974 Shell Programming and Scripting 6 05-06-2008 09:34 PM
if statement - how can I do 2 arguments? Darklight Shell Programming and Scripting 6 04-16-2008 06:28 AM
Arguments iago UNIX for Dummies Questions & Answers 1 08-24-2007 07:50 AM
several arguments together homefp Shell Programming and Scripting 8 07-04-2002 12:44 AM
passing arguments jpprial UNIX for Dummies Questions & Answers 4 04-03-2001 08:13 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-17-2008
Registered User
 

Join Date: Apr 2008
Posts: 23
puting arguments into varables

hello all
I am written a script that would put a line of text and a file into two different varables for later use, i.e a script called pimp with any number of words and a filename which is the last argument.

so for eg $pimp my name is the name given to me when i was born Afile
where pimp is the script name
"my name ...... born" is the line of text
and Afile is a name of the file.


I can get the filename since is the last argument but how can i get the remaining argument


this is what i have done.
for arg in "$@"
do
: # nothing
done
echo "the last command line argument is $arg"
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-17-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,501
The conventional solution would be to put any required arguments first, and leave variable arguments last.

Code:
#!/bin/sh
file=$1
shift
echo "file is $file, remaining arguments are" "$@"
The index of the last command line argument is in $#, and you can use eval to print it.

Code:
eval echo Last argument is ${$#}
eval is tricky to learn, and you need to understand proper quoting rules etc. to use it.
Reply With Quote
  #3 (permalink)  
Old 05-17-2008
Registered User
 

Join Date: Apr 2008
Posts: 23
i quiet understand but the $1 would not show the file name it would be part of the word.
i want to capture the last argument and at the same time able to use the rest of the arguments.
so
pimp my name is joe afile
where pimp is the name of the script
my name is joe = a text
afile = name of a file that exist in my home directory
so jow do i capture the afile and d same time able to use my name is joe later
Reply With Quote
  #4 (permalink)  
Old 05-17-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,487
An example how to shift the parameters:

Code:
#!/bin/sh

n=$(( $# - 1 ))

for i in `seq 1 $n`
do
  s=$s" $i"
  shift
done

echo Line: "$s"
echo Name: "$1"
And one with sed:

Code:
#!/bin/sh

line=$(echo "$*"|sed 's/\(.*\) .*/\1/')
name=$(echo "$*"|sed 's/.* \(.*\)/\1/')

echo Line " "$line"
echo Name " "$name"
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:02 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0