I am new to writing shell scripts for Unix and need a little help. I am not sure why cURL now all of the sudden after running the script is downloading corrupts packages which cannot be mounted and why running the commands separately in Terminal work, but during the script everything fails such as

For cURL it comes back saying command not found For the mv command it says cannot find file or directory and of course mounting and everything thereafter fail.

Could you please take a look at the shell script to see what is missing or I am doing wrong?

#!/bin/sh
#Machines must have cURL installed as a prerequisite to download software from internet or FTP server 

# grab files from www.teamviewer.com or custom FTP server
curl -O http://download.teamviewer.com/download/TeamViewerHost.dmg

# grab TeamViewer from custom FTP server
curl -O https://www.dropbox.com/s/***********/com.TeamViewer8.Settings.plist

# Wait for download
sleep 30s

# Place customized plist file into Library/Preferences
mv ~/Downloads/com.TeamViewer8.Settings.plist /Library/Preferences/

# Mount and Install TeamViewer
hdiutil mount ~/Downloads/TeamViewerHost.dmg
installer -pkg /Volumes/TeamViewerHost/Install\ TeamViewerHost.pkg -target /

# Unmount package
hdiutil unmount /Volumes/TeamViewerHost/

Solution 1:

This should work. I don't have time currently to explain the changes/additions, but I'll update. You must run this script as sudo for it to work.

Even easier, sudo chmod +s yourscript - will run as root without requiring password.

#!/bin/bash

#Machines must have cURL installed as a prerequisite to download software from internet or FTP server 

#Change to your working directory. 
cd ~/Downloads

#grab files from www.teamviewer.com or custom FTP server
curl -O http://downloadus3.teamviewer.com/download/TeamViewerHost.dmg

#grab TeamViewer from custom FTP server
curl -O https://www.dropbox.com/s/******/com.apple.TeamViewer8.Settings.plist

#Place customized plist file into Library/Preferences
mv ~/Downloads/com.apple.TeamViewer8.Settings.plist /Library/Preferences/

#Mount and Install TeamViewer
hdiutil mount ~/Downloads/TeamViewerHost.dmg
installer -pkg /Volumes/TeamViewerHost/Install\ TeamViewerHost.pkg -target LocalSystem

#Softkill process in order to unmount package
ps aux | grep -i TeamViewer | awk {'print $2'} | xargs kill

#Unmount package
hdiutil unmount /Volumes/TeamViewerHost/

Solution 2:

For cURL it comes back saying command not found

You need to solve this first. cURL ships default with OS X 10.9 and should be in your PATH assuming you haven't monkey with your PATH environment variable. Try calling it directly with:

/usr/bin/curl

in the script instead. Also:

which curl

in a Terminal will show you the full path to the curl command in your current environment.

Regarding:

curl -O http://download.teamviewer.com/download/TeamViewerHost.dmg

The -O option tells curl to save the output to a file in the current working directory. That means if you're not in ~/Downloads when you run that command, the file isn't saved to ~/Downloads. You should add a:

cd ~/Downloads

line before calling the curl commands or use -o <filename-with-a-path> to save the file to a specific location with a specific name.

You should also wrap the URLs in quotes (") -- this stops shell special characters in the URLs like & from being interpreted by the shell and causing the URL sent to curl to be incorrect.

This:

sleep 30s

in completely unnecessary. The curl calls won't return until they've completely downloaded the files. Note: they'll return if they fail to download the files as well. You may want to consider checking the ext codes of the curl calls or for the existence of the files after calling curl to make sure they ran correctly. Or consider adding set -e to the top of the script so that the script exits if any command returns a non-zero exit code.

Finally:

For the mv command it says cannot find file or directory and of course mounting and everything thereafter fail.

Your mv call is failing because your curl calls are failing and aren't downloading the files as expected. Fix the curl calls and you'll fix the mv call.

Solution 3:

This should work:

#!/bin/sh
#Machines must have cURL installed as a prerequisite to download software from internet or FTP server 

# grab files from www.teamviewer.com or custom FTP server
cd ~
curl -O http://downloadus3.teamviewer.com/download/TeamViewerHost.dmg

# grab TeamViewer from custom FTP server
curl -O https://www.dropbox.com/s/***********/com.TeamViewer8.Settings.plist

# Place customized plist file into Library/Preferences
mv ~/com.TeamViewer8.Settings.plist /Library/Preferences/

# Mount and Install TeamViewer
hdiutil mount ~/TeamViewerHost.dmg
installer -pkg /Volumes/TeamViewerHost/Install\ TeamViewerHost.pkg -target /

# Unmount package
hdiutil unmount /Volumes/TeamViewerHost/

Your link to download TeamviewerHost returned

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="http://downloadus3.teamviewer.com/download/TeamViewerHost.dmg">here</a>.</h2>
</body></html>

when I read the file, so I changed the link.

Solution 4:

Thank you for all the help guys. Taking everything into account from the awesome suggestions thanks to Steve, Ian, orkoden and njboot here is the result of the script which works. I wish I knew how to apply the error checking that Ian had suggested so I added the set -eux.

#!/bin/sh
#sudo chmod +s "scriptname" - will run as root without requiring password
#Machines must have cURL installed as a prerequisite to download software from internet or FTP server 

#Stop script when first command fails
set -eux

#Change to your working directory. 
cd ~/Downloads

#grab TeamViewer version 8.dmg package and plist files from custom FTP server or TeamViewer website
curl -O "https://www.dropbox.com/s/*************/com.TeamViewer8.Settings.plist" #file located in location ~/Library/Preferences/
curl -O "https://www.dropbox.com/s/*************/com.teamviewer.teamviewer.plist" #file located in location ~/Library/LaunchAgents/
curl -O "https://www.dropbox.com/s/*************/com.teamviewer.teamviewer_desktop.plist" #file located in location ~/Library/LaunchAgents/

#version 8 download first for installation prior to version 9
curl -O "http://download.teamviewer.com/download/version_8x/TeamViewerHost.dmg"

#Place customized plist file into Library/Preferences and Library/LaunchAgents/
mv ~/Downloads/com.TeamViewer8.Settings.plist ~/Library/Preferences/
mv ~/Downloads/com.teamviewer.teamviewer.plist ~/Library/LaunchAgents/
mv ~/Downloads/com.teamviewer.teamviewer_desktop.plist ~/Library/LaunchAgents/

#Mount and Install TeamViewer version 8
hdiutil mount ~/Downloads/TeamViewerHost.dmg
installer -pkg /Volumes/TeamViewerHost/Install\ TeamViewerHost.pkg -target LocalSystem

#Softkill process in order to unmount package
ps aux | grep -i TeamViewer | awk {'print $2'} | xargs kill

#Unmount package
hdiutil unmount /Volumes/TeamViewerHost/

#version 9 upgrade 
cd ~/Downloads
curl -O "http://downloadus3.teamviewer.com/download/TeamViewerHost.dmg"

#Mount and Install TeamViewer version 9
hdiutil mount ~/Downloads/TeamViewerHost.dmg
installer -pkg /Volumes/TeamViewerHost/Install\ TeamViewerHost.pkg -target LocalSystem

#Softkill process in order to unmount package
ps aux | grep -i TeamViewer | awk {'print $2'} | xargs kill

#Unmount package
hdiutil unmount /Volumes/TeamViewerHost/

The purpose of the script is to find a way to deploy TeamViewer 9 through ssh and terminal silently without having to install 3rd party software as suggested in the following links:

http://jdmsysadmin.wordpress.com/2013/09/27/configuring-teamviewer-host-to-be-deployable-via-managed-software-update-munki-on-mac-os-x/

http://jdmsysadmin.wordpress.com/2014/04/16/configuring-teamviewer-host-9-to-be-deployable-with-munki/