【问题标题】:bash: automate mounting folders to home from other drivebash:自动将文件夹从其他驱动器安装到主目录
【发布时间】:2016-12-21 20:36:14
【问题描述】:

我想只用一个脚本从外部驱动器自动挂载我的主文件夹。我的系统是 Ubuntu 16.04。

我想检查外部目录中是否存在与HOME中的文件夹同名的文件夹,然后将HOME中的文件夹重命名为foldername_local并将外部目录文件夹挂载到HOME中。

这是我在 google 和 stackoverflow 的帮助下取得的进展:

REMOTE=/path/to/remote/location

#create a list of external folders    
rLIST=$(find $REMOTE -maxdepth 1 -type d -name [^\.]\* -printf '%f\n'| sed 's:^\./::')

#create a list of folders in HOME
hLIST=$(find $HOME -maxdepth 1 -type d -name [^\.]\* -printf '%f\n'| sed 's:^\./::')

#declare comparison function
contains() {
[[ $1 =~ (^|[[:space:]])$2($|[[:space:]]) ]] && exit(0) || exit(1)
} 

for item  in "$rLIST"; do 

 if contains hLIST item 
  then 
 #rename folder and mount external drive fodlers
 mv $HOME$item $HOME$item_local
 mount --bind $REMOTE$item $HOME$item
  else
 continue
fi

done

到目前为止,创建文件夹名称列表是可行的。

不幸的是,比较部分不起作用,我被卡住了。

我很欣赏有关如何解决此问题的任何想法。

来源:
How do I check if a variable exists in a list in BASH
Listing only directories in UNIX

【问题讨论】:

    标签: bash compare ubuntu-16.04 home-directory


    【解决方案1】:
    for item in /path/to/remote/location/*; do 
         dir=${item##*/}
         [ -d $item ] && [ -d /home/$dir ] && mv /home/$dir /home/${dir}_local && mkdir /home/$dir && mount --bind $item /home/$dir
    done
    

    【讨论】:

    • 感谢 [参数扩展] (wiki.bash-hackers.org/syntax/pe#substring_removal) 的提示
    • 不幸的是,我只得到第 10 行:[: missing `]' 并且它不起作用。第 1 行是 [ -d $item ] - 行。
    • 抱歉,我还不能编辑自己的评论。错误是指第 10 行: [ -d $item ] && ....
    • 我得到了一个小错误 :-) 它的 [ -d /home/USER/$dir ] 感谢@Ipor Sircer 和检查这两个文件夹的好主意。您也可以为 /home/USER/$dir 编写 $HOME/$dir
    猜你喜欢
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多