LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to receive files sent as a parameter (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-receive-files-sent-as-a-parameter-865168/)

akhand jyoti 02-26-2011 11:14 AM

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

corp769 02-26-2011 11:31 AM

Edit: Nevermind, Eric wins below.

EricTRA 02-26-2011 11:35 AM

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

akhand jyoti 02-26-2011 11:48 AM

thanx!

EricTRA 02-26-2011 11:49 AM

Hi,

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

Kind regards,

Eric

akhand jyoti 02-26-2011 12:23 PM

@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

EricTRA 02-26-2011 12:25 PM

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

corp769 02-26-2011 12:29 PM

Quote:

Originally Posted by akhand jyoti (Post 4272059)
@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.

akhand jyoti 02-26-2011 12:41 PM

yes.. that is working..but i want to know
how to do that in this way..the way i am doing....

EricTRA 02-26-2011 12:55 PM

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


All times are GMT -5. The time now is 07:11 AM.