confused with cp

may i know what

cp $1 $2
    $0 $2

does?

Did you come up with this ?

cp $1 $2
    $0 $2

If you are trying to provide multiple arguments for cp, this is what man cp has to say..

DESCRIPTION
       Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Keeping your command in tune with the man cp, we probably might have the following:

cp $1 $0 $2

which is self explanatory as per the man page.

Hopefully , this is what you were looking for.

Vino

well, i read in a book,

if [ $# -eq 2 ]; then
	cp $1 $2
	$0 $2
else
	tmp=`cat $1 | wc -l`
	echo $tmp
	while [ $tmp -ne 0 ] ; do
		echo `head -$tmp $1 | tail +$tmp` >> $1.tmp
		tmp=$(($tmp -1))
	done
	mv $1.tmp $1
fi

but i cant figure out how this

cp $1 $2
	$0 $2

could affect the script

That looks better.

if [ $# -eq 2 ]; then
	cp $1 $2
	$0 $2
else

Here's my analysis.

If you have 2 command line arguments provided by the user, then make argument 2 the same as argument 1.

And after you are done with that,

execute the script with $2 as an argument. In the shell scripting world, $0 holds the name of the script. So basically, you call the script with the value held in $2.

Let me know if you dont understand.

Vino

hmm so it is a recursivity script :slight_smile:
thanks

Not a recursive one. I made a mistake in my explanation. :frowning:

The point is..

If you have two arguments, then make arg1 and arg2 the same.

Else, find the numbers of lines in $1 and carry on ..

Vino

but my question is what does $0 $2 below the cp command do?

If your script name is test.sh and say $2 turns out to be trust,

then $0 $2 is equivalent to

test.sh trust

And since you dont have more than one argument, it will not be recursive.

Vino

When a script can recurse at most one time, do you still call it recursive? I'm not sure either. We use UUOC to mean "unnecessary use of cat". Maybe we need UUOR to go with it? How about this alternative..,

if [ $# -eq 2 ] ; then
	cp $1 $2
	shift
fi
tmp=`wc -l < $1`
echo $tmp
while [ $tmp -ne 0 ] ; do
	echo `head -$tmp $1 | tail +$tmp` >> $1.tmp
	tmp=$(($tmp -1))
done
mv $1.tmp $1
fi

well im not talking about recursivity!
i wish to know how does this work?

vino has answered your question, correctly as far as I can see. He mentioned that the script is using recursion. And since you used the present tense, wish, it is apparently the recursion that is still confusing you.

C|[anti-trust],

In

cp $1 $2
$0 $2

,

$0 $2

is not part of the cp command

cp $1 $2

This code will give the same results with an echo'ed string.

cp $1 $2
echo " cp $1 to $2"
$0 $2

Vino

if [ $# -eq 2 ]; then
	cp $1 $2
	$0 $2
else
	tmp=`cat $1 | wc -l`
	echo $tmp
	while [ $tmp -ne 0 ] ; do
		echo `head -$tmp $1 | tail +$tmp` >> $1.tmp
		tmp=$(($tmp -1))
	done
	mv $1.tmp $1
fi

Concur with everyone's comment on this part of the code.
Here is mine.

The script is always taking $1 through out and ignoring $2.
I feel 'if' construct is not at all needed there. The code in 'else'
part simply works.

Only thing 'if' part doing is ,
if the script is provided with 2 arguments as files,
override the 2nd file contents with first file.
the second line $0 $2 is going recursive second and last time
leading that to else part.

"Complex way of doing things"