Running a shell script from Ipod Touch App

Discussion in 'iOS Development' started by rocomotion, Jun 24, 2009.

  1. rocomotion

    rocomotion New Member

    Joined:
    Jun 24, 2009
    Messages:
    14
    Likes Received:
    0
    Hey, so, Basically I am wondering if it is possible to run a console script (.sh) from an iPhone App. The script is written to download a program from a repository that I set up, respring, then after a set time delete the program and respring again, so it needs root privileges, and is interactive in that the user can set how long the program will be kept. I have the bash script written, and it works fine when called from mobile terminal, but is there any way to do this from within a (jailbroken) app (without using mobile terminal / SSH / Bossprefs)? Or is there an overall better way to achieve this?
    Thanks
  2. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    Spawn a separate thread to run system() with the path to your file. Once system() returns you can send a message to the main thread that says it finished, at which point you can notify your user.
  3. rocomotion

    rocomotion New Member

    Joined:
    Jun 24, 2009
    Messages:
    14
    Likes Received:
    0
    Thanks so much for your reply

    Please Register or Log in to view images

    . Good to know that it is at least possible to run this script.
    My only other question is will this open the terminal to allow for the interactive aspects (entering the root password and the amount of time to keep the program)?
  4. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    Use popen if you want output from the command.
  5. rocomotion

    rocomotion New Member

    Joined:
    Jun 24, 2009
    Messages:
    14
    Likes Received:
    0
    OK so thanks again for the input guys. I've done some funky stuff which will allow my script to run as root even when it gets called from the user "mobile" (editing the sudoers file and calling 'sudo myscript.sh'). I've also read up on popen() and it does seem to be what I want...my question now is: where do I put this command?? I've tried putting it in a simple helloworld app like this:

    of course, this would just show the console output from my script right? Needless to say, this does not work. Anyway, I was wondering if anyone had any ideas for this. Thanks.
  6. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    An NSString is not a char*, don't use it like one.
  7. rocomotion

    rocomotion New Member

    Joined:
    Jun 24, 2009
    Messages:
    14
    Likes Received:
    0
    ah, so it looks like I need to use stringWithContentsOfFile or initWithContentsOfFile?
    still, this doesn't solve the further problem, which is that popen only allows either reading or writing, but not both...
  8. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    No, just use a char*, allocate some memory for it (I don't know if popen allocates for you), and then you can make an NSString from a char* with stringWithUTF8String:.


    And here is an implementation of popen2 (which allows for bi-directional communication) in C:
    Code:
    #include <sys/types.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    #define READ 0
    #define WRITE 1
    
    pid_t
    popen2(const char *command, int *infp, int *outfp)
    {
        int p_stdin[2], p_stdout[2];
        pid_t pid;
    
        if (pipe(p_stdin) != 0 || pipe(p_stdout) != 0)
            return -1;
    
        pid = fork();
    
        if (pid < 0)
            return pid;
        else if (pid == 0)
        {
            close(p_stdin[WRITE]);
            dup2(p_stdin[READ], READ);
            close(p_stdout[READ]);
            dup2(p_stdout[WRITE], WRITE);
    
            execl("/bin/sh", "sh", "-c", command, NULL);
            perror("execl");
            exit(1);
        }
    
        if (infp == NULL)
            close(p_stdin[WRITE]);
        else
            *infp = p_stdin[WRITE];
    
        if (outfp == NULL)
            close(p_stdout[READ]);
        else
            *outfp = p_stdout[READ];
    
        return pid;
    }
    
    Here is how you use it:

    Code:
    int
    main(int argc, char **argv)
    {
        int infp, outfp;
        char buf[128];
    
        if (popen2("sort", &infp, &outfp) <= 0)
        {
            printf("Unable to exec sort\n");
            exit(1);
        }
    
        write(infp, "Z\n", 2);
        write(infp, "D\n", 2);
        write(infp, "A\n", 2);
        write(infp, "C\n", 2);
        close(infp);
    
        *buf = '\0';
        read(outfp, buf, 128);
    
        printf("buf = '%s'\n", buf);
    
        return 0;
    }
    
    I found it on the Internet, so I can't verify if it works. The standard popen() cannot do bi-directional communication.
  9. rocomotion

    rocomotion New Member

    Joined:
    Jun 24, 2009
    Messages:
    14
    Likes Received:
    0
    thanks alot, I'm got to try that one out
  10. rocomotion

    rocomotion New Member

    Joined:
    Jun 24, 2009
    Messages:
    14
    Likes Received:
    0
    OK...so it turns out that the iPhone just doesn't like doing the stuff in the script, so I've figured out how to get most of it running through Obj-c and system() methods...which is probably what I should have been doing from the start

Share This Page