Can anybody explain me what does this script in each line? I can not run this because I think the syntax is incorrect.
#! /bin/bash v=`echo $@|gawk '{print $NF}'` if[-d $v];then for v2 in $@;do if test $2 != $v;then ln $v2 $v/$v2 rm $v2 fi done
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityMost of it is shell, and that's incorrect because of missing blanks on the first if-statement. Also
fi
$@
should be quoted "$@"
$2
probably should be $v2
The -d
test anyway is suspect, since gawk should be printing a number (not a directory name). Still, you could be testing for a directory named for a number...
As suggested in a comment, if you made those corrections, the script would try to remove everything that is not the same as the last parameter of the script, and make link to those removed files under the directory named by the final parameter. (Just using mv
would be more straightforward).
Here's a suggested fixed script:
#! /bin/bash
v=`echo "$@"|gawk '{print $NF}'`
if [ -d $v ];then
for v2 in "$@";do
if test $v2 != $v;then
ln $v2 $v/$v2
rm $v2
fi
done
fi
${!#}
as described here How Do I Find the Last Positional Parameter in Linux i.e.if [ -d "${!#}" ]; then ...
mv "$@"