user1820333
user1820333

Reputation: 35

run shell script in program and input parameters

I am writing a C++ program for MAC OSX and I have a third parity program "parse" which is for uploading some .js to their server. There are just few cmd line to run in Terminal.

>parse new [folder]
Login>[email]
Pw>[pw]
projectID>[pid]

I want to auto. this process in my program, but I don't know how to pack all cmd togather.

when I call system("parse new [folder] && [email] && [pw] && [pid]");

the process hold at Login>

then I call system("parse new [folder] && wait && [email] && wait && [pw] && wait && [pid]");

it still hold at Login>

I would like to ask how can I input parameters for the cmd? thanks

Upvotes: 1

Views: 306

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 753455

The parse program appears to be reading from standard input. If that's correct, you'll need to ensure that it is given the email, directory and project ID on separate lines of input.

Test whether this works from a simple shell script:

parse new [folder] <<EOF
[email]
[pw]
[pid]
EOF

If it works, then you need to format the same command with multiple lines in the string you pass to system().

If it doesn't work, it suggests that the parse program is reading from something other than standard input; it might be futzing with /dev/tty or something else. I'd look hard at what it does with strace or a similar program to see what system calls it actually makes.

Your code using && etc won't work; that runs the parse command, and if it is successful, runs a program [email], and if by some mischance that works, then it runs [pw], and [pid].

Upvotes: 0

Blue Ice
Blue Ice

Reputation: 7922

I didn't remember exactly what the command was called, but it exists!

Now, I have found it. The expect command will wait for a prompt like the one you have described, and then it will pretend to be a keyboard and it will enter data. A quick example:

#!/usr/bin/expect


set timeout 20

spawn "./parse"

FOLDER="/x/y/z"
LOGIN="Randy"
PASSWORD="horse_stapler"
PROJECTID="136729"


expect "parse new " { send "$FOLDER" }
expect "Login>" { send "$LOGIN" }
expect "Pw>" { send "$PASSWORD" }
expect "projectID>" { send "$PROJECTID" }

interact

... more examples like this one here.

BUT WAIT.

There are 2 things that you need to know.

Firstly, The reason that the top of the script is "#!/usr/bin/expect" is because this is not bash. Instead this is it's own special kind of script. Read more at the link.

THIS IS VERY IMPORTANT. I'm not going to get into this, but NEVER, EVER store a password as plain text within a program. Do not give this program to people with their plain text password because it is a huge security vulnerability.

Upvotes: 1

Related Questions