LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 02-26-2011, 12:14 PM   #1
akhand jyoti
LQ Newbie
 
Registered: Feb 2011
Posts: 12

Rep: Reputation: 1
how to receive files sent as a parameter


as $1 $2 represent first and second argument..
for example-
in a shell script...
./commfile file1 file2 file3

now in commfile i want to receive files by running a loop...like

for (i=1;i<$#;i++)
do
cat ${i} //help me here
echo do u want to proceed
read req
if($req=y||$req=Y) //it also not works
then
continue
else
exit
fi
done
 
Old 02-26-2011, 12:31 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Edit: Nevermind, Eric wins below.

Last edited by corp769; 02-26-2011 at 12:37 PM.
 
Old 02-26-2011, 12:35 PM   #3
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hello and Welcome to LinuxQuestions,

If you want to go through parameters you added on a commandline without knowing how many there will be you can use $@
Quote:
@
Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" .... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).
Also you'll need to read up a bit on your Bash, for example Bash Beginners Guide and Advanced Bash Scripting Guide are good reading. From them you'll learn when to use = or == and also about quotation "". Taking those things into account your script should look a bit like this:
Code:
for i in $@
do
   cat $i 
   echo "Do you want to proceed"
   read req
   if [[ $req == "y"||$req == "Y" ]]; then
      continue
   else
      exit
   fi
done
Next time try putting your code in CODE tags and indent for readability. It'll help you a lot.

Kind regards,

Eric
 
1 members found this post helpful.
Old 02-26-2011, 12:48 PM   #4
akhand jyoti
LQ Newbie
 
Registered: Feb 2011
Posts: 12

Original Poster
Rep: Reputation: 1
thanx!
 
Old 02-26-2011, 12:49 PM   #5
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hi,

You're welcome. If you consider your question/problem solved then please mark it as such using the Thread Tools.

Kind regards,

Eric
 
Old 02-26-2011, 01:23 PM   #6
akhand jyoti
LQ Newbie
 
Registered: Feb 2011
Posts: 12

Original Poster
Rep: Reputation: 1
@ericTRA
sorry for ur inconvenience...but can u plz tell me why it is not working.....

for (( i=1; i<$#;i++))
do
cat ${i} // or cat ${$i}
//or cat $"$i"
.
.
.
done
 
Old 02-26-2011, 01:25 PM   #7
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hello,

No inconvenience at all here. To what are you referring when you say it's not working? To the code example I provided (that does work)? Or do you want explanation on how it is yours is not working?

Kind regards,

Eric
 
Old 02-26-2011, 01:29 PM   #8
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Quote:
Originally Posted by akhand jyoti View Post
@ericTRA
sorry for ur inconvenience...but can u plz tell me why it is not working.....

for (( i=1; i<$#;i++))
do
cat ${i} // or cat ${$i}
//or cat $"$i"
.
.
.
done
Eric clearly stated to use "for i in $@" in order to parse all arguments passed to your script. This is what you need to use.

Cheers,

Josh

Edit - I am just helping you because the way you are doing it CAN work, but it is more complicated; Why do something more complicated and ugly, when you can use the basics to achieve the same task? Also, please spell out your words, instead of using "plz" and "u." Thank you for understanding.

Last edited by corp769; 02-26-2011 at 01:34 PM.
 
Old 02-26-2011, 01:41 PM   #9
akhand jyoti
LQ Newbie
 
Registered: Feb 2011
Posts: 12

Original Poster
Rep: Reputation: 1
yes.. that is working..but i want to know
how to do that in this way..the way i am doing....
 
Old 02-26-2011, 01:55 PM   #10
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hello,

For a complete understanding you should start by reading the links I pointed to in order to learn Bash from the beginning. For example you are not using = correctly (look at the difference between your code and mine). Apart from that you're not quoting y or Y (look at the Bash Guide for Beginners on quotation and what it's used for.

Kind regards,

Eric
 
  


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
tar with parameter -T (or --files-from) does not compress? Babelduo Linux - Server 3 01-18-2011 09:23 AM
What parameter do I add to RSYNC to update modified or new files to the destination? tankman1989 Linux - Software 2 02-20-2010 07:20 PM
Can't receive files via bluetooth CrownAmbassador Linux - Hardware 0 04-27-2008 01:43 PM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 05:54 AM
Mgetty /AutoPPP/ line does not work wright! PPPD doesn't receive the wright parameter diogo_cc Linux - Networking 0 05-04-2004 02:33 PM


All times are GMT -5. The time now is 01:10 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration