Archive

Archive for December, 2009

UNIX – Lesson 009 – Standard file descriptors, Input Redirection, Output Redirection and I/O Redirection

Standard file descriptors
Whenever any program is executed (i.e. when the user types a command) the program has 3 important files to work with. They are standard input (stdin), standard output(stdout), and standard error(stderr).
At the end, each shell open three kind of files:
stdin, stdout and stderr

  • stdin = standard input file, from which shell reads its input (generally the KEYBOARD)
  • stdout = standard output file, where shell writes its normal output (generally DISPLAY/SCREEN)
  • stderr = standard error file, where shell writes its error messages (generally DISPLAY/SCREEN)

================================================================================
Input Redirection
Any command that reads its input from stdin can have its input redirected from another file.
This is called input redirection (<)

Example:
$ mail e.c@xarabas.com < input.txt
It sends an email to e.c@xarabas.com with the content of input.txt file as body of email.

================================================================================
Output Redirection
Any command that produces an output to stdout (that normally goes to the terminal) can have its output redirected to another file instead.
This is the output redirection > or >>
Note that > and >> works in different way in the two examples below.

  • In the first one, if the output file doesn’t exist it’s created, else it’s overwritten.
  • In the second (>>), if the file doesn’t exist it’s created, else the output is
    appended to the existing file.

Example:
$ ls -al > list_file.txt
It creates a file named “list_file.txt” and write the directory listing in that file.

$ date > date.out
It creates or overwrites a files called date.out

$ date >> date.out
It appends the output in date.out file

================================================================================
I/O Redirection
This is a very popular feature that many Unix users are happy to learn. In case you have worked with Unix for some time, you must have realised that for a lot of commands you type you get a lot of error messages. And you are not really bothered about those error messages. For example whenever I perform a search for a file, I always get a lot of permission denied error messages. There may be ways to fix those things. But the simplest way is to redirect the error messages elsewhere so that it doesn’t bother me. In my case I know that errors I get while searching for files would be of no use to me.

Here is a way to redirect the error messages
$ my_program 2>errorsfile
This above command would execute a program named “my_program” and whatever errors are generated while executing that program would all be added to a file named “errorsfile” rather than be displayed on the screen. Remember that 2 is the error output file descriptor. Thus “2>” means redirect the error output.

$ my_program 2>>all_errors_till_now
The above command would be useful in case you have been saving all the error messages for some later use. This time the error messages would append to the file rather than create a new file.

You might realize that in the above case since I wasn’t interested in the error messages generated by the program I redirected the output to a file. But since those error messages don’t interest me I would have to go and delete that file created every time I run that command. Else I would have several such files created all over whenever I redirect my unwanted error output. An excellent way around is shown below

$ find / -name s*.jpg 2>/dev/null

What’s /dev/null ?
That something like a black hole. Whatever is sent to the “/dev/null” never returns. It simple disappears.

There is a way for redirect both standard output and standard error

  • SH syntax:
    > file_name 2>&1      redirect both stdout and stderr to file_name
    >> file_name 2>&1    append both stdout and stderr to file_name
  • CSH syntax:
    >& file_name               redirect stdout and stderr to file_name
    >>& file_name             append stdout and stderr to file_name
Advertisements
Categories: Unix

PHP – Example 0004

Fourth Example 0004

<html>
<head>
<title>Fourth example</title>
</head>

// Fourth Example (VARIABLES INSIDE A STRINGS)
// ————————

$name = “Xarabas”;
echo “Hi, $name”;
echo ” <br />”;
echo ‘Hi, $name’;
echo ” <br />”;

?>

</body>
</html>

================================================================================

In this example, we see the using of variables inside a string.
In
echo “Hi, $name”;
here the variable $name is evaluated because we use a double quote ( ).

In
echo ‘Hi, $name’;
here the variable $name isn’t evaluated because we use  a single quote ( ).

The output window you will see the following:

Hi, Xarabas
Hi, $name

Categories: PHP

PHP – Example 0003

Third Example 0003

<html>
<head>
<title>Third example</title>
</head>

// Third  Example (STRING)
// ————————
$my_string = ‘first string’;
$another_string = “another string”;

echo $my_string;
echo ” <br />”;
echo $another_string;
echo ” <br />”;
?>

</body>
</html>

================================================================================

In this example, we see the using of string and variable.
In
$my_string = ‘first string’;
$another_string = “another string”;
we see two ways of using a variable with a single quote ( ) and with a double quote ( ).

In
echo $my_string;
echo $another_string;
we see the “echo” function to print the variable value.

The output window you will see the following:

first string
another string

Categories: PHP

UNIX – Lesson 008 – find and umask commands

find
The “find” command utility recursively descends the directory hierarchy for each path seeking files that match a boolean expression.
Syntax :
find path “expression”

Key argument :

  • -atime n           True if the file was accessed n days ago
  • -ctime n           True if the file’s status was changed n days ago
  • -mtime n          True if the file’s data was modified n days ago
  • -group “name”   True if the file belongs to the group “name”
  • -name pattern  True if pattern matches the current file name
  • -size n              True if the file is n bytes
  • -type x              True if the type of the file is x
  • -newer myfile    True if current file has been modified more recently than myfile
  • -print                 Always true; causes the pathname to be printed
  • -user uname      True if the file belongs to the user uname
  • -perm mode       Seek for files with permissions of type “mode”

Example:

$ find . –name “xarabas.txt”
./temp/home/xarabas.txt

$ find . –print
.
./Report.log
./Reports
./Reports/Report.log
./backup.sh

$ find . -perm u=rwx,g=rx,o=rx
./Report.log
./Reports
./backup.sh

$ find /tmp -name “ct*” -print
/tmp/ctn368

$ find /usr/local -type d -print
/usr/local
/usr/local/bin
/usr/local/bin/sge_arch
/usr/local/bin/plugins
/usr/local/bin/tkcvs_7_1_2
/usr/local/bin/tkcvs_7_1_2/bitmaps
/usr/local/bin/tkcvs_7_1_2/tkcvs
/usr/local/bin/tkcvs_7_1_2/tkdiff
/usr/local/bin/sparcv7
/usr/local/bin/sparcv9
/usr/local/bin/mgdiff_dir

================================================================================
umask
The “umask” utility sets the file mode creation mask of the current shell execution environment to the value specified by the mask operand. This mask affects the initial value of the file permission bits of subsequently created files. The value of each specified digit is subtracted from the corresponding “digit” specified by the system for the creation of a file.
Syntax :
umask ###

Without set umask, files are created with 666 permission (rw-rw-rw-) an the directories with
777 permission (rwxrwxrwx).
With umask 022 files are created with 644 permission (rw-r–r–) and directories with 755
permission (rwxrw-rw-).

666 – 022 = 644
777 – 022 = 755

Example:
$ umask 000
Reset the mask permission to 666

$ umask
0
Now the umask is set to 666

$ touch test
$ ls -la test-rw-rw-rw- 1 xarabas admins 0 Feb 21 14:22 test

$ umask 022
Now we set the permission to 644

$ touch test2
$ ls -la test2
-rw-r–r– 1 xarabas admins 0 Feb 21 14:23 test2

Categories: Unix

NetWorx

NetWorx – Free Bandwidth Monitoring and Usage Reporting

NetWorx is a simple and free, yet powerful tool that helps you objectively evaluate your bandwidth situation. You can use it to collect bandwidth usage data and measure the speed of your Internet or any other network connection. NetWorx can help you identify possible sources of network problems, ensure that you do not exceed the bandwidth limits specified by your ISP, or track down suspicious network activity characteristic of Trojan horses and hacker attacks. The program allows you to monitor all your network connections or a specific network connection (such as Ethernet or PPP) only.

The software also features a system of highly customizable visual and sound alerts. You can set it up to alert you when the network connection is down or when some suspicious activity, such as unusually heavy data flow, occurs. It can also automatically disconnect all dialup connections and shut down the system. The incoming and outgoing traffic is represented on a line chart and logged to a file, so that you can always view statistics about your daily, weekly and monthly bandwidth usage and dialup duration. The reports can be exported to a variety of formats, such as HTML, MS Word and Excel, for further analysis.

 

 

Homepage: http://www.softperfect.com/products/networx/

Download Page:  http://www.softperfect.com/download/freeware/networx_setup.exe

Amic Email Backup

 

Amic Email Backup

Amic Email Backup is a handy software tool for Microsoft Windows based PCs designed to create backup copies of your email database and Web browser Favorites. Amic Email Backup can save emails, address books, mail and news accounts and their settings, message rules, blocked sender lists and signatures, to a single compact, compressed backup file that can be easily restored when necessary.
It supports the top 11 email clients such as MS Outlook, Outlook Express, Eudora, Netscape Messenger, IncrediMail and others.
It supports the most popular browsers.

Some of the reasons to do backups: In case virus infects your computer, software failures, or power outages can destroy the data on your hard drives. Why would you have to re-download or even loose all you existing email messages ( together with address book, contacts, mail rules and all other email data ) every time your system crashes or you re-install Windows? You can just use Amic email Backup to create a compressed backup copy of your email data and store it in a safe place. After a crash or a format, simply restore the backup and your data will be back in no time!
This is where Amic Email Backup comes to rescue, it allows the home and small business computer user to save the email folders and settings easily, either manually or with the automated wizard, at scheduled intervals. This eliminates the need for an unnecessary complex and expensive backup software.

The software is very easy and intuitive to use, has two working modes, a Wizard mode and a standard mode.
The Schedule feature allows you to backup the email database daily, weekly or at any pre determined time frame. The email database can be saved on one computer and restored to another, thus making Amic Email Backup not just a backup utility but also a synchronization tool.

Homepage: http://amictools.com/v-amic_email_backup.html

Download Page: http://www.amictools.com/download/AmicEmailBackupSetup.exe

PHP – Example 0002

Second Example 0002
==================================================================================

<html>
<head>
<title>Second example</title>
</head>

<body>
<?php
// Second Example
// ————————
echo “Today is ” . showDate();
function showDate(){
$date = getdate();
return ($date[“mday”] . “-” . $date[“mon”] . “-” . $date[“year”] );
}
?>
</body>
</html>

==================================================================================
In this example we see a call to a function (i.e. showDate()).
The output is depending of the day when you execute it.
The variable $date is the date, and the value is obtained thanks to getdate() function;
The following values are
Day = $date[“mday”]
Month = $date[“mon”]
Year = $date[“year”]
and using the return command, you can output the value of Day, Month and Year and then you have to use “.” you concatenate them.

We explain better in the next lessons.

Categories: PHP
%d bloggers like this: