我编写了以下脚本,如果您在 VirtualBox 上运行 Ubuntu 并使用 NetBeans,您可能会发现它很有用。这些脚本甚至可以用于其他 VirtualBox 需求,因为它会尝试:
- 安装一个应用程序(NetBeans)。
- 测试 Ubuntu 是否正在运行客户添加程序。
- 尝试根据默认项目名称挂载共享文件夹,该名称通常在任何系统(主机或客户机)上保持不变。
- 将挂载写入
rc.local
以方便使用。 - 在用户箱中创建和/或附加文件以允许卸载文件夹。
除非您升级,否则脚本将完美运行。在这种情况下,将进行多次挂载,而卸载脚本将无法按预期运行,因为 中将有多个条目/etc/mtab
。
这不能通过使用umount
诸如这样的标志来解决-f -l -a -t
,因为这通常会导致所有挂载都被卸载。这umount -a -t vboxsf
也不/target
是所需的解决方案,因为可能不需要卸载所有共享文件夹。
我想到了一个解决方案,但我对 bash/脚本还不熟悉,不知道如何实现。我的解决方案是测试rc.local
潜在的重复行,以避免重复安装:
#!/bin/bash
#Author: Yucca Nel http://thejarbar.org
#Will restart system
PATH="/sbin:/usr/sbin:/bin:/usr/bin"
export PATH
#Modify these variables as needed...
tempWork=/tmp/work
startupScript=/etc/init.d/rc.local
defaultNetBeansVersion=7.0.1
echo "Provide NetBeans version (7.0.1 is default) then hit [Enter] :"
read NetBeansVersion
if [ -z "$NetBeansVersion" ]
then
$NetBeansVersion=$defaultNetBeansVersion
fi
mkdir -p /$tempWork;
cd /$tempWork;
wget http://dlc.sun.com.edgesuite.net/netbeans/7.0.1/final/bundles/netbeans-$NetBeansVersion-ml-javase-linux.sh;
sh $tempWork/*sh;
#Add Netbeans launcher to your PATH. Doing so allows you to run 'netbeans' command from the terminal
#This line will need to be changed if you changed the default install location (IOW Netbeans is not in ~/)
sudo ln -f -s ~/netbeans-$NetBeansVersion/bin/netbeans /usr/bin/;
#If you use VirtualBox , you can share your projects between Host and guest. Name of shared
#folder must match 'NetBeansProjects'
mkdir -p $HOME/NetBeansProjects
if [ -f /sbin/mount.vboxsf ]
then
sudo /sbin/umount /home/$HOME/NetBeansProjects
sudo /sbin/mount.vboxsf NetBeansProjects $HOME/NetBeansProjects
fi
if mountpoint -q ~/NetBeansProjects
then
#Add it to the universal start script to automate process...
sudo sed -ie '$d' $startupScript
echo "sudo /sbin/mount.vboxsf NetBeansProjects $HOME/NetBeansProjects"| sudo tee -a $startupScript
echo "exit 0"| sudo tee -a $startupScript
sudo chmod +x $startupScript
#Create a mount and unmount script file and add it to users local bin
rm -rf $tempWork/*
echo '#!/bin/bash' > $tempWork/netbeans-mount.sh
echo '#!/bin/bash' > $tempWork/netbeans-umount.sh
echo '#!/bin/bash' > $tempWork/mount-from-host.sh
echo '#!/bin/bash' > $tempWork/unmount-from-host.sh
echo "sudo /sbin/mount.vboxsf NetBeansProjects $HOME/NetBeansProjects" >> $tempWork/netbeans-mount.sh
echo "sudo /sbin/mount.vboxsf NetBeansProjects $HOME/NetBeansProjects" >> $tempWork/mount-from-host.sh
echo "sudo umount $HOME/NetBeansProjects" >> $tempWork/netbeans-umount.sh
echo "sudo umount $HOME/NetBeansProjects" >> $tempWork/unmount-from-host.sh
echo "exit 0" >> $tempWork/unmount-from-host.sh
echo "exit 0" >> $tempWork/mount-from-host.sh
echo "exit 0" >> $tempWork/netbeans-mount.sh
echo "exit 0" >> $tempWork/netbeans-umount.sh
sudo chmod +x $tempWork/*
sudo mv -f $tempWork/*.sh /usr/local/bin
rm -rf $tempWork
fi
#This function is used to cleanly exit with an error code.
function error_exit {
sleep 7
exit 1
}
#restart
sudo reboot
exit 0
有什么提示吗?我的目标是为 Java 开发人员编写一个超级脚本,将最需要的工具下载到任何 Linux(不仅仅是 Ubuntu)上,并安装潜在的东西,如果您有现有的开发主机,则无需重新安装。如果 Maven、Tomcat、SVN、JBoss 等东西已经在主机系统上,则不需要特殊的客户机安装,将不同的系统合并为一个系统还有更多好处;例如,Windows 可以运行 Photoshop 和 Safari 浏览器,但 Linux 提供了更好的自定义功能和开箱即用的 ssh 等工具。
答案1
我不确定我是否理解......但在这里我计算了 grep 输出中唯一行的数量。
grep "sudo /sbin/mount.vboxsf" /etc/rc.local | sort | uniq -c | wc -l
两个来自 echo 行,两个来自命令行。它应该始终等于四个,对吗?