Suppose you have to run many applications on terminal (lets say gnome-terminal) and you want to automate this process, You can use command lines options of gnome-terminal.
Example
#gnome-terminal --geometry=80x20+700+0 --title=ping -x ping google.com &
where geometry will decide what will be length and width of your terminal and what will be the position of your terminal.
title sets the title of the terminal
-x is for the command to run
& to run this as background process.
(See man pages of gnome-terminal for more options, you can have tabs and many more options)
Now lets say we want to run following applications viz
1) my server lets say the name is /home/xyz/myServer.exe
2) tail -f /var/log/messages to see the log
3) ping google.com
4) ssh to some remote machine lets say xx.yy.zz.aa
So I will make a script say
automate.sh with following
#cat automate.sh
gnome-terminal --geometry=80x20+0+0 --title=myServer -x ./home/xyz/myServer.exe &
gnome-terminal --geometry=80x20+0+400 --title=logMessages -x tail -f /var/log/messages &
gnome-terminal --geometry=80x20+700+400 --title=ping -x ping google.com &
gnome-terminal --geometry=80x20+700+0 --title=remoteMachine -x ssh root@xx.yy.zz.aa &
Now you can run all the applications with a simple command
#sh automate.sh
Please remember that the opened window will close as soon as the command it is running stops executing, lets say the window/terminal running "tail -f /var/log/messages" will immediately go if you dont have permission to read /var/log/messages
Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts
C Linux - Making Libraries - Static and Dynamic
We will be using two files namely main.c and hello.c as following:
//Filename : hello.c
#include <stdio.h>
void hello()
{
printf("\n in hello.c hello .... \n");
}
//Filename : main.c
#include <stdio.h>
int main()
{
printf("\n in main.c calling hello \n");
hello();
}
1) Static Library
#gcc -c -fPIC hello.c -o hello.o
#ar rcs libhello.a hello.o
#gcc -static main.c -L. -lhello -o static_link_exe
#export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`pwd`
Run the application to test
#./static_link_exe
To verify that statically linked executable files statically linked libraries, you can do
#strings static_link_exe | grep hello
2) Dynamic Library / Shared Library
#gcc -c -fPIC hello.c -o hello.o
#gcc -shared -o libhello.so hello.o
#gcc main.c -o dynamic_link_exe -L. -lhello
#export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`pwd`
Run the application to test
#./dynamic_link_exe
To verify that dynamically linked executable files share libraries, you can do
#strings dynamic_link_exe | grep hello
Also verify using ldd
#ldd dynamic_link_exe
libhello.so => ./libhello.so
libc.so.1 => /lib/libc.so.1
libgcc_s.so.1 => /usr/sfw/lib/libgcc_s.so.1
libm.so.2 => /lib/libm.so.2
/platform/SUNW,Ultra-80/lib/libc_psr.so.1
See the libhello.so file is linked here
//Filename : hello.c
#include <stdio.h>
void hello()
{
printf("\n in hello.c hello .... \n");
}
//Filename : main.c
{
printf("\n in main.c calling hello \n");
hello();
}
1) Static Library
#gcc -c -fPIC hello.c -o hello.o
#ar rcs libhello.a hello.o
#gcc -static main.c -L. -lhello -o static_link_exe
#export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`pwd`
Run the application to test
#./static_link_exe
To verify that statically linked executable files statically linked libraries, you can do
#strings static_link_exe | grep hello
2) Dynamic Library / Shared Library
#gcc -c -fPIC hello.c -o hello.o
#gcc -shared -o libhello.so hello.o
#gcc main.c -o dynamic_link_exe -L. -lhello
#./dynamic_link_exe
#strings dynamic_link_exe | grep hello
Also verify using ldd
#ldd dynamic_link_exe
libhello.so => ./libhello.so
libc.so.1 => /lib/libc.so.1
libgcc_s.so.1 => /usr/sfw/lib/libgcc_s.so.1
libm.so.2 => /lib/libm.so.2
/platform/SUNW,Ultra-80/lib/libc_psr.so.1
See the libhello.so file is linked here
gcc - How to find what preprocessor does to a C program
There are gcc options that allow us to do only preprocessing or preprocessing and compiling or all preprocessing, compiling and linking to make an executable. If we need to see what does the compiler does during preprocessing we can ask it to do only preprocessing and see the result to find out what it does in preprocessing. This can be useful in many situations where we need to identify preprocessor errors, in academics etc.
Lets understand this with an example:
I am using Fedora8 with gcc - gcc (GCC) 4.1.2 20070925 (Red Hat 4.1.2-33)
Example contains a C file named "first.c" and a header file named "first.h" as following:
first.c
-------
#include "first.h"
#define MAX 10
int main()
{
int i;
int j;
printf("hello world \n");
i=MAX;
j=MIN;
printf("i - %d and MAX is %d\n",i,MAX);
printf("j - %d and MIN is %d\n",j,MIN);
return 0;
}
----
first.h
----
#ifndef __FIRST
#define __FIRST
#define MIN 5
#include <stdio.h>
#endif
----
if you compile the program and run it you will get
#gcc first.c
#./a.out
hello world
i - 10 and MAX is 10
j - 5 and MIN is 5
What we need to find out here is what preprocessor does to this program, as we all know, preprocessor does include the header file first.h in the program, which internally includes stdio.h (in my case it is /usr/include/stdio.h file), replaces MACROS (MIN and MAX here) with their values. In my case the gcc gives "-E" option for preprocessing only, find out your option by seeing manual page. So I run
#gcc -E first.c > first_only_preprocessor.out
and when you see the output file "first_only_preprocessor.out", you will find out what preprocessor does to our example C file.
Lets see, for example, what it does to our printfs, where we are printing MAX and MIN, I searched MAX and MIN in the output as follows:
# grep 'MIN\|MAX' first_only_preprocessor.out
OR
# gcc -E first.c | grep 'MIN\|MAX'
output is
printf("i - %d and MAX is %d\n",i,10);
printf("j - %d and MIN is %d\n",j,5);
As you see, preprocessor correctly replaces MAX with 10 and MIN with 5 :)
Think on the ideas where you can use this ...
Lets understand this with an example:
I am using Fedora8 with gcc - gcc (GCC) 4.1.2 20070925 (Red Hat 4.1.2-33)
Example contains a C file named "first.c" and a header file named "first.h" as following:
first.c
-------
#include "first.h"
#define MAX 10
int main()
{
int i;
int j;
printf("hello world \n");
i=MAX;
j=MIN;
printf("i - %d and MAX is %d\n",i,MAX);
printf("j - %d and MIN is %d\n",j,MIN);
return 0;
}
----
first.h
----
#ifndef __FIRST
#define __FIRST
#define MIN 5
#include <stdio.h>
#endif
----
if you compile the program and run it you will get
#gcc first.c
#./a.out
hello world
i - 10 and MAX is 10
j - 5 and MIN is 5
What we need to find out here is what preprocessor does to this program, as we all know, preprocessor does include the header file first.h in the program, which internally includes stdio.h (in my case it is /usr/include/stdio.h file), replaces MACROS (MIN and MAX here) with their values. In my case the gcc gives "-E" option for preprocessing only, find out your option by seeing manual page. So I run
#gcc -E first.c > first_only_preprocessor.out
and when you see the output file "first_only_preprocessor.out", you will find out what preprocessor does to our example C file.
Lets see, for example, what it does to our printfs, where we are printing MAX and MIN, I searched MAX and MIN in the output as follows:
# grep 'MIN\|MAX' first_only_preprocessor.out
OR
# gcc -E first.c | grep 'MIN\|MAX'
output is
printf("i - %d and MAX is %d\n",i,10);
printf("j - %d and MIN is %d\n",j,5);
As you see, preprocessor correctly replaces MAX with 10 and MIN with 5 :)
Think on the ideas where you can use this ...
C - Questions one should ask...
Last week, I was explaining someone the difference between switch case and if-else if, I thought of writing this blog to ask some questions. Try finding the answers, if you cant, ping me, I will try to answer:
Plz note that, this is meant for beginners only and I will update the list as I come across the questions. If you have questions like this, do ping me :)
Q1: When to use "switch" and "if-else if"?
Q2: What should be the ideal/efficient way of ordering the cases in switch cases?
Q3: When you would rather not have a break in a switch case?
Q4: When you would want the default case to be the first case in a switch case?
Q5: When to use different types of loops?
Q6: How to use different number systems in programming?
Q7: When to use ternary operator?
Q8: When to use union then structure?
Q9: Whats the difference between heap and stack memory?
Q10: How to find out what preprocessor does to a C program file?
Q11: What is preprocessing, compiling and linking?
Q12: When to use pointers and when not to use them?
Q13: Whats the use of return value from main function, How you can use it?
Q14: When to use iteration(loops) and when to use recursion?
Plz note that, this is meant for beginners only and I will update the list as I come across the questions. If you have questions like this, do ping me :)
Q1: When to use "switch" and "if-else if"?
Q2: What should be the ideal/efficient way of ordering the cases in switch cases?
Q3: When you would rather not have a break in a switch case?
Q4: When you would want the default case to be the first case in a switch case?
Q5: When to use different types of loops?
Q6: How to use different number systems in programming?
Q7: When to use ternary operator?
Q8: When to use union then structure?
Q9: Whats the difference between heap and stack memory?
Q10: How to find out what preprocessor does to a C program file?
Q11: What is preprocessing, compiling and linking?
Q12: When to use pointers and when not to use them?
Q13: Whats the use of return value from main function, How you can use it?
Q14: When to use iteration(loops) and when to use recursion?
Linux tips for newbies
1) Dont remember the command
#apropos "list directory"
2) use tab to complete the type, dont type all the words
3) to goto last dir
#cd -
4) to goto home dir
#cd ~
5) want to find that file
#locate filename
6) for tips on VI editor, see VI post
7) report file system disk space usage
#df -kh
8) to get/set hard disk parameters
#hdparm -t /dev/sda5
replace /dev/sda5 with the name of your disk
9) Use seq command to generate sequences
# seq 1 5
output will be
1
2
3
4
5
This you can use in many places including scripts, eg
# for i in `seq 1 5`; do echo "i is $i" ; done
output will be
i is 1
i is 2
i is 3
i is 4
i is 5
10) Sending mail through linux command line
First check, whether sendmail is running
#/etc/init.d/sendmail status
if not run it via (you need to be root for this)
#/etc/init.d/sendmail start
then, send mails using either mail or mutt command as below:
# echo "body of the mail" | mail -s "subject of the mail" toAddress
Give recipient's mail id in place of toAddress
As for body of the mail, you can also redirect from a file, like
# mail -s "subject of the mail" toaddress < body_mail.txt
if you want to send file as attachment, you can use mutt instead
# echo "body of the mail" | mutt -s "subject of the mail" -a fileToAttach.txt toAddress
Give recipient's mail id in place of toAddress
#apropos "list directory"
2) use tab to complete the type, dont type all the words
3) to goto last dir
#cd -
4) to goto home dir
#cd ~
5) want to find that file
#locate filename
6) for tips on VI editor, see VI post
7) report file system disk space usage
#df -kh
8) to get/set hard disk parameters
#hdparm -t /dev/sda5
replace /dev/sda5 with the name of your disk
9) Use seq command to generate sequences
# seq 1 5
output will be
1
2
3
4
5
This you can use in many places including scripts, eg
# for i in `seq 1 5`; do echo "i is $i" ; done
output will be
i is 1
i is 2
i is 3
i is 4
i is 5
10) Sending mail through linux command line
First check, whether sendmail is running
#/etc/init.d/sendmail status
if not run it via (you need to be root for this)
#/etc/init.d/sendmail start
then, send mails using either mail or mutt command as below:
# echo "body of the mail" | mail -s "subject of the mail" toAddress
Give recipient's mail id in place of toAddress
As for body of the mail, you can also redirect from a file, like
# mail -s "subject of the mail" toaddress < body_mail.txt
if you want to send file as attachment, you can use mutt instead
# echo "body of the mail" | mutt -s "subject of the mail" -a fileToAttach.txt toAddress
Give recipient's mail id in place of toAddress
Subscribe to:
Posts (Atom)
Books I like
- Inside C++ Object Model
- Unix Network Programming - Stevens
- Professional-c++-programmer-to-programmer - wrox
- Beautiful-code-leading-programmers-explain-how-they-think-theory-in-practice