Tuesday, July 03, 2007

Whats your background?

As part of job control its possible to stop a process and continue it later.
So while you are running a program, you can type Ctrl-Z to suspend it , its like a pause in a media player

Then you can ask bash to continue the process in background by typing 'bg'.
Background processes can not read from terminal and if they try to they will be suspended(paused).

Here is quick test for this:

bash$ cat read.sh
#!/bin/bash

sleep 5
echo -n "Enter some text > "
read text
echo "You entered: $text"

Run the script and hit Ctrl-Z

bash$ ./read.sh

[1]+ Stopped ./read.sh

Now run this in background using bg command.

You are prompted to enter some text

bash$ bg
[1]+ ./read.sh &
bash$ Enter some text >

As soon as you enter some text and hit Enter you will see this

bash$ Enter some text >

[1]+ Stopped ./read.sh

Any more attempts to run this in backgroup will suspend the script.
Run fg to get the script to run in foreground to get out of this loop.

bash$ bg
[1]+ ./read.sh &

[1]+ Stopped ./read.sh
bash$ bg
[1]+ ./read.sh &

[1]+ Stopped ./read.sh
bash$ bg
[1]+ ./read.sh &

[1]+ Stopped ./read.sh
bash$ fg
./read.sh
hi there
You entered: hi there

Wednesday, December 27, 2006

Reviewing Sed - Part 1

There is a good collection of sed one liners at http://www.student.northpark.edu/pemente/sed/sed1line.txt.

So I have just begun to review few them using sed manpage because of the earthquake in Taiwan.

# Following line will insert a blank space after everyline in temp.txt

sed G temp.txt

G - Append hold space to pattern space.

So hold space holds nothing initially and if you append that to pattern space, you get a new line.

So thats the way to add blank lines after everyline.
Sed goes through each line and loads it to the pattern space.

# Adds only a single blank line to all lines to temp.txt

sed '/^$/d;G' temp.txt

/regexp/ - Match lines matching the regular expression regexp.

d - Delete pattern space. Start next cycle.

There are two things to consider.
For blank lines : 'd' is executed that deletes the pattern space and starts next cycle(loads next line and starts all over again)

For non blank lines : as the pattern doesn't match, d is not executed and next command G gets executed.
We get a blank line after every non blank line.

The command would delete the blank lines and skip the G command for blank lines.
Thing to note is that d command applies only to lines matching the regex /^$/ whereas
G command applies to all lines and is not tied to the regex. However when regex is matched the d command will cause G command to be skipped.

Wednesday, December 13, 2006

Sed and No DuPlication

I needed to remove duplicate names from a file so I thought sed should be a good choice for it.
uniq is too easy so I found out how sed does it. Herez how

$ sed '$!N; /^\(.*\)\n\1$/!P; D' filename

So now time for an explanation.

1 $!N - sed reads one line at a time and then works on it. It doesn't read in the newline at the end of the line into pattern space (what sed works on). So N command appends the newline and the next line to the pattern space.
$ denotes the last line and ! means NOT, so it means for the last line don't execute N command.Heck as if there is anything to read after the last line..read it if you can for all I care.

2. /^\(.*\)\n\1$/!P; - If you see start of pattern space and then anything followed by a newline which is followed by exactly that anything. Don't "print the first part of pattern space till the newline"(P) else print first part of pattern space till the newline

3. D - "just delete the first part of pattern space till the newline and restart the command cycle i.e go back to N"

Friday, December 08, 2006

Its time for tee

I was asked this question and suddenly i realized that bash doesnt have redirection like operator to do this. Do what? Print Standard Output/Error as it is and also redirect it to a file. So therefore its time for tee.


$ls i_dont_exist 2>&1 | tee output.txt
ls: i_dont_exist: No such file or directory
$cat output.txt
ls: i_dont_exist: No such file or directory

Thursday, October 19, 2006

Colorful Bash

Well here is how(er..i mean how not to :)) to make your terminal a bit more colorful

[kalyan@duck snippets]$PROMPT_COMMAND='tput setf $(( RANDOM % 7 + 1 ))'

And so we get a colorful terminal

Friday, July 14, 2006

Bash Meditation

Here is the oneliner for meditation in bash

while :; do whoami; done

Monday, May 15, 2006

First post using Flock

Let me see :)