0

Currently, I have the following:

#!/bin/sh

pathlink() {
  for file in "$@";
  do
    if [ -d "$file" ];
    then
#      echo "$file"
      pathlink "$file/*"
    else
      echo '/home/buddhilw/dotfiles/'$(basename $file)
#       ln -nfs /home/buddhilw/dotfiles/$(basename $directory) $directory
    fi
  done
}

pathlink \
  /home/buddhilw/.config/* \
  /home/buddhilw/.local/* \
  /home/buddhilw/.bashrc

I get the following error, for every file in upper-directories,

basename: extra operand ‘/home/buddhilw/.local/quicklisp/dists’
BuddhiLW
  • 608
  • 3
  • 9
  • Btw.: `sh` ([Bourne-shell](https://en.wikipedia.org/wiki/Bourne_shell)) is usally not `bash` ([Bourne-again shell](https://en.wikipedia.org/wiki/Bash_(Unix_shell))). – Cyrus Dec 13 '21 at 05:34
  • change `pathlink "$file/*"` to `pathlink $file/*` – Yuri Ginsburg Dec 13 '21 at 06:02
  • @YuriGinsburg: This would get you in trouble, if `file` contains spaces. I would do a `pathlink "$file"/*`. – user1934428 Dec 13 '21 at 10:49
  • @BuddhiLW : I changed your tag _bash_ into _shell_, because there is nothing in your question related to bash. May I suggest that you also update the question's title accordingly? – user1934428 Dec 13 '21 at 10:50

1 Answers1

0

change basename $file to basename "${file}". It likely just ran into a filename with a space in it.

i would suspect you'd have an easier time leaving this kinda stuff up to the find utility.

find /some/path -printf '%d %y %Y %f\n'

provides all the info you need as far as i can see. the first column indicating the depth, the 2nd and 3rd indicating the filetype (like d for directories; one follows symlinks one does not), and the 4th column being the file's basename.

you can put that command in a bash script, change the \n to \0, pipe it into a while loop with IFS=" " read -r -d$'' DEPTH TYPEA TYPEB FILENAME and you have a very well rounded filescanner.

Raxi
  • 2,452
  • 1
  • 6
  • 10
  • 1
    The braces don't add any value here; `basename "$file"` is quite sufficient and idiomatic. – tripleee Dec 13 '21 at 05:33
  • never a downside really, i always brace them all :) – Raxi Dec 13 '21 at 05:48
  • It is not sufficient to quote the argument of `basename`. It is necessary to quote the sub-shell expression as well: `"$(basename "$file")"`. – ceving Dec 13 '21 at 08:11