How to run a basic -bash?- script?

Discussion in 'Mac Programming' started by BrianFD3S, Jul 15, 2008.

  1. BrianFD3S macrumors member

    Joined:
    Feb 19, 2008
    Location:
    Ireland
    #1
    Hi all.

    I was looking around the web for a method to download all the xkcd webcomic strips at once. xkcd is available under Creative Commons, so there's no legality issue, I just can't seem to figure it out. I've found what apparently is a script on DZone - link - but I have no idea how to run it. I saved it as a plain text file in TextEdit, then changed the extension to .sh and tried to get it to execute in Terminal, but no joy. Terminal states: "Cannot execute binary file".

    The code provided in the link is this:
    Code:
    #!/bin/bash
    
    for i in `seq 1 449`
    do
    	wget http://xkcd.com/$i/
    	wget `grep http://imgs.xkcd.com/comics/ index.html | head -1 | cut -d\" -f2`
    	rm index.html
    done
    (With the sequence number updated in line with the current strip number)

    If anyone could help me out, that would be great.
     
  2. brn2ski00 macrumors 68020

    brn2ski00

    Joined:
    Aug 16, 2007
    #2
    Make sure that the permissions are set to execute. Do an "ls -la" and see what the permissions are. If they are not set to execute, the script won't run.

    Run chmod filename -777 to rectify that.
     
  3. brn2ski00 macrumors 68020

    brn2ski00

    Joined:
    Aug 16, 2007
    #3
    Also, I think the first line in the script should be /sh not /bash....
     
  4. BrianFD3S thread starter macrumors member

    Joined:
    Feb 19, 2008
    Location:
    Ireland
    #4
    Ok, thanks very much. I haven't run it yet, because I thought I'd first ask- how are these files going to be downloaded [Autostart of Safari?], and where are they going to get saved? I don't want c500 scripts downloading themselves onto my desktop, my screen is only 15" :eek:

    ls -la xkcd.sh gives me -rwxr-xr-x
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    Run the script from the directory you want the files downloaded to. It will save them all to the current working directory.

    Also, Randall uses title text on every comic for extra comedic effect. If you just download the images, you will miss out on all of that. How to grab the title text as well would be a bit more difficult, but certainly not impossible.

    -Lee
     
  6. BrianFD3S thread starter macrumors member

    Joined:
    Feb 19, 2008
    Location:
    Ireland
    #6
    Brians-iMac:~/Desktop/xkcd [user]$ ./xkcd.sh
    -bash: ./xkcd.sh: cannot execute binary file


    Any ideas?
     
  7. brn2ski00 macrumors 68020

    brn2ski00

    Joined:
    Aug 16, 2007
    #7
    let me see the output from "ls -la"
     
  8. BrianFD3S thread starter macrumors member

    Joined:
    Feb 19, 2008
    Location:
    Ireland
    #8
    Brians-iMac:~/Desktop/xkcd [user]$ ls -la xkcd.sh
    -rwxrwxrwx 1 [user] [user] 318 Jul 15 15:57 xkcd.sh
     
  9. brn2ski00 macrumors 68020

    brn2ski00

    Joined:
    Aug 16, 2007
    #9

    Ok, I think that the first line in the script should read: #!/bin/sh
     
  10. BrianFD3S thread starter macrumors member

    Joined:
    Feb 19, 2008
    Location:
    Ireland
    #10
    I already changed it :/

    To create the actual file I used TextEdit, is that right?
     
  11. brn2ski00 macrumors 68020

    brn2ski00

    Joined:
    Aug 16, 2007
    #11
    The text editor doesn't matter: vi, pico, etc...

    The first line of the file doesn't read what I told you to put. It should be:

    Code:
    #!/bin/sh
    
    for i in `seq 1 449`
    do
    	wget http://xkcd.com/$i/
    	wget `grep http://imgs.xkcd.com/comics/ index.html | head -1 | cut -d\" -f2`
    	rm index.html
    done
     
  12. BrianFD3S thread starter macrumors member

    Joined:
    Feb 19, 2008
    Location:
    Ireland
    #12
    Yeah, I meant I had changed it on my computer. Starting with a new file it now tells me that it doesn't recognise 'seq' as a command-

    ./xkcd2.sh: line 1: seq: command not found


    I don't get it what's going on at all, so I really appreciate your help so far, thanks.
     
  13. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #13
    Did you save as text?
     
  14. BrianFD3S thread starter macrumors member

    Joined:
    Feb 19, 2008
    Location:
    Ireland
    #14
    I changed it to Plain Text and saved it as a .sh file.
     
  15. brn2ski00 macrumors 68020

    brn2ski00

    Joined:
    Aug 16, 2007
    #15
    Ok, back to square one.

    It looks like "seq" isn't part of the .sh scripting language, so maybe it was supposed to be /bash.

    Can you send me the file and I can try?

    Edit: Never mind, I just c/p the file from above.
     
  16. BrianFD3S thread starter macrumors member

    Joined:
    Feb 19, 2008
    Location:
    Ireland
  17. brn2ski00 macrumors 68020

    brn2ski00

    Joined:
    Aug 16, 2007
    #17
    Ok, this works and loops through the files, but doesn't seem to pull the correct data. Can you verify the line in the script where it points to a file?

    Code:
    #!/bin/bash
    COUNTER=0
    while [  $COUNTER -lt 449 ]; do
            wget http://xkcd.com/$i/
            wget `grep http://imgs.xkcd.com/comics/ index.html | head -1 | cut -d\" 
    -f2`
            rm index.html
            let COUNTER=COUNTER+1
    done
    I basically got rid of the for loop and used a while loop... now its looping, but the actually get command is not working. Mess around with that and I will be back to help later.
     
  18. BrianFD3S thread starter macrumors member

    Joined:
    Feb 19, 2008
    Location:
    Ireland
    #18
    Well the xkcd/$i/ one should run through xkcd.com/1/ to xkcd.com/449/, each of which is a webpage, but not specified as an html file.
     
  19. brn2ski00 macrumors 68020

    brn2ski00

    Joined:
    Aug 16, 2007
    #19
    This: (notice the variable change in the 4th line)

    Code:
    #!/bin/bash
    COUNTER=0
    while [  $COUNTER -lt 449 ]; do
            wget http://xkcd.com/$COUNTER/
            wget `grep http://imgs.xkcd.com/comics/ index.html | head -1 | cut -d\" -f2`
            rm index.html
            let COUNTER=COUNTER+1
    done
    
     
  20. brn2ski00 macrumors 68020

    brn2ski00

    Joined:
    Aug 16, 2007
    #20
    It's complaining about the wget command.
     
  21. BrianFD3S thread starter macrumors member

    Joined:
    Feb 19, 2008
    Location:
    Ireland
    #21
    Yeah, it sure is. Hmm.

    I've found that all the comic files are linked to via http://imgs.xkcd.com/comics/
    Is there any way of downloading them so that they retain the date of release? The order of them is kinda important :eek:
     
  22. brn2ski00 macrumors 68020

    brn2ski00

    Joined:
    Aug 16, 2007
    #22
    I can look into this further when I get home from work... I went home at lunch and ran those quick tests.

    Send me a pm and we can email -- that will be easier.
     
  23. Baron58 macrumors 6502

    Joined:
    Feb 19, 2004
    #23

    no, 'sh' is for the old bourne shell. BASH is 'bash'. :rolleyes:
     
  24. operator207 macrumors 6502

    Joined:
    Jul 24, 2007
    #24
    http://scruss.com/blog/2008/02/08/seq-for-os-x/

    Install sh-utils, and use the existing script posted here.
    I searched for a macport, but I could not find one that had seq in it.
     
  25. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #25
    Don't know if this contains other comics, but saw this path too while browsing around

    http://imgs.xkcd.com/comics_old/comics/

    All the data is right there (filename and mod date), so just read that file in and process line by line.
     

Share This Page