2

Trying to move selected dotfiles from ~ directory into new directory dotfiles and create symbink, with this code:

  1 #!/bin/bash.
  2 # This file moves selected files to newly created .dotfile folder and creates symbolic synblinks.
  3 
  4 # STEPS:.
  5 # 1. create and array of with .dotfiles elements.
  6 # 2. create a new directory .dotfiles if this directory does not exits.
  7 # 3. move selected .dotfiles from step 1 into directory created in step 2.
  8 # 4. create symbolic links for all .dotfiles from step 1.
  9 # 5. initialize a git repo.
 10 
 11 # 1. declare an array with .dotfiles element for versioning:.
 12 declare -a Dotfiles=('bash_profile' 'bashrc' 'gitconfig' 'gitignore' 'profile' 'vimrc' 'vimrc.after' )
 13 
 14 # inform the user and print the whole array on the screen:
 15 echo 'going to move the following selected .dotfiles:'
 16 echo ${Dotfiles[@]}
 17 
 18 # 2. create a variable for new directory for storing dotfiles..
 19 dir = ~/dotfiles
 20 cd ~ && mkdir -p $dir
 21 echo 'directory created'
 22 
 23 # move selected dotfiles to new directory .dotfiles
 24 
 25 for dotfile in "${Dotfiles[@]}";do
 26   echo "moving $dotfile into directory $dir"
 27   mv ~/.$dotfile $dir
 28   echo "creating symblik for $dotfile"
 29   ln -s $dir/$dotfile ~/.$dotfile
 30 done
 31 echo 'done'

When I run it, new directory is created, but original dotfiles are not moved and symblink are not running as well.

Questions are

  1. What is missing in this code?
  2. Do you have some suggestions for refactoring?

2 Answers 2

2

Your assignment to dir should be

dir=~/dotfiles

The spaces you have cause it to be interpreted as a call to the dir command with 2 arguments. The mv command subsequently tries to move the files to the root directory (since $dir/ expands to /).

0

Try:

25 for dotfile in "${Dotfiles[@]}";do
26   echo "moving $dotfile into directory $dir"
27   mv ~/.$dotfile $dir/
28   echo "creating symblik for $dotfile"
29   ln -s $dir/$dotfile ~/.$dotfile
30 done

though there are other ways to resolve. This better not be homework :)

0

You must log in to answer this question.