Thread: create scripts inside a c prog

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    3

    Question create scripts inside a c prog

    Hi first post here..... so dont execute me at once


    I trying to find examples of the following

    i making a small .c progg that i want to create a outputfile called

    move-expect

    wich would contain this
    #!/usr/bin/expect -f
    set username someting
    set password something
    spawn sftp $username@localhost
    expect "assword:"
    send "$password\r"
    expect "ftp>"
    send "put file\r"
    expect "ftp>"
    send "quit"

    and then execute the file move-expect

    i know how to do it in a .sh script but cat <<_EOF>> dont seem to work to good inside a.c proggie

    hope someone knows how to do it

    cheers!

  2. #2
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    Oi! Is this what you ment. I use system() -> this work on Windows. Don think for Linux, change there system(emacs move-expect);
    Code:
    #include <stdio.h>
    
    int main()
    {
        FILE *fp;
        fp = fopen("move-expect","w");
        fprintf(fp, "#!/usr/binexpect -f\n"
               "set username something\n"
               "set password something\n"
               "spawn sftp $username@localhost\n"
               "expect \"assword\"\n"
               "send \"$password\\r\"\n"
               "expect \"ftp>\"\n"
               "send \"put file\\r\"\n"
               "expect \"ftp>\"\n"
               "send \"quit\"\n");
        fclose(fp);
        system("notepad move-expect");
        system("PAUSE");
        return 0;
    }
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  3. #3
    .
    Join Date
    Nov 2003
    Posts
    307
    What are your requirements for this project? If it works in shell leave it there.

    When you call system() in unix, you invoke a POSIX shell,( which may or may not have all the envrionment variables you reference in the script), not necessarily the shell you use to execute the script. It probably ain't worth the trouble.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    3
    xxxrugby
    thanks man works like a charm

    hope i can help you out sometime to!

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    For your system command, run:
    Code:
     system("/usr/binexpect move-expect");
    Don't forget to include stdlib.h as well.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    3
    Thanks for the help but i have another question...

    when you make a c prog like this it seems likewhen you edit the complied prog with lets say "vi" it show the entire script in cler text is there someway to get around that ?

    Cheers!

  7. #7
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by xxxrugby
    Oi! Is this what you ment. I use system() -> this work on Windows. Don think for Linux, change there system(emacs move-expect);
    Code:
    #include <stdio.h>
    
    int main()
    {
        FILE *fp;
        fp = fopen("move-expect","w");
        fprintf(fp, "#!/usr/binexpect -f\n"
               "set username something\n"
               "set password something\n"
               "spawn sftp $username@localhost\n"
               "expect \"assword\"\n"
               "send \"$password\\r\"\n"
               "expect \"ftp>\"\n"
               "send \"put file\\r\"\n"
               "expect \"ftp>\"\n"
               "send \"quit\"\n");
        fclose(fp);
        system("notepad move-expect");
        system("PAUSE");
        return 0;
    }

    concise and to the point but now that you have it down don't forget this. and its security implications.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > it show the entire script in cler text is there someway to get around that ?
    Having figured that writing a C program just to output a script and execute it was a pretty pointless thing to do (why not just write the script and be done with it), it now seems that your real question should have been "how do I stop people viewing the contents of my script".

    The answer may have been to use chmod
    chmod 700 move-expect
    read-write-executable by you, hidden from everyone else

    chmod 710 move-expect
    read-write-executable by you, executable (but not read/writable) by people in the same group.

    Yes, people can run your programs without having the priviledge to look inside them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  2. How to create a file association program?
    By eShain in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2006, 12:15 PM
  3. Cant create curses's newwin() inside a class
    By dimaash in forum Linux Programming
    Replies: 1
    Last Post: 02-01-2006, 08:26 AM
  4. Implant a callback inside a class
    By Rune Hunter in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2005, 08:20 PM
  5. Cannot create MDI Client Win
    By JaWiB in forum Windows Programming
    Replies: 1
    Last Post: 10-31-2005, 10:05 PM