May 17, 2008

Creating path environment

Creating path environment

The $path works like a variable that can hold any information. In this case it will hold an exact route to get to a specific directory. When it is executed, the system will remember it. The system itself has all kind of variables many are defined in the shell's configuration files and are part of any users profile including root defined in /etc.

For example X has variables of its own set by the start-up files of the windows manager.

This is how it is used:

PATH=/usr/bin:/bin:/usr/local/bin:/usr/X11R6/bin

PATH=$PATH:/some/directory

When you define a local environment it is restricted to the terminal you defined it to; however you can export it to any terminal you like or make it global to all terminals.

export PATH=$PATH:/some/directory

Once you have exported to a directory; you can now set it permanently to your $PATH, just add the 'export' command line to your '.bash_profile'.

Writing and executing bash shell script

When the time comes and you have the need of writing automated tasks the answer is scripts. Under UNIX, scripts are executable batch files. The bash shell has the power of writing and executing large complicated programs. If you have created batch files under dos, then scripts will be very simple to understand.

Let's write a simple program so you can understand the basics.

  • Create a directory under your regular username, in your Documents folder
  • Call it myscript
  • Change to myscript

[user1@server2 Documents]$mkdir myscript
[user1@server2 Documents]$cd myscript
[user1@server2 myscript]$

At this point I am going to edit a file named hello, using my favorite editor… Linux comes with many editors including vim, emacs. You can use any editor that you wish. Just save it as a plain text when you finish (files have no extensions under Unix). Under Linux, extensions are not used; instead, there is a reference bit to each file that accomplishes the task. Following this rule you can name the file whatever you want. If you choose an editor and it is not installed, don't worry, it is very easy to install.

If you need any application that runs under Linux (before you waste your time looking it up on the Internet), I suggest you first look in the installation CDs. If you don't know how to install it, jump to the package manager (software installation in chapter 4)

Start the editor…

[user1@server2 myscript]$vi hello

Press the insert key

Start typing the following:

#! /bin/bash
                echo “\“Hello I am a little program\””
                echo “I am in the directory:”
                echo
                pwd
                echo
                echo 'do an ls –l'
                ls –l
                echo
                echo

When finished typing, press the Esc key

Use the shift key to type the colon (:)

Press the w & q keys (write and quit)

Your last line should look:

:wq

Press enter

You should be back to your prompt

[user1@server2 myscript]$

Type ls –l hello file should be in your myscript directory

Programs are normally run by typing their names some may require special options.
Try:
[user1@server2 myscript]$hello
-bash: hello: command not found
[user1@server2 myscript]$

The bash shell responded that it couldn't find the file hello. I then specify bash to look in the current directory:

[user1@server2 myscript]$./hello -bash: ./hello: Permission denied
[user1@server2 myscript]$

Now it found it, but it does not have permission to execute it. So, in order to make the hello file an executable, I will have to give it the executable permission.

[user1@server2 myscript]$ chmod 745 hello

  • Read by owner
  • Write by owner
  • Execute/search by owner
  • Read by group
  • Read by others
  • Execute/search by others

If you execute ls –l, you should see the hello file turned green.

Try to run it now:

[user1@server2 myscript]$ ./hello
“Hello I am a little program”
I am in the directory:

/home/user1/Documents/myscript

do ls –l

Total 4
-rwxr—r-x 1 user1 user1 141 Aug 19 00:53 hello

[user1@server2 myscript]$
Sometimes it is a good idea to automate tasks, scripts becomes handy once you get to know the system. I suggest you get a book focused on shell programming.

No comments:

Post a Comment

Popular Posts