Tag:
programming

Command substitution in BASH

by
Ryan
on
November 7, 2008

BASH has the ability to execute a command string and replace that string with the output of the command. Or to say it another way, the output of a command will replace the command itself.
There are 2 ways to accomplish this.
The first is to surround the command with backticks or blockquotes.
`command`
You can also substitute with [...]

Read More
No Comments
bash

Find IP address from remote end of a TCP socket

by
Ryan
on
October 31, 2008

In C or C++ it is fairly simple to find the IP address of the remote end of a TCP socket.
The following example shows how to do this using the getpeername() and inet_ntoa() system calls.

int sockfd;
int len;
char * hostip;
struct sockaddr_in sin;

len = sizeof(sin);

if (0 != getpeername(sockfd, (struct sockaddr *) &sin, &len))
[...]

Read More
No Comments
C

Delete a specific line from a text file with sed

by
Ryan
on
October 26, 2008

At some point you may have the need to remove all lines within a text file that match a certain pattern. Accomplishing this is easy with the sed command.
Here is the command format.

$ sed -i ‘/PATTERN/ d’ file.txt

The ‘-i‘ option allows you to edit the specified file in place.
PATTERN is a regular expression
d [...]

Read More
2 Comments
bash

Display hostname and IP address in C

by
Ryan
on
October 18, 2008

Here is a good way to determine the hostname and IP address of the local machine in C.
You first have to grab the hostname with gethostname().

char hostbuf[256];
gethostname(hostbuf,sizeof(hostbuf));

Take the hostname and use it to grab the hostent struct with gethostbyname().

struct hostent *hostentry;
hostentry = gethostbyname(hostbuf);

Finally you have to take the hostent that is returned and pull out [...]

Read More
3 Comments
C

Single versus double quotes in BASH

by
Ryan
on
October 15, 2008

Understanding the difference between double versus single quotes is important when using BASH. Many times you may have seen them being used interchangeably. The basic difference is that variable names will be expanded within double quotes but not within single ones.
This example shows the normal output with no quotes.

$ echo $USER

ryan

As you can see [...]

Read More
1 Comment
bash

Format output using printf

by
Ryan
on
October 4, 2008

printf and its family of functions allow you to easily format string data. There are so many formatting options that it is easy to miss some of its more powerful functionality. Here is a rundown of the more useful features and some examples.
The basic printf format.

printf(format string, format parameter list);

The format string is composed [...]

Read More
No Comments
C
Copyright 2008-2010 WiredRevolution.com. All rights reserved.