1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

.sh script ( closing terminal when script is completed)

Discussion in 'Mac Programming' started by forbade, May 10, 2006.

  1. macrumors member


    I have an applescript calling a .sh script.

    It opens up a termin al window and then executes the script.
    all works ok, but I would like the terminal to close once the script is done
    is there any way to close the Terminal window via applescript or the .sh script ?
  2. Moderator


    Staff Member

    Open Terminal.app

    Go (Menu) Terminal->Window Settings

    Choose Shell from the popup-menu.

    Change "When the shell exits" to "Clost the window"

    Hit the "Use Settings as Defaults" button

    Try running your script.
  3. macrumors member


    nah, its giving me problems.
    i dont know why.?

    It wont close on exit. hm, It executes the script fine,but
    It will leave the terminal window running, and if I type exit then it will close
    I appended an exit command at the end of the script but it still doenst work.

  4. Moderator


    Staff Member

    What is your script?
  5. macrumors member


    its actually just a test script right now, so I can work out the bugs with closing the terminal window. I will be using slightly more advanced scripting later on.

    Here is the apple script I am using.
    tell application "Terminal"
    do script "./test.sh"
    end tell

    echo Test
    sleep 3

    I am using Filmaker PRO 8, to execute the applescript, which in turn executes a shell script.

    I am going to use this so I can sync database's over our network via ssh.
    and other handy things that might be useful through a filemaker button. :D

    Just a little pinch?
  6. macrumors member


    what would be even better is if I could bypass the terminal app completely. or at least have it run with out the window.
  7. macrumors member


    I guess, applescript executes the .sh file before even applying the .profile to the shell, because it wont let me use alias' in the script, but when I type the alias in the remaining terminal it works fine.

    ./test.sh: line 3: nsm: command not found <nsm alias doesnt work
    Mac1:~ mac1$ nsm < now it does....
    Last login: Wed May 10 16:16:36 2006
    Welcome to Darwin!
    SERVER:~ nsm$
    ./test.sh: line 3: nsm: command not found
    < so it is running the sh script here, and then starting the terminal prog. ???
    mac1:~ mac1$ nsm
    Last login: Wed May 10 16:16:36 2006
    Welcome to Darwin!
    SERVER:~ nsm$
  8. Moderator


    Staff Member

    Since your calling from Applescript why can't you close the window from there?
  9. macrumors 6502a

    Why are you activating Terminal at all?

    According to http://developer.apple.com/technotes/tn2002/tn2065.html it appears that you should just be able to use:

    do shell script "./test.sh"
    My quick test of this indicates that it works. The above tech note may be helpful if you decide that you want to pass arguments to your shell script. Note that you may need to specify the full path to your script in the applescript... I'm not certain what environment applescripts are executed under.
  10. macrumors 68020


    My thoughts exactly. I use "do shell script ..." as well and it works without any need for Terminal.

    If you read the tech note, it warns you about your aliases etc. I believe Applescript just executes the shell scripts in the default shell environment. Tech note tells you how to tell Applescript to execute the commands inside a different shell, but if you need your own aliases, I think you could do something like

    do shell script "~/set_aliases.sh; ~/test.sh"
  11. macrumors member


    Thanks for all the help

    I havent had time to research Applescript too much, It really helps to bank off your guys knowledge. I appreciate it.
  12. macrumors member


    How do you tell applescript to close the window?
  13. macrumors member


    The problem I have been encountering with
    - do shell script "~/lcr/test.sh"
    is that it won't let me use ssh, so it works ok for local commands but when I want to rsync via ssh, it gives me problems.
    So it looks like I am going to have to invoke the terminal window anyway.
  14. macrumors 6502a

    Yeah, looks like ssh isn't being allowed to access some of the root services it uses when invoked through Applescript.

    Reading back over your original post I'm confused as to why you would be using ssh in the shell script. Is there some reason why you cannot just keep an ssh tunnel open between the two machines and then rsync localhost:sshtunnelport?
  15. Moderator


    Staff Member

    Is there some reason you can just make a shell script and put it in launchd to periodically sync your data?
  16. macrumors 603


    or the cron daemon?

    I see Apple advertises the use of launchd instead..
  17. macrumors member



    Yes, but.
    I need be able to update specific laptops that are mobile. They are not always on the network, so I want to be able to have a button within Filemaker that will update automaticly before the Person leaves for home etc etc.
    I just want a manual option available. Several of our office personal are in and out of the office constantly, its more for them than anything else.
  18. macrumors 68020


    Well, how is ssh configured currently? If you are using keyboard authentication, it will fail. If you use public-key authentication, you will be in a much better situation.

    For example, I just ran the following script:

    do shell script "touch ~/mymonkey;scp ~/mymonkey <host-deleted>:~/mymonkey"

    It worked just fine... the host now had the file: ~/mymonkey.

    So, your script /will/ work, but you will have to setup those portable machines with public key authentication for SSH to work 'simply'. That or ask for the username/password in the applescript, and then pass that along to the script to use and pass to SSH.
  19. macrumors 6502a

    If you try that using "ssh <some host> uname" to execute "uname" on the remote system you may find that it doesn't work. At least it didn't work on my OS X systems.

Share This Page