views:

5584

answers:

7
#!/bin/bash

hello()
{
    SRC=$1
    DEST=$2

    for IP in `cat /opt/ankit/configs/machine.configs` ; do
        echo $SRC | grep '*' > /dev/null
        if test `echo $?` -eq 0 ; then
            for STAR in $SRC ; do
                echo -en "$IP"
                echo -en "\n\t ARG1=$STAR ARG2=$2\n\n"
            done
        else
            echo -en "$IP"
            echo -en "\n\t ARG1=$SRC ARG2=$DEST\n\n"
        fi
    done
}

hello $1 $2

The above is the shell script which I provide source (SRC) & desitnation (DEST) path. It worked fine when I did not put in a SRC path with wild card ''. When I run this shell script and give ''.pdf or '*'as follows:

root@ankit1:~/as_prac# ./test.sh /home/dev/Examples/*.pdf /ankit_test/as

I get the following output:

192.168.1.6
ARG1=/home/dev/Examples/case_Contact.pdf ARG2=/home/dev/Examples/case_howard_county_library.pdf

The DEST is /ankit_test/as but DEST also get manupulated due to '*'. The expected answer is

ARG1=/home/dev/Examples/case_Contact.pdf ARG2=/ankit_test/as

So, if you understand what I am trying to do, please help me out to solve this BUG. I'll be grateful to you.

Thanks in advance!!!

I need to know exactly how I use '*.pdf' in my program one by one without disturbing DEST.

+1  A: 

The shell will expand wildcards unless you escape them, so for example if you have

$ ls
one.pdf two.pdf three.pdf

and run your script as

./test.sh *.pdf /ankit__test/as

it will be the same as

./test.sh one.pdf two.pdf three.pdf /ankit__test/as

which is not what you expect. Doing

./test.sh \*.pdf /ankit__test/as

should work.

agnul
you are right but I want to use all 'one.pdf, two.pdf, three.pdf' one by one. for example i need to copy this all to another location. Then wat is the Solution. Please help me with that.
Ankit S
+1  A: 

You are also missing a final "done" to close your outer for loop.

dogbane
yeh.... i got you. By mistake. But my code is working. The output i Provided is given out by program. Please ignore syntax porblem. Program is working. Please suggest me the logic or how to achieve my target.
Ankit S
A: 

There's no need to spawn a shell to look at the $? variable, you can evaluate it directly.

It should just be:

if [ $? -eq 0 ]; then
Alnitak
I do not have much exp. in shell scripting. So, don't know this is a bizzare way to use. I tried yours also but got following error.getting this error when using this./test.sh: line 14: [: missing `]'
Ankit S
+1  A: 

Your script needs more work. Even after escaping the wildcard, you won't get your expected answer. You will get:

ARG1=/home/dev/Examples/*.pdf ARG2=/ankit__test/as

Try the following instead:

for IP in `cat /opt/ankit/configs/machine.configs`
do
    for i in $SRC
    do
     echo -en "$IP"
        echo -en "\n\t ARG1=$i ARG2=$DEST\n\n"
    done
done

Run it like this:

root@ankit1:~/as_prac# ./test.sh "/home/dev/Examples/*.pdf" /ankit__test/as
dogbane
A: 

OK, this appears to do what you want:

#!/bin/bash

hello() {

  SRC=$1
  DEST=$2

   while read IP ; do
     for FILE in $SRC; do
       echo -e "$IP"
       echo -e "\tARG1=$FILE ARG2=$DEST\n"
      done
   done < /tmp/machine.configs
 }

 hello "$1" $2
  1. You still need to escape any wildcard characters when you invoke the script
  2. The double quotes are necessary when you invoke the hello function, otherwise the mere fact of evaluating $1 causes the wildcard to be expanded, but we don't want that to happen until $SRC is assigned in the function
Alnitak
p.s. this version only uses shell builtins, so doesn't need to spawn any child processes to do its work.
Alnitak
what was this down-voted for?
Alnitak
+1  A: 

If you can, change the order of the parameters passed to your shell script as follows:

./test.sh /ankit_test/as /home/dev/Examples/*.pdf

That would make your life a lot easier since the variable part moves to the end of the line. Then, the following script will do what you want:

#!/bin/bash
hello()
{
    SRC=$1
    DEST=$2

    for IP in `cat /opt/ankit/configs/machine.configs` ; do
        echo -en "$IP"
        echo -en "\n\t ARG1=$SRC ARG2=$DEST\n\n"
    done
}

arg2=$1
shift
while [[ "$1" != "" ]] ; do
        hello $1 $arg2
        shift
done
paxdiablo
A: 

Here's what I came up with:

#!/bin/bash

hello()
{
    # DEST will contain the last argument
    eval DEST=\$$#

    while [ $1 != $DEST ]; do
        SRC=$1

        for IP in `cat /opt/ankit/configs/machine.configs`; do
            echo -en "$IP"
            echo -en "\n\t ARG1=$SRC ARG2=$DEST\n\n"
        done

        shift || break
    done
}

hello $*

Instead of passing only two parameters to the hello() function, we'll pass in all the arguments that the script got.

Inside the hello() function, we first assign the final argument to the DEST var. Then we loop through all of the arguments, assigning each one to SRC, and run whatever commands we want using the SRC and DEST arguments. Note that you may want to put quotation marks around $SRC and $DEST in case they contain spaces. We stop looping when SRC is the same as DEST because that means we've hit the final argument (the destination).

godbyk