Give input to gdb debugger script - gdb

I want to pass some inputs to my gdb debugger to automate it.
Something like this:
Enter number of inputs:
5
Enter 5 inputs:
2 4 3 2 5
I have an expect script for that to automate my binary file.
Can I combine my expect script and gdb script?

Create a file testinput.txt containing:
5
2 4 3 5 2
Then in gdb:
(gdb) run < testinput.txt

Related

How do I store numbers in two arrays with specific order?

I want to open a text file and read it in its entirety while storing its contents into arrays using c++. I have my example text file below. I would like to store the first number into an array and the rest of them into a 2nd array line by line. For example 9 to be stored in first array and 22 22 at second array,then 1 to be stored at first array 2 3 4 at second array etc...I am not sure how to accomplish this in c++, any help is greatly appreciated!
9 22 22
1 2 3 4
1 5
2 3 6 9
For example when I print the first array i want to show: 9 1 1 2 (first column)
and when I print the second array I would like to show: 22 22 2 3 4 etc...
Here's one approach:
Create a loop goes on for as long as you can successfully read a line from the file. You'll need a std::string and std::getline for that.
In the loop:
Put the line you read in an std::istringstream to simplify the extraction.
Declare a temporary variable to use for extracting the numbers.
Try to extract a number from the istringstream.
If you successfully extracted a number:
Put the extracted number in arr1.
Create another loop where you extract all the rest of the numbers one by one from the istringstream and put them in arr2.

How to use 'sed' to automate changes of config file?

I am trying to create a script that dynamically finds line numbers in a .groovy config file and then utilizes the 'head/tail' command to insert multiple lines of code into the .groovy' config file. I cannot hardcode line numbers into the script because the vendor may alter the config and order of line numbers in the future. Anybody have suggestions for the best way to accomplish this?
EX.)
1: This is line one
2: This is line two
Problem: I need to insert:
test {
test{
authenticationProvider =/random/path
}
}
I cannot hard code the lie numbers in sed because they may change in the future. How can I dynamically make sed find the appropriate line number and insert multiple lines of code in the proper format?
this should do
$ line_num=2; seq 5 | sed "${line_num}r insert"
1
2
test {
test{
authenticationProvider =/random/path
}
}
3
4
5
to be inserted text is placed in the file named insert. Since there is no sample input file, I generated sequence of 5 as the input source.
Assuming you can find the line number, you could do this fairly easy with a bash script:
file insert-lines.sh:
#!/bin/bash
MYLINE=$1
FILE=$2
head -$MYLINE < $FILE
cat <<__END__
test {
test{
authenticationProvider =/random/path
}
}
__END__
tail +$((MYLINE+1)) $FILE
Then you can run this:
chmod 755 insert-lines.sh
./insert-lines.sh 3 .groovy > .groovy.new
mv .groovy.new .groovy
and the script will insert the block between lines 3 and 4 of the .groovy file.
Note that I'm assuming a recent Linux distro which supports the tail +n syntax, which outputs the end of the file starting at line n. You'll have to replace that part of the code if your version of tail does not support it.

looping over different list files, retrieving always the sam Eline

I have three files with a list of files I need to use later to apply a function.
I have managed to create a loop going through the 3 different files, but no exactly the output I need.
My input:
rw_sorted_test.txt
1
2
3
fwd_sorted_test.txt
A
B
C
run_list.txt
1st
2nd
3rd
I am running it like this:
for f in `cat rw_sorted_test.txt`; do for l in `cat fwd_sorted_test.txt`; do for r in `cat run_list.txt` do echo ${f} ${l} ${r}; done; done; done;
What I am obtain now is something like:
1 A 1st
1 A 2nd
1 A 3rd
2 A 1st
2 A 2nd
2 A 3rd
3 A 1st
(...)
What I am looking for is something like:
1 A 1st
2 B 2nd
3 C 3rd
I am sure that it will be something simple, but I am really beginner and all the workarounds have not been working.
Also, how can I then make it run after echo my desired output?
Thank you
Quick try, if it is this that you need:
exec 4< run_list.txt
exec 5< rw_sorted_test.txt
for a in $(cat fwd_sorted_test.txt); do
read run <&4
read sort <&5
echo "$sort $a $run"
done
...output is:
1 1st A
2 2nd B
3 3rd C
Files should also be closed:
exec 4<&-
exec 5<&-
The whole point is to do a single cycle, and read a line at a time from 3 different files. The files which are opened for input (exec ...< ...) should contain at least the same number of lines as the main file, which is controlling the loop.
Some reference could be find here: How do file descriptors work?
or doing some study on bash file descriptors. Hope it helps.
You can use this as a testcase for writing a program in awk:
3 inputfiles, store lines in an array and print everything in an END-block.
In this case you can use another program that works even easier:
paste -d" " rw_sorted_test.txt fwd_sorted_test.txt run_list.txt

how to merge odd number line and even number line in vim?

In order to merge the odd number line and the even number line in two methods.
One use command :s, the other use command :g and :s.
It's our homework and I could not get appropriate answer from the google.
And I had worked out the first one, which means I can solve it with command :s:
:%s/\(^.*$\)\n\(^.*$\)/\1 \2
And how could I use command :d and :s to solve it?
BEFORE:
1 aa
2 bb
3 abc
4 abc
5 an apple
6 is a bug
7 mazic
8 homework!
9 try a time
10 dodo
AFTER:
1 aa bb
2 abc abc
3 an apple is a bug
4 mazic homework!
5 try a time dodo
thanks to everyone and I have leant about how to solve it before the lesson.hah
:g/\(^.*$\)\n\(^.*$\)/s//\1 \2
What you can do here is :
Move the cursor to the line number to which you want to append the next line and then type below command in normal mode.
:s/\n/ /
Another way is go to the particular line and press SHIFT+V and then type below command:
:'<,'>s/\n/, /
Note that when you are in visual mode and press : then :'<,'> will automatically get typed. You just need to type regex ahead of that.
In both the above commands, g is not needed as it will not do any impact because only one \n will be there for each line.
You don't need to use :substitute here, there's a special command :join.
You can use the Ex command with :global, using ^ as the pattern to match all lines:
:global/^/join
Or use the shorter normal mode variant J:
:%normal! J

Script to automatically execute multiple runs of a c++ program

Consider a basic program - SumOf2Numbers.cpp. I can give 2 number as input through command line and it give the sum of the number.
I want to run this program with various inputs like,
./a.out 5 6
./a.out 123456 654321
./a.out -200 200
And the output would be,
5 + 6 = 11
-200 + 200 = 0
123456+654321 = 777777
I want to automate this process of executing the c++ code and storing the output in a file. I am new to writing scripts. I would like to know how I can do this ? I believe I can do this by writing perl or bash scripts. Can someone guide me to a nice tutorial on this.
PS: I am sure there would be lot of online tutorials. But I am not sure how exactly I should perform the search.
This can be done easily with a shell script:
#!/bin/sh
(
./a.out 5 6
./a.out 123456 654321
./a.out -200 200
) > output.txt