Monday, May 16, 2011

exporting variable in Shell

You can create your own environmental variables in shell

It is very simple just type export followed by your variable and the value.

For exporting a variable gg

export gg="hello"

echo $gg
you will be able to see the content of the variable gg - "hello"

But it stays untill reboot.Once you reboot you have to set the variable again.

To make it permanent you can add it to /etc/profile to make the change visible for all users using your system or you can add the change to the ~/.bash_rc to make the change visible only for your shell.

Changing the prompt in Shell

For changing the prompt in shell

we need to export the PS1 variable

Its by default set to \$ and # for root

To change it  simply follow the below step:

you can change to many special characters.but commonly used are:

\$     -  shows the user prompt($) or root prompt(#) ,depending on which user you are.

\W   -  shows the current working directory base name.if the current working directory is "/home/karthik/perl" then it simply appears as "perl"

\w   -  Displays the full path of the current working directory.

\t    -   prints the current time in hours.minutes and seconds(for ex:9:30:40)

\s   - prints the current shell name.For bash shell it simply prints bash as prompt.

\u - in this case it prints the current username.

These options can also be used together.

For ex:
export PS1="\t \w \$"
makes the prompt as
[18:30:40 /home/karthik]$>

export PS1="\W"
makes the prompt as  if the full path is "/home/karthik/perl"

Files for configuring your shell

Sets up user information for every user.
It is executed when you first log in.
 It sets up the path as well as the environmental variables,the location of your mailbox and size                                                                            of your history files.
Finally,files under the /etc/profile.d are executed.

 Runs for every user who runs the bash shell.
 It sets a default prompt and may add one or more aliases.
But this file can be overridden by information in ~/.bashrc file.

It is run when the particular user logs in.
Used by the user to enter information that is specific to  his or her own use of the shell.
This file inturn calls the user's .bashrc file

Contains the information specific to the particular user.
This is the best location to add environmenta  variables and your aliases so that shell      
 will pick up when you log in.
Executes when you log out of your bash shell.
 By default just simply clears the screen.(this is also for a particular user)

using chmod command (or) Setting File permissions in Linux,Shell or Unix

chmod -is used to change the permissions for a file.
when you type the name of the file

ls -ld  - you will be able to see the permissions provided for the file

there are three groups to which permissions can be provided:
o - others
g - user group
u -users

to set the permissions for the file use:
chmod command followed by the fiename.

there are three permissions namely read,write,execute which can be given to the users,user group and others

For providing permission to a file we must be owner of the file or root
read permssion -4
write permission-2
execute permission-1

to give all permissions to a particular group such as user
provide 7=4+2+1 and no permission to usergroup and others
chmod 700 filename
ls -ld
-rwx------ 1 chris sales 4983 jan 23 22:13 ch3
 ---   ---
 user  others
1nd)r - read
2nd)w - write
3nd)x - execute

Other way to do that:
a - all users
g - owner group
u - owner user
o - others

for permissions use:

yu can either add or subtract the permissions:

assume the permission for the file to be rwxrwxrwx
permission change when each command is executed:
chmod a-w file     r-xr-xr-x
chmod o-x file     rwxrwxrw-
chmod go-rwx file  rwx------

assume the file to have a permission of ---------
chmod u+rw files   rw-------
chmod a+x files    --x--x--x
chmod ug+rw files  rw-rw----

to change the permissions for the entire directory we have to -R opton to recuresively change the permission for the entire directory

chmod -R 777 /tmp/hello/
this command willchange the perission for the entire hello directory to 777

visual mode in vi

These commands can be used only after you enable visual mode in vim editor

Shift+V - to enable the visual mode in vim

Then select the lines in the vi

Alt+> - Shift right the selected lines

Alt+< - Shift left the selected lines

c - change the selected lines

y - copy or yank the selected lines to the clipboard.

p - paste the copied lines from the clipboard to the Editor.

getenv in C

getenv in C

this is to get the environmental variable's value in Shell from C file

to store it to a variable use:


The following program will explain it more clearly

This program demonstrates how to get a value for environmental value in shell
and compare it with a predefined variable.

1 #include
2 #include
3 #include
4 int main()
5 {
6 char * cc;
7 cc = getenv("HOME");
8 printf("--->%s<--",cc);
9 if ( strcmp(cc,"/home/karthik") == 0 )
10 {
11 printf("yes the home is /home/karthik");
12 }
13 return 0;
15 }

Editing in vi

Many keywords ease the use of vi

Some of them are:

Ctrl+F - to move forward one page at a time

Ctrl+B - to move backward one page at a time

Ctrl+D - to move half page forward at a time

Ctrl+U - to move half page backward at a time

Ctrl+G - name of the file you are editting

G -last line of the file

1G - move to the first line of the file
this any line no you want to move to

H - want the cursor to move to the upper left corner of the screen

M - want to move the cursor to the middle left corner of the screen

L - want to move the cursor to the lower left corner of the screen

Ctrl+R - to Redo the changes in the file

Ctrl+U - to Undo the changes in the file

dw - delete the word

d$ - delete starting from where the cursor is to the end of the line

d0 - delete starting from where the cursor is to the beginning of the line

Editing using numbers:

5cl - delete the 5 characters following the cursor and make it in insert mode

12j - move the cursor down 5 lines

3dw - delete 3 words

substitution in vi:

:g/Local - find the occurence of Local globally and print the instances

:s/Local/Remote - replace the occurence in the current line

:g/Local/s//Remote - for replacing the first occurence

:g/Local/s//Remote/g - for replacing the entire occurence of Local

:g/Local/s//Remote/gp - for replacing and printing the changed occurence