對我的腳本的幫助或建議

對我的腳本的幫助或建議

我編寫了以下腳本,如果您在 VirtualBox 上執行 Ubuntu 並使用 NetBeans,您可能會發現該腳本很有用。這些腳本甚至可以用於滿足其他 VirtualBox 需求,因為它試圖:

  1. 安裝應用程式 (NetBeans)。
  2. 測試 Ubuntu 是否正在執行來賓新增。
  3. 嘗試根據預設項目名稱掛載共用資料夾,該名稱通常在任何系統(主機或來賓)上都保持不變。
  4. 將掛載寫入rc.local以方便使用。
  5. 在使用者 bin 中建立和/或附加檔案以允許卸載資料夾。

除非您升級,否則腳本可以完美運行。在這種情況下,將完成多次安裝,並且卸載腳本將無法按預期運行,因為/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

兩個來自回顯線,兩個來自命令列。它應該總是等於四,是嗎?

相關內容