Skip navigation

Bash script running on my MBP 10.7.4 but not on other Macs (all 10.7.4)

1123 Views 15 Replies Latest reply: Jun 9, 2012 7:03 PM by wideEyedPupil RSS
1 2 Previous Next
wideEyedPupil Level 2 Level 2 (360 points)
Currently Being Moderated
Jun 7, 2012 9:39 PM

A friend of mine wrote me this short script to run in terminal that downloads a particular file every n seconds from a password protectected FTP site. The script worked on his linux machine and on my Mac but not on my collegues Macs. I'm wondering if anybody knows reasons why it may be failing on other Macs.

 

We changing the first line from: #!/bin/sh to #!/bin/bash

and exceuting with

user_prompt_$ bash ./download_stats.sh

 

Another idea is to script cyberduck but it doesn't support applescript though may have a comand line interface (looking into that at present).

 

There error output from one of the accounts it fails on follows the script listing.

 

#!/bin/sh

#

# =================================================================

# Download XML_stats Bash Script

#

# Filename:     download_stats.sh

# Arguments:    input_filename  - file to be downloaded from remote server. Optional.

#               poll_interval   - seconds delay between downloads. Optional (input_filename must be present)

# Use:          Invoke from shell, e.g. ./download_stats.sh

# Author:       Luke Neeson

# Date Created: 2012-05-31

# Comments:     This script will download a given file, input_filename, at the given interval, poll_interval.

# Notes:

# - .netrc is required in the current directory and must contain the logon details for the fox server. This could be installed in the user home directory, if so, delete the line marked below.

# =================================================================

 

# If the .netrc is in the directory of this script, the following must be enabled. If the user's home .netrc is to be used, comment out the following line:

export HOME="`pwd`"

 

export input_filename="the_file_I_want_as_default.xml"

export poll_interval=10

 

if [ ! -z "$1" ] ; then

  export input_filename="$1"

  if [ ! -z "$2" ] ; then

    export poll_interval="$2"

  fi

fi

 

while [ true ]

do

  ftp access.foxsports.com.au << EOF 2>&1 | sed 's/.*AUTH GSSAPI.*//g' | sed 's/.*KERBEROS.*//g'

  get  "$input_filename"

  quit

EOF

  sleep $poll_interval

done

 

 

ERRORS RETURNED:

a-mbp:~ macca$

a-mbp:~ macca$ cd ~/stats/download_rugby_stats/

a-mbp:download_stats a$ sh download_rugby_stats.sh

: command not founds.sh: line 16:

: command not founds.sh: line 19:

: command not founds.sh: line 22:

download_stats.sh: line 39: syntax error: unexpected end of file

a-mbp:download_stats a$

 

Those are the blank lines and 39 is the last line of script.

MacBook Pro, Mac OS X (10.7.3), 10.7.4 actually Wacom 6x11
  • Mark Jalbert Level 5 Level 5 (4,385 points)

    If I have your script formatted correctly, there are 37 lines in the script. If there are any blank lines at the end of the script then remove them.

  • X423424X Level 6 Level 6 (14,190 points)

    A blank line at the end of the script shouldn't matter nor should the other blank lines causing errors either.  So I am assuming those blanks aren't really blanks.  What editor did you use to create this file?  Also when I cut/pasted the script into BBedit there does appear to be a blank at the beginning of those blank lines.  I assume that the original character got lost in the posting and extracting from the post into my editor window.

     

    So try to remove the character at the start of each of those "blank" lines.

     

    You might also want to use TextWrangler as your text editor (BBEdit's little brother).

  • Mark Jalbert Level 5 Level 5 (4,385 points)

    A blank line at the end of the script shouldn't matter nor should the other blank lines causing errors either.

    Well, they should not matter. The idea was to have the OP look at the file in a text editor then operate on the file, saving it with unix line endings (hopefully).

     

    What editor did you use to create this file?

    I use vi (vim) and the file is double spaced when you copy it from the forum. I removed the double spacing. The "syntax error: unexpected end of file" error looks like it is coming from the while loop. There are parts of his script that aren't well formed such as:

     

    if [ ! -z "$1" ] ; then
      export input_filename="$1"
      if [ ! -z "$2" ] ; then
        export poll_interval="$2"
      fi
    fi
    
  • BobHarris Level 6 Level 6 (12,505 points)

    I did a copy and paste into a text editor window.  And when I ran the text file it did not get an error, and ftp prompted me for a password.

     

    As others have suggested, I think you have a problem with the way the file is formatted.

     

    I would suggest a) download TextWrangler (free) which is a very good text editor:

    <http://www.macupdate.com/info.php/id/11009/textwrangler>

     

    Copy and paste the script into TextWranger, then save the file.

     

    Now try:

     

    bash saved.file

     

    Or change its permissions "chmod +x saved.file" and run it directly.

  • BobHarris Level 6 Level 6 (12,505 points)

    If the file is good, then check the user's PATH and make sure all the commands used in the script can be found in the PATH

     

    which ftp

    which pwd

    which true

    which sed

    which sleep

     

    You can check to see that line endings are good using

     

    cat -vte download_rugby_stats.sh

     

    You might also want to check if the user has a $HOME/.netrc (or that you do) and if that is affecting the behavior of ftp (long shot and very unlikely).

     

    Finally, make sure EOF is in column 1.  No leading spaces or tabs in front of it.

  • BobHarris Level 6 Level 6 (12,505 points)

    How do I edit the OP, I want to edit a line I shouldn't have left as per original, got all the rest.

    You get about 15 minutes to edit a post, and then it is lock and the only way to make changes to it after that is through a moderator, and only tend to edit offensive material (if they don't just delete the post altogether ).

  • BobHarris Level 6 Level 6 (12,505 points)

    cat -vte looks OK.  The $ at the end of each line indicates <LF> terminated.  If this had been <CR> terminated, it would have ended with ^M, and if it was DOS <CR><LF> terminated it would have been ^M$

     

    I ONLY see $ indicating proper Unix line terminations.  This is good.

     

    You can easily look at your PATH using:

     

    (IFS=:  ; for j in $PATH;do echo $j;done)
    /usr/bin
    /bin
    /usr/sbin
    /sbin
    /usr/local/bin
    /usr/X11/bin
    

     

    The above PATH is a typical vanilla PATH.  If the user has modified their PATH, then it can have a lot more in it.


    The 'which' commands should tell you what versions of the programs were used.  'which' on my system found:

    /usr/bin/ftp
    /bin/pwd
    /usr/bin/true
    /usr/bin/sed
    /bin/sleep
    

     

    And again (long shot) check for a $HOME/.netrc on either their or your system that might change the ftp behavior

    MacBook Pro, Mac OS X (10.7.4), 27" iMac, MacBook, MacMini, etc...
  • BobHarris Level 6 Level 6 (12,505 points)

    I'm not sure how these conditionals get parsed but what exactly is poor form about them? Best-practice would be…

    For what its worth, they look fine to me.  I personally use slightly different style, but not so much different from what is being shown.  And I have been writing shell scripts since '85 (lots of shell scripts).

  • BobHarris Level 6 Level 6 (12,505 points)

    /opt/local/bin and /opt/local/sbin are not normally in a PATH, which means this user has a custom PATH.

     

    I again suggest you use 'which' to find out what commands are being run

     

    which ftp
    which pwd
    which true
    which sed
    which sleep
    

     

    Here is what I get, and would be standard for Mac OS X

     

    /usr/bin/ftp
    /bin/pwd
    /usr/bin/true
    /usr/bin/sed
    /bin/sleep

     

    Having additional directories in PATH is not bad, but when having scripting problems, it is something to look at.

    MacBook Pro, Mac OS X (10.7.4), 27" iMac, MacBook, MacMini, etc...
1 2 Previous Next

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.