Wednesday, May 29, 2013

linux article 2 - for loop, if, vi editor, $?

From now onwards, i decided to save all my important documents esp educational material in dropbox, since it is available in all platforms and provides excellent usability.

So for the next part

more commands for vi editor

there are two modes for this editor

command mode  - press 'x' to delete the character under the cursor
and to cut or copy text, press 'v' and move the cursor and to select the text, then press 'y' to copy the selected text or press 'x' to cut it. Moved the cursor to the desired position and then press 'p' to paste the selected text.
insert mode  -  press 'i' to enter the insert mode at point
                       press  'o' to open a new line below the current line and enter the insert mode

Great cmd

in command mode press 'u' to undo the change of previous command
                                Press 'U' to undo the changes in the current line

I am studying about how to write shell scripts.
So when you type a cmd or script, we give arguments or parameters to them, they are saved automatically under these variables , $0 has the command, $1 stores the argument 1, and so on

and with echo cmd , the backslash(\) is treated normally by default just as any other character
but if we use "echo -e", then backslash interpretation of escapes is enabled.

$? stores the exit status of last command used. 0 means cmd executed normally.
we can print it on screen by using : echo $?
a great use of this mentioned in this article, it has a program which checks whether the user is registered user in the system or not.
this is the link to the article
http://www.cyberciti.biz/faq/shell-how-to-determine-the-exit-status-of-linux-and-unix-command/

so with my voyage to the kernel, from now.

we can send all the useless output by sending it to /dev/null, instead of default terminal
so we can : echo "who" > /dev/null
where nothing is printed on screen

the main thing to remember with if or while, if the cmd in the 'if' condition is executed , then the block of if is executed, if cmd execution failed, the block code in 'if' statement is not executed as well.
example:

if date
then
....
fi

this is executed

there should always be some kind of cmd or script in the condition statement.

if n
then
...
fi

this is not executed

Remember , it doesn't check the output status of the cmd, because the output status of any succesful cmd is 0, which is essentially false for other languages. so output status of cmd doesn't matter, only the succesful execution of cmd, even with 'expr 1 = 1', here output put status is 0, but the output is 1. so it is  executed.

the only exception is when we directly evaluate expression by using this

if [ ] : we use these boxes to evaluate expressions
example:

if [ var1 -eq var2 ] then

fi



For loop can be done, just like C

structure:

for  (( i = 0 ; i < 2 ; i++))
do
...
done

to print stuff in the same line as before , use : echo -n

Looks like the voyage is done
goodbye





No comments:

Post a Comment