0

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
0

Most of it is shell, and that's incorrect because of missing blanks on the first if-statement. Also

  • the first if-statement lacks a balancing fi
  • the $@ should be quoted "$@"
  • the $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
  • But what does those lines do? Can you explain me please? – user81514 Aug 25 '16 at 20:29
  • with the errors, it does nothing – Thomas Dickey Aug 25 '16 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy