Entries Comments



Category: misc

misc

Command Substitution

30 November, 2007 (04:32) | misc, UNIX | By: passion@linux

Normally, when you run a command from a script, the output from the command goes to the standard output: usually the user’s terminal. However, you may want to do further work with the output from the command, or use it to make decisions within the script.

Lets take a simple example. Suppose that you want to display the message, “Your current working directory is “ followed by the working directory. This can be done by inserting the output from the pwd command into the echo command, using a technique known as command substitution. There are two ways of doing this. The first is to enclose the name of the command in back quotes thus: `pwd`. The second is to enclose it in brackets and prefix it with the $ sign as follows: $(pwd).

So the above example could be entered into a script as follows:

echo Your current working directory is $(pwd)

This facility can be very useful in installation scripts, which may need to install different resources depending on system information such as the type of processor or operating system.

The uname command displays information about the current operating environment. Various switches can be used with it:

-a  Display all information
-s  Display the name of the operating system
-p  Display the type of processor
-r  Display the release number of the operating system.

The following is part of a script that uses the name of the operating system to decide which files to install.


ins_opsys=$(uname –s)
if [ $ins_opsys = SunOS ]
then
tar xvf sunfiles.tar
elif [ $ins_opsys = UnixWare ]
then
tar xvf unixwarefiles.tar
elif [ $ins_opsys = AIX ]
then
tar xvf aixfiles.tar
else
echo Operating system $ins_opsys not yet supported
fi

Command substitution becomes even more useful when combined with techniques covered in the next few articles. We will learn how to split the output from the command so that we can use selected parts of it, or reformat it. We will also learn how to deal with multi-line output.

Metacharacters

30 November, 2007 (04:22) | misc, UNIX | By: passion@linux

Certain characters have special meaning to the shell. These are known as metacharacters. They are listed below.

Pattern Matching:

This set of metacharacters is used in pattern matching, also known as wild cards.

*    Match zero or more characters
?    match exactly one character
[]   Match a list or range of characters eg [AB], [A-N]
[!]  Match anything except the list or range of characters e.g. [!AB]

Redirection:

>    Redirect away from the standard output file
>>   Redirect away from the standard output and append to the target file
2>   Redirect away from the standard error file
<    redirect away from the standard input file
<<   Start of a Here document
|    Pipe the output of one command to the input of a second command

Miscellaneous

&    Run the command in the background
()   Execute command(s) in sub-shell
$    Substitute the value of a variable or argument
#    ignore text to end of line
;    New line
:    Null command

Literal

\    take the literal of the following character – this is used to include an actual metacharacter in a string, rather than have the shell try to interpret it e.g.\$
‘’   Interpret all characters enclosed in single quotes as a literal
“”   Interpret all characters enclosed in double quotes as a literal, except the dollar sign.

Literals are important, since without them, the shell will first interpret any metacharacters before working with the string. For example, the following command would produce an error:

echo Enter your name>

since the > would be interpreted as a redirection symbol. This can be avoided by placing the text in quotes:

echo ‘Enter your name>’

Further uses of the Find command

8 November, 2007 (12:42) | misc, UNIX | By: passion@linux

In another article, we briefly looked at the find command, which searches for specified files. We will now look at more uses of this very powerful command.

It can be used to search for files using various criteria, including name, owner and time last changed. Additionally, it can execute a command or script for each of the matching files. It could therefore be used for many purposes, for example, to remove files not accessed since a given date, to recompile all programs modified since the time of the last compile, or to backup all the files belonging to a specific user.

The syntax of the command is:

find
where is the pathname to the directory where the search is to begin and
and specify the search criteria and any actions to be taken, as detailed below:

-name Find files whose name matches the given pattern. The pattern should be in quotes.
-print Display the path of matching files
-user Find files belonging to a specific user
-exec command {} \; Execute Unix/Linux command for each matching file.
-atime (+t,-t,t) Find files accessed more that +t days ago, less than -t or precisely t days ago.
-ctime (+t,-t,t) Find files changed more that +t days ago, less than -t or precisely t days ago.

-perm Find files with given permissions.
-type Locate files of a given type:
c=character device b=blocked device d=directory
p=pipe s=socket f=normal file l=symbolic link
-size nx Find files with size larger than n where x can be b=bytes;
c=characters; k=kilobytes w=2 byte word:

Examples:

find . –name myfile
Search for myfile under the current directory

find /usr/sam/cprogs –ctime –1 –exec cc {} \;

Compile all programs that have changed less than one day ago.

The logrotate utilility

6 November, 2007 (13:57) | misc, UNIX | By: passion@linux

Under Unix, log files are generated by both the system and by various application packages such as database and web servers. These log files can grow so large over time that they become difficult to work with, and also use a lot of system resources. The logrotate utility can help to keep these files more manageable. It renames the log file after a specified period, and creates an empty one. The renamed files can be either compressed, backed up or deleted. It also allows the administrator to limit the number and size of the log files. This program is usually run daily as a cron job.

A configuration file /etc/logrotate.conf allows the administrator to configure basic settings for logrotate. Further configuration settings for each individual log are then placed in the directory /etc/logrotate.d, and given the name of the process.

An example of a typical configuration script /etc/logrotate.d/myprog is shown below:

/var/log/myprog.log {
rotate 7
daily
errors root@localhost
missingok
postrotate
/ etc/init.d/myprog restart >dev/null
endscript
}

Notes:
/var/log/myprog.log - the name of the log file to be rotated
rotate 7 - causes it to keep 7 sets of rotation logs
daily - indicates that logs are to be rotated daily
errors root@localhost - causes errors to be mailed to root on the local machine
postrotate - contains a command to stop and restart the relevant service

This should be used in conjunction with an archiving strategy that backs up the log files to tape on a regular basis. In the above example, a weekly archive would be adequate, since we are keeping seven sets of log files on disk.

Obtaining Information about your System

2 November, 2007 (11:20) | misc, UNIX | By: passion@linux

When reporting a fault to your operating system support center, you will often be asked for information about your system which you may not have readily to hand. It is therefore useful to know where to find the kind of information that they require. In this article we will look at a few commands that give information about the current system.

The hostname command can be used to display the hostname, domain name or IP address of the local computer. Used with no switches, it displays the hostname only. Additional information can be obtained using the following switches:

-d Display the name of the domain to which the machine belongs e.g. mydomain.com
-f Display the fully qualified host and domain name – admin.mydomain.com
-i Display the IP address for the current machine

The uname command displays information about the current operating environment. Switches include:

-a Display all information
-s Display the name of the operating system
-p Display the type of processor
-r Display the release number of the operating system

The uptime command displays how long the machine has been running since the last reboot.

The lsmod command displays a list of all kernel modules that are currently loaded.

The support center may also want to know if there were any abnormal messages when the system was booted. Since these messages flash past rather quickly on the screen, and you may not even have been present at the last reboot, it is useful to be able to access these messages. The dmesg command redisplays the messages from the last boot. There is also a file /boot.log that contains a log of the initiation processes.