Saturday, 19 March 2016

using aliases in Linux shell commands

using aliases in Linux shell commands



An alias is a (usually short) name that the shell translates into another (usually longer) name or command. Aliases allow you to define new commands by substituting a string for the first token of a simple command. They are typically placed in the ~/.bashrc (bash) or ~/.tcshrc (tcsh) startup files so that they are available to interactive subshells.
Under bash the syntax of the alias builtin is
alias [name[=value]]
Under tcsh the syntax is
alias [name[ value]]
In the bash syntax no spaces are permitted around the equal sign. If valuecontains spaces or tabs, you must enclose value within quotation marks. Unlike aliases under tcsh, a bash alias does not accept an argument from the command line in value. Use a bash function when you need to use an argument.
An alias does not replace itself, which avoids the possibility of infinite recursion in handling an alias such as the following:
alias ls='ls -F'
You can nest aliases. Aliases are disabled for noninteractive shells (that is, shell scripts). Use the unalias builtin to remove an alias. When you give an aliasbuiltin command without any arguments, the shell displays a list of all defined aliases:
$ alias
  alias ll='ls -l'
  alias l='ls -ltr'
  alias ls='ls -F'
  alias zap='rm -i'
To view the alias for a particular name, enter the command alias followed by the name of the alias. Most Linux distributions define at least some aliases. Enter analias command to see which aliases are in effect. You can delete the aliases you do not want from the appropriate startup file.
Single versus double quotation marks in aliases
The choice of single or double quotation marks is significant in the alias syntax when the alias includes variables. If you enclose value within double quotation marks, any variables that appear in value are expanded when the alias is created. If you enclose value within single quotation marks, variables are not expanded until the alias is used. The following example illustrates the difference.
The PWD keyword variable holds the pathname of the working directory. Max creates two aliases while he is working in his home directory. Because he uses double quotation marks when he creates the dirA alias, the shell substitutes the value of the working directory when he creates this alias. The alias dirAcommand displays the dirA alias and shows that the substitution has already taken place:
 $ echo $PWD
  /home/max
  $ alias dirA="echo Working directory is $PWD"
  $ alias dirA
  alias dirA='echo Working directory is /home/max'
 
WHAT READERS LIKE
·         
·         
·         
When Max creates the dirB alias, he uses single quotation marks, which prevent the shell from expanding the $PWDvariable. The alias dirB command shows that the dirBalias still holds the unexpanded $PWD variable:
 $  alias dirB='echo Working directory is $PWD'
  $  alias dirB
  alias dirB='echo Working directory is $PWD'
After creating the dirA and dirB aliases, Max uses cd to make cars his working directory and gives each of the aliases as a command. The alias he created using double quotation marks displays the name of the directory he created the alias in as the working directory (which is wrong). In contrast, the dirB alias displays the proper name of the working directory:
 $ cd cars
  $ dirA
  Working directory is /home/max
  $ dirB
  Working directory is /home/max/cars
TIP: How to prevent the shell from invoking an alias
The shell checks only simple, unquoted commands to see if they are aliases. Commands given as relative or absolute pathnames and quoted commands are not checked. When you want to give a command that has an alias but do not want to use the alias, precede the command with a backslash, specify the command's absolute pathname, or give the command as ./command.
Examples of aliases
The following alias allows you to type r to repeat the previous command or r abc to repeat the last command line that began with abc:
·        VIDEO/WEBCAST
Sponsored
·         
Go
Bottom of Form
$ alias r='fc -s'
If you use the command ls -ltr frequently, you can create an alias that substitutes ls -ltr when you give the command l:
$ alias l='ls -ltr'
  $ l
  -rw-r-----. 1 max pubs  3089 02-11 16:24 XTerm.ad
  -rw-r--r--. 1 max pubs 30015 03-01 14:24 flute.ps
  -rw-r--r--. 1 max pubs   641 04-01 08:12 fixtax.icn
  -rw-r--r--. 1 max pubs   484 04-09 08:14 maptax.icn
  drwxrwxr-x. 2 max pubs  1024 08-09 17:41 Tiger
  drwxrwxr-x. 2 max pubs  1024 09-10 11:32 testdir
  -rwxr-xr-x. 1 max pubs   485 09-21 08:03 floor
  drwxrwxr-x. 2 max pubs  1024 09-27 20:19 Test_Emacs
Another common use of aliases is to protect yourself from mistakes. The following example substitutes the interactive version of the rm utility when you enter the command zap:
$ alias zap='rm -i'
  $ zap f*
  rm: remove 'fixtax.icn'? n
  rm: remove 'flute.ps'? n
  rm: remove 'floor'? n
The -i option causes rm to ask you to verify each file that would be deleted, thereby helping you avoid deleting the wrong file. You can also alias rm with the rm -i command: alias rm='rm ‒i'.
The aliases in the next example cause the shell to substitute ls -l each time you give an ll command and ls ‒F each time you use ls.The -F option causesls to print a slash (/) at the end of directory names and an asterisk (*) at the end of the names of executable files.
$ alias ls='ls -F'
  $ alias ll='ls -l'
  $ ll
  drwxrwxr-x. 2 max pubs  1024 09-27 20:19 Test_Emacs/
  drwxrwxr-x. 2 max pubs  1024 08-09 17:41 Tiger/
  -rw-r-----. 1 max pubs  3089 02-11 16:24 XTerm.ad
  -rw-r--r--. 1 max pubs   641 04-01 08:12 fixtax.icn
  -rw-r--r--. 1 max pubs 30015 03-01 14:24 flute.ps
  -rwxr-xr-x. 1 max pubs   485 09-21 08:03 floor*
  -rw-r--r--. 1 max pubs   484 04-09 08:14 maptax.icn
  drwxrwxr-x. 2 max pubs  1024 09-10 11:32 testdir/
In this example, the string that replaces the alias ll (ls ‒l) itself contains an alias (ls). When it replaces an alias with its value, the shell looks at the first word of the replacement string to see whether it is an alias. In the preceding example, the replacement string contains the alias ls, so a second substitution occurs to produce the final command ls ‒F ‒l. (To avoid a recursive plunge,the ls in the replacement text, although an alias, is not expanded a second time.)
When given a list of aliases without the =value or value field, the alias builtin displays the value of each defined alias. The alias builtin reports an error if an alias has not been defined:
$ alias ll l ls zap wx
  alias ll='ls -l'
  alias l='ls -ltr'
  alias ls='ls -F'
  alias zap='rm -i'
  bash: alias: wx: not found
You can avoid alias substitution by preceding the aliased command with a backslash (\):
$ \ls
  Test_Emacs XTerm.ad  flute.ps  maptax.icn
  Tiger    fixtax.icn  floor     testdir
Because the replacement of an alias name with the alias value does not change the rest of the command line, any arguments are still received by the command that is executed:
$ ll f*
  -rw-r--r--. 1 max pubs   641 04-01 08:12 fixtax.icn
  -rw-r--r--. 1 max pubs 30015 03-01 14:24 flute.ps
  -rwxr-xr-x. 1 max pubs   485 09-21 08:03 floor*
You can remove an alias using the unalias builtin. When the zap alias is removed, it is no longer displayed by the alias builtin, and its subsequent use results in an error message:
$ unalias zap
  $ alias
  alias ll='ls -l'
  alias l='ls -ltr'
  alias ls='ls -F'
  $ zap maptax.icn
  bash: zap: command not found



Installing Apache2 With PHP5 And MySQL Support On Scientific Linux 6.3 (LAMP)

Installing Apache2 With PHP5 And MySQL Support On Scientific Linux 6.3 (LAMP)


LAMP is short for Linux,Apache, MySQL, PHP. This tutorial shows how you can install an Apache2 webserver on a Scientific Linux 6.3 server with PHP5 support (mod_php) and MySQL support.
I do not issue any guarantee that this will work for you!

1 Preliminary Note

In this tutorial I use the hostname server1.example.com with the IP address 192.168.0.100. These settings might differ for you, so you have to replace them where appropriate.

2 Installing MySQL 5

To install MySQL, we do this:
yum install mysql mysql-server
Then we create the system startup links for MySQL (so that MySQL starts automatically whenever the system boots) and start the MySQL server:
chkconfig --levels 235 mysqld on
/etc/init.d/mysqld start
Set passwords for the MySQL root account:
mysql_secure_installation
[root@server1 ~]# mysql_secure_installation




NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!


In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n]
 <-- ENTER
New password: <-- yourrootsqlpassword
Re-enter new password: <-- yourrootsqlpassword
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]
 <-- ENTER
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]
 <-- ENTER
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]
 <-- ENTER
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]
 <-- ENTER
 ... Success!

Cleaning up...



All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!


[root@server1 ~]#
 

3 Installing Apache2

Apache2 is available as a Scientific Linux package, therefore we can install it like this:
yum install httpd
Now configure your system to start Apache at boot time...
chkconfig --levels 235 httpd on
... and start Apache:
/etc/init.d/httpd start
Now direct your browser to http://192.168.0.100, and you should see the Apache2 placeholder page:

Apache's default document root is /var/www/html on Scientific Linux, and the configuration file is /etc/httpd/conf/httpd.conf. Additional configurations are stored in the /etc/httpd/conf.d/ directory.

4 Installing PHP5

We can install PHP5 and the Apache PHP5 module as follows:
yum install php
We must restart Apache afterwards:
/etc/init.d/httpd restart
 5 Testing PHP5 / Getting Details About Your PHP5 Installation
The document root of the default web site is /var/www/html. We will now create a small PHP file (info.php) in that directory and call it in a browser. The file will display lots of useful details about our PHP installation, such as the installed PHP version.
vi /var/www/html/info.php
<?php
phpinfo();
?>
Now we call that file in a browser (e.g. http://192.168.0.100/info.php):
As you see, PHP5 is working, and it's working through the Apache 2.0 Handler, as shown in the Server API line. If you scroll further down, you will see all modules that are already enabled in PHP5. MySQL is not listed there which means we don't have MySQL support in PHP5 yet.

6 Getting MySQL Support In PHP5
To get MySQL support in PHP, we can install the php-mysql package. It's a good idea to install some other PHP5 modules as well as you might need them for your applications. You can search for available PHP5 modules like this:
yum search php
Pick the ones you need and install them like this:
yum install php-mysql php-gd php-imap php-ldap php-mbstring php-odbc php-pear php-xml php-xmlrpc
APC is a free and open PHP opcode cacher for caching and optimizing PHP intermediate code. It's similar to other PHP opcode cachers, such as eAccelerator and Xcache. It is strongly recommended to have one of these installed to speed up your PHP page.
APC can be installed as follows:
yum install php-pecl-apc
Now restart Apache2:
/etc/init.d/httpd restart
Now reload http://192.168.0.100/info.php in your browser and scroll down to the modules section again. You should now find lots of new modules there, including the MySQL module:

7 phpMyAdmin
phpMyAdmin is a web interface through which you can manage your MySQL databases.
First we enable the RPMforge repository on our Scientific Linux system as phpMyAdmin is not available in the official Scientific Linux 6.3 repositories:
Import the RPMforge GPG key:
rpm --import http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt
On x86_64 systems:
yum install http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
On i386 systems:
yum install http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.i686.rpm
phpMyAdmin can now be installed as follows:
yum install phpmyadmin
Now we configure phpMyAdmin. We change the Apache configuration so that phpMyAdmin allows connections not just from localhost (by commenting out the <Directory "/usr/share/phpmyadmin"> stanza):
vi /etc/httpd/conf.d/phpmyadmin.conf
#
#  Web application to manage MySQL
#

#<Directory "/usr/share/phpmyadmin">
#  Order Deny,Allow
#  Deny from all
#  Allow from 127.0.0.1
#</Directory>

Alias /phpmyadmin /usr/share/phpmyadmin
Alias /phpMyAdmin /usr/share/phpmyadmin
Alias /mysqladmin /usr/share/phpmyadmin
Next we change the authentication in phpMyAdmin from cookie to http:
vi /usr/share/phpmyadmin/config.inc.php
[...]
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'http';
[...]
Restart Apache:
/etc/init.d/httpd restart
Afterwards, you can access phpMyAdmin under http://192.168.0.100/phpmyadmin/:

8 Links
·        Apache: http://httpd.apache.org/
·        PHP: http://www.php.net/
·        MySQL: http://www.mysql.com/
·        Scientific Linux: https://www.scientificlinux.org/
·        phpMyAdmin: http://www.phpmyadmin.net/