Monday, July 2, 2012

Understanding UNIX Shell Part - 3


COMMAND GROUPING

We can join commands on a command line in such a way that the second command executes only if the first command has executed successfully.
For eg, we can use a first command to check whether a file exists and a second command to perform an operation on it if it exists.

NOTE:
1.    To make one command conditional on another, we can join the commands using a double ampersand (&&). The command after the && symbol executes only if the command before the && symbols produces a zero exit status – in other words, if its executes successfully.

For eg:
$ ls userlist && sort userlist
In this eg, the ‘ls’ command checks whether the ‘userlist’ file exists. Because it does exist, the ‘ls’ command executes without errors – so its exist state is zero.  This causes the ‘sort’ command to execute.


Also, if we delete the ‘userlist’ file and run the command again, the ‘ls’ command encounters an error – so its exit state is non-zero.  Because the ‘sort’ command is conditional, the shell doesn’t attempt to execute it.


2.    We can use double pipe (||) to make a command conditional on the unsuccessful execution of the previous command.
In such case, the second command executes only if the first command has non-zero exit state.

For eg:
$ ls userlist || touch userlist
In this example, the ‘ls’ command looks for a file called ‘userlist’. If it fails to find the file, the ‘touch’ command creates it.

Also, if the ‘ls’ command executes successfully, this means that the file already exists. In this case, the ‘touch’ command doesn’t execute.


3.    Can group commands using braces ({}). The shell treats any command block enclosed in braces as if it were a single command.
This allows us to redirect input and output to and from a group of commands.

For eg:
$ {sort | grep ‘che’}
In this example, the braces group the ‘sort’ and ‘grep’ commands into a code block so that the shell sorts input and then extracts any lines containing the word ‘che’.

4.    Can redirect input and output to a command block as if it were a single command.
For eg:
$ {sort | grep ‘Mexico’} mex_flights
In this example, the code specifies
 Flight ---> file as input
Mex_flights ---> file as output


5.    Can group commands using round brackets – Often called parentheses – instead of braces.  This causes the shell to spawn a subshell and execute the command block in the subshell.


6.    Commands that execute in a subshell do not affect what’s happening in the main shell.
This allows you to define variables that exist only for the lifetime of the subshell, and to change the working directory within the subshell without affecting the parent shell.

For eg:
$ (sort | grep ‘Mexico’) < massfile > mex_info


Exercise:
Want to create a file named hostname and containing the text server1.example.com. However, you don’t want to overwrite any existing file by that name.

Ans: $ cat hostname || echo server1.example.com > hostname

Justification: The use of the || ensures that the code that writes the output from the echo command to the hostname file will only execute if the attempt to list the hostname file fails.

Sunday, June 24, 2012

Understanding UNIX Shell Scripts Part -2


Expansion and substitution takes place in the following sequence:

1.    Brace expansion
2.    Tilde expansion
3.    Parameter expansion
4.    Command expansion
5.    Arithmetic substitution
6.    Word splitting
7.    Pathname substitution

Brace expansion:
In brace expansion, the shell looks for braces ({}) – also called curly brackets – in the token. If braces are present, it expands their contents.

For Eg:
The token b{all, ook} expands into
 ball book


Tilde expansion:
The shell looks for tildes (~) in the token. If a tilde is present, it replaces the tilde with the location of the current user’s home directory.

For Eg:
Depending on the system configuration, the token ~che/file2 might expand into
/export/home/che/file2.
Parameter substitution:
In parameter substitution, the shell checks whether the token is variable name preceded by a dollar sign ($). If it is, the shell replaces the token with the current value of corresponding variable.

For Eg:
If the value of the SHELL parameter is /bin/bash, the token $SHELL is replaced with /bin/bash.


Command substitution:
In command substitution, the shell checks whether the token is a command enclosed in brackets and preceded by a dollar sign ($). If it is, the shell processes the command and replaces the token with the command’s output.

For Eg:
The token $(type username) might be replaced with che.


Arithmetic substitution:
In arithmetic substitution, the shell checks whether the token is an arithmetic expression enclosed in double brackets and preceded by a dollar sign.  If it is, the shell evaluates the expression and replaces the token with the result.

For Eg:
The replaces the token $(72/9) with 8.

Word splitting:
In word splitting, the shell examines those parts of the command line that have resulted from previous stages of expansion and substitution.  If any of these contain spaces or special characters, it splits them into tokens for processing.

Pathname substitution:
In pathname substitution, the shells look for wildcard characters in the token. If it finds asterisks (*), question marks (?), or double slashes(//), it searches the current directory for filenames that match these wildcards and substitutes them for the token.

For Eg:
Depending on the files in the current directory, the token f*.txt might expand into fat.txt, fund.txt, fight.txt.

Wednesday, May 30, 2012

Understanding UNIX Shell Scripts


Special characters that can be used to manipulate commands in the command line include

1.    Backslash (\)
2.    Greater than (>)
3.    Less than (<)
4.    Pipe (|)
5.    Ampersand (&)


Backslash:
Character prevents the shell from treating another character as a special character through a process called backslash escaping.
This allows us to split a command statement across multiple lines. When we place the backslash at the end of a line and then press Enter, we can continue the statement on the next line. The backslash prevents the shell from treating the Enter keystroke – or new line character – as a special character.

For Example:
$echo Long pieces of test may not always fit onto to a single \
Line of the command line interface, so it becomes \
Necessary to split them across multiple lines using \
Backslashes.



Greater than (>):
Allows us to direct the standard output of a command to a file or a device such as a printer instead of to the terminal screen.

For Eample:
$ ls –l /usr/home > userdirs



Less than (<):
Allows us to send the contents of a file to a command as its standard output.

For Eg:
$ sort –d < list




Pipe line (|):
Direct the output of one command to the input of another command.

For Example:
$ cat good | grep ‘India’



Ampersand(&):
Character at the end of a command statement allows us to run commands in the background.

For Eg:
$ find / -name che &
[1]  48748
$ che




NOTE:
If we want to use special character in command line text without the shell recognizing them as special characters, we have to enclose them with a backslash (\).

For Example:
$echo Tours \& Accommodation
Tours & Accommodation

This eg shows an ‘echo’ command in which the ‘echo’ text contains an ampersand. There’s backslash
In front of the ampersand, which prevents the shell from treating it as a special character.

Sunday, May 27, 2012

What is …..? Part : 1




What is Mile Stone?
A mile stone is a special type of service that is made up of a defined set of other services.
A mile stone can be regarded as a system state to be reached.  This system state requires a defined set of services to be running.  These services depend on other services being available.  Therefore, there is a hierarchy of dependency relationships.  This is one of the core features managed by SMF.
  

What is Min Free Space?
File system minfree space:
1. The portion of a file system that is reserved and held back from users.
2. On large file systems, the minfree is automatically determined so that disk space is not wasted.
3. A thumb rule is to use default 10% minfree value for file systems upto 1GB and then adjust the minfree value so that your minfree area is no larger than 100MB.
4. Note: As for performance, applications do not complain about the lower minfree value.
5. Purpose:
When the file system is full, the free space can only be accessed by the root user. It can act as an emergency overflow.

To view the minfree value of a slice:
# fstyp -v /dev/dsk/c0t12d0s0 | grep -i min
minfree 10% maxbpg 2048 optim time


To change the minfree value:
# tunefs -m 1 /dev/dsk/c0t12d0s0
minimum percentage of free space changes from 10% to 1%
In the same way, the minfree space can be increased.

 
Note:  By default, the newfs utility calculates the minimum free space based on volume size (64Mbytes /volume size * 100), rounded down to the nearest integer. The default value is limited to between 1 percent and 10 percent.


Journaling:
The recording of Unix File System (UFS) updates in a log before the updates are applied to Unix File System.  This allows for increased data recovery in the event of a catastrophic system failure.  Also called logging.

What is logging?
Logging is a process of storing transactions (changes that make up a complete UFS operation) in a log before the transactions are applied to the file system.  After a transaction is stored, it can be applied to the file system.  This process prevents file system from becoming inconsistent, therefore eliminating the need to run the fsck command.  Because the fsck command can be bypassed, logging reduces the time required to reboot a system if it crashed or after an unclean halt. 

The default behavior prior to Solaris 10 OS is nologging. But remember, UFS logging is enabled by default for all file systems in the Solaris 10 OS. The default behavior can be overwritten by specifying the nologging option on the file system mount.

The log is allocated from free blocks in the file system, and it is sized at approximately 1Mbytem per 1 Gbyte of file system space, up to a maximum of 64Mbytes.  Logging can be enabled on any UFS file system, including root (/). 
The log created by UFS logging continually flushes as it fills up. 
The log is totally flushed when the file system is unmounted or when the lockfs –f command is used.



Private Region:
Private region is used for configuration information. Adding the disk to disk group, the media name, disk access name, the disk name and the disk configurations all are written to the private region.

Starting with VxVM 5.0 , the default size of the private region was increased to 32 Mbytes, which is large enough to store the records of several thousand VxVM objects.

Will be created at the time of initialization of disk to VxVM control.


Holds current host ownership of the disk drive.


Private Region Configuration Database:

Is similar to metadb or replica or state database in Solaris Volume Manager Software.

Contains information about the configuration of a particular disk group.  By default, VxVM keeps 4 copies of the configuration database per disk group to avoid any possibility of losing the disk group information.  Each copy of the configuration database contains the following information –

 dgname – Disk group name is assigned by the administrator.

dgid – Disk group ID is a 64-byte universally unique identifier that is assigned by VxVM to a disk group when the disk group is created.  This identifier is in addition to the disk group name

Records – VxVM virtual structure information for the disk group.


Kernel Log:
The kernel log is kept in the private region on the disk drive and is written by VxVM kernel. The kernel log contains –
Records describing certain types of actions, such as transaction commits, plex detaches resulting from I/O failures, dirty-region log failures, first write to a volume, and volume close information.

The kernel log is used after a crash or clean reboot to recover the state of the disk group just prior to the crash or reboot.


In nut shell, Private Region contains the following –
1.    Current host ownership: host id/host name.
2.    Disk identifier
3.    Configuration database
4.    Kernel log