Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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/
share|improve this question
    
Add a set -eux to the top of your script. This will lead to it stopping when the first command fails. –  orkoden Aug 22 at 10:56
2  
That’s not an FTP server if the URL starts with http/https. –  bfontaine Aug 23 at 10:24

5 Answers 5

up vote 3 down vote accepted

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/
share|improve this answer
    
Only thing is, you need to cd as ~/Downloads and also you can just get rid of sleep 30s. Good softkill workaround –  Steve Aug 21 at 1:06
    
Thanks for input. Why cd ~/downloads? I don't see the need, other than perhaps tightening it up. The indicated links automatically download into ~/Downloads, without any cd needed. Regards. –  njboot Aug 21 at 1:30
    
Well, they /don't/ download automatically to that folder. They automatically download to ~, at least on my macbook. –  Steve Aug 21 at 1:33
1  
This doesn't work well in all but the narrowest of cases. The files are downloaded to the current working directory. Your mv calls assume a fixed download location. Sudo should not be necessary. The commands that need elevated privs will prompt for them if they are required here. Also: you have no error checking. What if the curl calls fail? –  Ian C. Aug 21 at 1:52
    
@IanC. Thanks for input. I've modified based on Steve's suggestions as well. Immediately removed sudo from the script...quickly realized that was stupid. Adding conditional expressions in case of failure of curl calls would make a great deal of sense...too tired now though. Tomorrow! Thanks for your feedback. Cheers. –  njboot Aug 21 at 4:52

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.

share|improve this answer
    
He had a problem with mv that would fix the problem with curl when it was rectified. However good post! –  Steve Aug 20 at 23:53
    
@Steve I quoted his post in my answer. He said "For cURL it comes back saying command not found". –  Ian C. Aug 20 at 23:54
    
Fixed my comment, sorry I worded it wrongly –  Steve Aug 20 at 23:54
    
Fixing mv still wouldn't help if he's getting command not found trying to call curl from his script. –  Ian C. Aug 20 at 23:57

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.

share|improve this answer
    
That first curl call will download the file to the directory you happen to be in when the script is run, not to ~ as the mv command is expecting. Better to cd ~/Downloads at the start of the script so you're in a known location. –  Ian C. Aug 20 at 23:56
    
Yuuuuup thanks for catching that –  Steve Aug 21 at 0:05
    
Also: no error checking. –  Ian C. Aug 21 at 1:53

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/

share|improve this answer

I would first try putting quotes around your URLs for curl and the other commands. Inside the shell script, punctuation can cause issues.

Also change the first line to #!/bin/bash instead of sending it to sh.

share|improve this answer
    
/bin/sh is a full and proper bash shell. Has been that way for all the OS X released at least. See /bin/sh --version if you doubt this. No need to change the #! line. –  Ian C. Aug 21 at 1:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.