Today


Showing posts with label Tips N tricks. Show all posts
Showing posts with label Tips N tricks. Show all posts

Wednesday, February 9, 2011

Purging the process, Part 2

Advanced topics in pipes, filters, and redirection

Last month I covered several basics, such as input redirection:

$ grep "hello" <hello.txt
say hello.
 Purging the process: Read the whole series! 
Part 1. The basics of pipes and redirections
Part 2. Pipes and redirection: More advanced features
Output redirection:
$ grep "hello" >junk.txt
Now is the time
for every good person to
say hello.
(type control-D here)
$ cat junk.txt
say hello.
$
Input and output redirection, and the use of input files on the command line instead of redirected input:
$ grep "Now" <hello.txt >junk.txt
$ grep "Now" hello.txt >junk.txt
Appending additional data to a file using an output redirection:
$ echo "Now is the time" >hello.txt
$ echo "for every good person to" >>hello.txt
$ echo "say hello." >>hello.txt
$ cat hello.txt
Now is the time
for every good person to
say hello.
$
Redirecting standard output and standard error, and redirecting standard error to the /dev/null byte wastebasket:
$ find / -name *.txt -exec ls -l {} \; 2>/dev/null >textfiles
$
Basic pipes:
$ grep "hello" < hello.txt | sed -e "s/hello/bye/" > result.txt
$( grep "hello" | sed -e "s/hello/bye/" ) < hello.txt > result.txt
$
I also stated that redirecting output to an existing file would delete the file and create a new version of it. In the following example, the fourth line causes hello.txt to be overwritten with a new version of the file containing only a single line, bye.
$ echo "hello" >hello.txt
$ cat hello.txt
hello
$ echo "bye" >hello.txt
$ cat hello.txt
bye
You can set the noclobber option to prevent redirected files from automatically overwriting their predecessors. In the following example, the option causes an error message at line six when the user tries to overwrite the hello.txt file.
$ set noclobber
$ echo "hello" >hello.txt
$ cat hello.txt
hello
$ echo "bye" >hello.txt
File "hello.txt" already exists
$ cat hello.txt
hello
unset noclobber
If noclobber is set, you can force a redirection to clobber any pre-existing file by using the >| redirection operator. This operator looks like a redirection to a pipe, but it's actually just a force redirect to override the noclobber option. In the following example the forced redirection operator prevents any error messages.
$ set noclobber
$ echo "hello" >|hello.txt
$ cat hello.txt
hello
$ echo "bye" >|hello.txt
$ cat hello.txt
bye
unset noclobber
Combining standard output and standard error
Redirection is frequently used for jobs that run for a long period of time, or for jobs that produce a lot of output. For such jobs, redirection can capture the results in a file. When this is done, it's also necessary to capture any output errors. Remember that if you redirect standard output but not standard error, output will go to a file and error messages will still go to your screen. The following find command will save the results to found.txt, although errors still appear on the screen.
$ find / -name *.txt -exec ls -l {} \; >found.txt
find: /some/directory: Permission denied
find: /another/one: Permission denied
$
The redirection operator is actually a number followed by the redirection symbol, as in the following example. If number is omitted, 1 is the default.
$ find / -name *.txt -exec ls -l {} \; 1>found.txt
$
The following commands are equivalent:
$ find / -name *.txt -exec ls -l {} \; 1>found.txt
$ find / -name *.txt -exec ls -l {} \; >found.txt
$
Unix utilities open three files automatically when a program starts up. These files are given file descriptor numbers inside the program -- 0, 1, and 2 -- but they're more commonly known as stdin (standard input -- file descriptor 0), stdout (standard output -- file descriptor 1), and stderr (standard error -- file descriptor 2). When the program starts, default assignments for these files are made to /dev/tty, which is the device name for your terminal. The stdin file is assigned to the keyboard of your terminal, while stdout and stderr are assigned to the screen of your terminal. The output redirection operator defaults to 1; thus > and 1> are equivalent. The input redirection operators < and <0 are equivalent. Redirecting standard error, file descriptor 2, requires that its number be explicitly included in the redirection symbol.
The following examples use 1> to redirect standard output because it helps clarify how the redirection works. When reviewing these examples remember that > and 1> are the same.
One method of handling the logging problem would be to create separate logs for each of the outputs, as in the following example.
$ find / -name *.txt -exec ls -l {} \; 1>found.txt 2>errors.txt
$
It is also possible to redirect an output by attaching it to an already open redirection using the >& redirection operator. In the following example, the standard output of find is redirected to the file result.txt. The 2>&1 redirection command instructs the shell to attach the output from standard error (2) to the output of standard output (1). Now both standard output and standard error are sent to result.txt.
$ find / -name *.txt -exec ls -l {} \; 1>result.txt 2>&1
$
The order of redirection is important. In the following example, the output of file descriptor 2 (standard error) is attached to file descriptor 1. At this point, standard output is still attached to the terminal, so standard error is sent to the terminal. The next redirection sends standard output to result.txt. This redirection doesn't drag file descriptor 2 along with it, so standard error is left pointing to the terminal device.
$ find / -name *.txt -exec ls -l {} \; 2>&1 1>result.txt
find: /some/directory: Permission denied
find: /another/one: Permission denied
$
Input redirection from here documents
Perhaps one of the most useful forms of redirection is redirecting input from a here document. A shell script can be written that executes a command and serves all input to the command. This is frequently used for a command that is normally run interactively. As an extreme example, I will show you how to do this with the editor vi. I am using vi for two reasons: first, it's interactive, and second, you're probably fairly familiar with it already and so will have a better understanding of what the script's doing. Normally, hands-off editing is done with the sed command.
First, create a text file with several hello strings in it, as in the following example, then name it hello.txt.
sample hello.txt
hello world
hello broadway
hello dolly
Create a file named here.sh that contains the lines in the example below. The second line starts the vi editor on the hello.txt file and the <<END-OF-INPUT option states that vi will run taking its input from this current file, here.sh, reading in a line at a time until a single line containing END-OF-INPUT is read in. The subsequent lines are vi commands to globally search for hello, replace each instance of it with bye, write the file back out, then quit. The next line is the END-OF-INPUT line and final echo statement to indicate that the editing is complete.
# here.sh - sample here document
vi hello.txt <<END-OF-INPUT
:g/hello/s//bye/g
:w
:q!
END-OF-INPUT
echo "Editing complete"
Change the mode on the file to make it executable:
$ chmod a+x here.sh
When you execute the here.sh script, you may receive a warning from vi that it's not running in interactive mode. Next, the actual editing takes place; afterwards, you can cat out the hello.txt file and see your handiwork.
$ ./here.sh
Vim: Warning: Input is not from a terminal
Editing complete
$ cat hello.txt
sample bye.txt
bye world
bye broadway
bye dolly
If you really want to suppress the vi warning, redirect the error to the /dev/null device, as in the following version of here.sh:
# here.sh - sample here document
vi hello.txt 2>/dev/null <<END-OF-INPUT
:g/hello/s//bye/g
:w
:q!
END-OF-INPUT
echo "Editing complete"
here documents frequently appear as small pieces of larger scripts. In order to make the here portion stand out, it's helpful to indent that section of the shell. Using a minus (-) in front of the end-of-input marker eats the white spaces at the beginning of a line and prevents them from being passed on to the program. The following is an example:
# here.sh - sample here document
vi hello.txt 2>/dev/null <<-STOP-HERE
:g/hello/s//bye/g
:w
:q!
STOP-HERE
echo "Editing complete"
Because it's an interactive program, the ftp utility is a common candidate for here document status. The following example starts ftp and redirects standard output and standard error to xfr.log. The process logs in to a remote system named nj_system, switches to binary transfer mode, creates two directories, transfers a file named newstuff.a to the remote system, and signs out again. Using a here document makes it possible to execute ftp through a shell script while seeing what the script is doing. The second example below is another method of doing this, but it involves a separate file with the ftp commands.
# xfr.sh - Transfers to a remote system
district=nj
ftplog=xfr.log
insbase=/usr/installations
insdir=$insbase/new
inskit=newstuff.a
echo "Transferring to" $district
ftp 1>>$ftplog 2>&1 $district"_system" <<-ALL-DONE
        user mo ddd789
        binary
        mkdir $insbase
        chmod 777 $insbase
        mkdir "$insdir"
        chmod 777 $insdir
        put $inskit $insdir/$inskit
        chmod 777 $insdir/$inskit
        bye
ALL-DONE
echo "Transfer to" $district "complete."
The first file would have to contain nothing but the commands for ftp, and couldn't take advantage of script variables. Here's a sample input for ftp:
user mo ddd789
binary
mkdir /usr/installations
chmod 777 /usr/installations
mkdir /usr/installations/new
chmod 777 /usr/installations/new
put newstuff.a /usr/installations/new/newstuff.a
chmod 777 /usr/installations/new /newstuff.a
bye

# xfr.sh - Transfers to a remote system
district=nj
ftplog=xfr.log
echo "Transferring to" $district
ftp 1>>$ftplog 2>&1 $district"_system" <ftp_commands
echo "Transfer to" $district "complete."
In our next installment, I'll cover Unix system and global variables. What are they and how do you use them? I have been meaning to do this one for a while, and now seems like a good time.

 

The language of shells


Making sense of shell commands

Summary
Working with shells can be difficult, as they require unusual and specific combinations of words and punctuation. This month, Mo Budlong helps you out by explaining some basic commands, such as ls, echo, and man. Also, Mo corrects a problem from May's Unix 101 in a sidebar. (1,300 words)

From the end user's perspective, the shell is the most important program on the Unix system because it is the user's interface to the Unix system kernel. The shell reads and interpreting strings of characters and words.
The shells operate in a simple loop:
  1. Accept a command
  2. Interpret the command
  3. Execute the command
  4. Wait for another command
The shell displays a prompt, notifying the user that it is ready to accept a command. It would be nice if you could speak or type instructions into the computer in some form of natural language.
OK, Hal. Sort out my correspondence, throw out anything
that is too old, and archive the rest.
Unfortunately, the shell recognizes a very limited set of command words, so the user must offer commands in a way that it understands. This means learning to string odd words and punctuation together.
Each shell command consists of a command name, followed, if desired, by command options and arguments. The command name, options, and arguments are separated by blank space.
The shell is one of many programs that the Unix kernel can run for you. When the kernel is running a program, that program is called a process. The kernel can run the same program many times (one shell for each user), and each running copy of the program is a separate process. Because each user runs a separate copy of the shell, each user is running in his or her own process space.
Many basic shell commands are subroutines that are built in to the shell program. The echo command is almost always built in to a shell.
$ echo "Hello, Hal"
Hello Hal
$
Commands not built in to the shell require that the kernel start another process in order to run.
When you execute a command that is not built in to a shell, the shell asks the kernel to create a new subprocess (or child process) to perform the command. The child process exists just long enough to execute the command. The shell waits for the child process to finish before accepting the next command.
The basic form of a Unix command is:
command name [-options] [arguments] 
The square brackets signify parts of the command that may be omitted.
The command name is the name of a built-in command or a separate program you want the shell to execute. The command options, usually indicated by a dash, allow you to alter the behavior of the command. The arguments are the names of files, directories, or programs that the command needs to access.
ls -l /home/mjb
The ls command is usually a separate program rather than a built-in command. The command above will get you a long listing of the contents of the /home/mjb directory. In this example, ls is the command name, -l is an option that tells ls to create a long, detailed output, and /home/mjb is an argument naming the directory that ls is to list.
The Unix shell is case sensitive, and most Unix commands are lower case.
Some of the more popular shells are sh (the Bourne shell), ksh (the Korn shell), csh (the C shell), bash, (the Bourne Again shell), pdksh (the Public Domain Korn shell), and tcsh (the Tiny C shell).
You can frequently identify your shell by typing:
echo $SHELL
Unix recognizes certain special characters as command directives. If you use a special character in a command, make sure you understand what it does. The special characters are / < > ! $ % ^ & * | { } ~ and ;. When naming files and directories on Unix, it is safest to only use numerals, upper and lower case letters, and the period, dash, and underscore characters.
A Unix command line is a sequence of characters in the syntax of the target shell language. Of the characters in a command line, some are known as metacharacters. Metacharacters have a special meaning to the shell. The metacharacters in the Korn shell are:
  • ; -- Separates multiple commands on a command line
  • & -- Causes the preceding command to execute asynchronously (as its own separate process so that the next one does not wait for it to complete)
  • () -- Enclose commands that are to be launched in a separate shell
  • | -- Pipes the output of the command to the left of the pipe to the input of the command on the right of the pipe
  • > -- Redirects output to a file or device
  • >> -- Redirects output to a file or device and appends to it instead of overwriting it
  • < -- Redirects input from a file or device
  • newline -- Ends a command or set of commands
  • space -- Separates command words
  • tab -- Separates command words
Some metacharacters can be used in combinations, such as ||, &&, and >>. With these metacharacters you can define a command-line word, which is a sequence of characters separated by one or more nonquoted metacharacters.
To access the online manuals, use the man command, followed by the name of the command you need help with. For instance, to see the manual for the ls command, enter:
man ls
End of article.
A note to my readers
 
I would like to note a correction to the May edition of Unix 101, in which I said:
"Once a shell variable has been exported and becomes an environment variable, it can be modified by a subshell. The modification affects the environment variable at all levels where the environment variable has scope."
Several sharp eyed readers picked up on this and sent comments ranging from, "Oh, no, you can't" to "Gee, whiz, which shell are you using? It doesn't work for me."
They are right. A subshell cannot modify an environment variable and return it to the parent. It can modify an environment variable and pass it on to a child process, but it cannot return the new value to a higher level. To illustrate this correctly, create the following three script files and grant them execute privileges using chmod a+x script*.

# script1
myvar="Hello" ; export myvar
echo "script1:myvar=" $myvar
./script2
echo "Back from script1 and script2
echo "script1:myvar=" $myvar

# script2
myvar="Goodbye"
echo "script2:myvar=" $myvar
./script3

# script3
echo "script3:myvar=" $myvar
If you run this sequence, the results show that $myvar exists in all three scripts (and, consequently, in all three processes), but modifying it in script2 only affects its value in script3.

$ ./script1
script1:myvar= Hello
script2:myvar= Goodbye
script3:myvar= Goodbye
Back from script 1 and 2
script1:myvar= Hello
$
My apologies to those of you who tried to make the example in the May issue work.

Tuesday, February 1, 2011

Mobile Telephone Number Codes for INDIA

If you get a missed call from unknown mobile no., find out from where you have been called with the table. provided in the following blog.

I found it extremely worth to link to that blog which provides this service ..


LINK :Mobile Telephone Number Codes

RUN MULTIPLE GTALK AT SAME INSTANCE

It is the simplest hack ever,for those who all thought to have multiple gtalk messengers running on DIFFERENT GMAIL ACCOUNTS at the same time.
Here the following steps r given:
1.Create a shortcut of Google Talk messenger on your desktop or any other location.
2.Right click on the Google Talk messenger icon and select properties option
3.Modify target location text by this
“c:\program files\google\google talk\googletalk.exe” /startmenu

to

“c:\program files\google\google talk\googletalk.exe” /nomutex

4.Click OK
Now you can run multiple google talk at same instance .

MULTIPLE YAHOO MESSENGER HACK

Here is the stp by step procedure for that..
1.Go to Start -> Run -> Type regedit -> hit enter

2.Go to HKEY_CURRENT_USER->> Software ->> Yahoo ->> pager ->>Test

3.Right click on test -> choose new Dword value .

4.Rename it as Plural.

5.Double click it -> assign a decimal value of 1.

6.Close registry -> Restart yahoo messenger.

NOW you can open yahoo messenger N number of times

youtube hack -"Tested" and updated

Its not working now

Download any Youtube Videos/Movie Directly from your Chrome or mozilla Browser. It will Instantly get the link from the webpage even if it's not yet finished loading. No Website or software is needed.

It's easy! While watching the video in Youtube. Just Copy and Paste the Code Below to your Address Bar and give a GO:

javascript:window.location.href = 'http://youtube.com/get_video?video_id=' + yt.getConfig('SWF_ARGS')['video_id'] + "&l=" + yt.getConfig('SWF_ARGS')['l'] + "&sk=" + yt.getConfig('SWF_ARGS')['sk'] + '&t=' + yt.getConfig('SWF_ARGS')['t'];

then just rename the file to "*.flv"


If you want to download the video in High Quality MP4 Format, Higher Quality, or High Definition (HQ) - really? :)

Just paste the code Below:


javascript:window.location.href = 'http://youtube.com/get_video?video_id=' + yt.getConfig('SWF_ARGS')['video_id'] + "&fmt=18&l=" + yt.getConfig('SWF_ARGS')['l'] + "&sk=" + yt.getConfig('SWF_ARGS')['sk'] + '&t=' + yt.getConfig('SWF_ARGS')['t'];


STEPS
1. go to youtube site
2.watch any video
3. A) while watching ,just paste the above code on to the address bar(circled red in image) and give a GO(NO NEED TO EDIT THE CODE GIVEN IN THE BOX JUST PASTE AS IT IS)
B) u can also give it in the NEXT TAB and give a GO
C) U can even book mark the code n click it when u browse with chrome



4. download will start automatically ( u can even switch on the tabs,after the download has began!)

Also as all know u can download youtube videos thro sites like VIDEODOWNLOADX.COM n many other softwares..but this is different without any site or software...
try out!