How to get similar functionality of Expect in a C program [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have the below expect script
#!/usr/bin/expect
set timeout 60
set user [lindex $argv 0]
set password [lindex $argv 1]
set command [lindex $argv 2]
spawn su $user
expect "Password:"
send "$password\r";
send "$command\r";
send "exit\r";
interact
How do i do the similar operations in C?
AFAIK the header expect.h provides similar functionality but i am kind of lost while using it
Any help is appreciated

If you are not locked on expect, i believe the easiest way to handle the execution of another process is via forkpty.
It allows you executing a process and control its input/output via a file descriptor.
Here is just an example i googled for.

Related

pass a linux command line into two executables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I have a C program which reads its input from the command line, I would like to feed the executable with the output of ls | wc -m command, as I need to call two instances of the executable (./a.out1 , ./a.out2) using that same input and make them running in parallel (pipes ?).
Thank you for your help in advance!
You say you want to use a pipe, so first of all you need to adapt your program to read the input from stdin instead of argc and argv. Input passed via a pipe is not added to the command line argument list.
To pipe stdout of a process to multiple other processes, you can use tee and process substitutions:
ls | wc -m | tee >(./a.out1) >(./a.out2) >/dev/null
However, the reason why you require it be piped (as opposed to passed as an argument) isn't clear to me, so storing the output in a variable as suggested in the comments would work just as well for the example you present.

bash array from string keeping only content between delimiters [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to create a bash script that uses info pulled from Wikipedia via curl to help sort my music collection. I've gotten it to reliably return the information I want, but because of Wikipedia's formatting, there is sometimes information I want to discard. It is also not always formatted consistently; sometimes it is on multiple lines, and sometimes only one, but the information I want is consistently delimited between "[[" and "]]". I want to keep only the text between [[ and ]] and ignore the rest. All of the solutions I've found so far use sed and rely on consistent formatting. Basically what I want to do is take a long string formatted:
{{[[abcd]]efgh[[hijk]]lmno
[[pqrs]]
[[tuvw]]yz}}
and create an array with the values
abcd
hijk
pqrs
tuvw
With GNU grep and a Perl-compatible regular expression (PCRE):
grep -Po '(?<=\[\[).*?(?=]])' file
Output:
abcd
hijk
pqrs
tuvw

Linux script in C get file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have own script in C for linux, it is working with string loaded from file. I compiled on server gcc -pthread -o pipeline pipeline.c. It is working. Now I am running this script any like this ./pipeline UPPERCASE LOWERCASE < my_file.txt. I can read all arguments but I dont know how to read file name behind < in this script. It is possible, or how is it working?
The contents of that file will show up on your process's standard input (stdin). You don't get the name of the file, as the same interface will be used in situations where there's no filename, such as when the output of another process is piped to yours (doSomething | pipeline UPPERCASE LOWERCASE), or when the user's terminal is used for input by default.

string manipulation in AWK or in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a problem, I have a file with some number delimited by "". For example "125" etc.
An example of the file is:
10.0.0.0 11.0.0.0 "1200"
10.0.0.1 11.0.0.0.1 "200"
11.0.0.1 11.0.0.2 "320"
I use AWK for take the data but my problem is that I have to take only the integer value of the third column without "" because after I need to have some calculation with this numbers.
The solution is good also in C language.
Someone can help me?
Thanks
In awk, you'll have to strip off the quotes manually
$ echo '"2134"' | awk '{gsub(/(^")|("$)/,"",$1); print $1+2}'
2136
Obviously, this is not a C answer.

Is it possible to pipe multiple inputs into one program? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a C program that takes in 2 separate inputs through the read(0,buffer,size(buffer)) function.
They take two different inputs. Is it possible, through bash command only, to pipe two pytho -c or perl -e scripts into the C program? Or do I have to change its source code? Thanks in advance
You can use a command group
{
echo "First command"
echo "Second command"
} | nl
Or on one line for your interactive editing convenience:
{ echo "First"; echo "Second"; } | nl

Resources