Alias and Script to start DHCP and Xen VM

January 21st, 2012

I wanted to create a bash script which started a dhcp server daemon and also started a Xen virtual machine. The script would be tiny and it would be executed using an alias, which I added to my bashrc.

Here is the script:

[root@shimla ~]# cat start.sh
#!/bin/bash
/etc/init.d/dhcpd start
cd /etc/xen
xm create Elastix-PBX

[root@shimla ~]#

Change permissions:

[root@shimla ~]# chmod 777 start.sh
[root@shimla ~]#

Edit ~/.bashrc to add an alias of “go” for the command “./start.sh” (see further below)

alias go=’./start.sh’

Test (need to logout and back in to read bash profile again):

[root@shimla ~]# go
Starting dhcpd: [ OK ]
Using config file “./Elastix-PBX”.
Started domain Elastix-PBX
[root@shimla ~]#

Linux startup files explained:
These files contain the aliases and environmental variables made available to Bash running as a user shell and to all Bash scripts invoked after system initialization:

/etc/profile

These are systemwide defaults, mostly setting the environment (all Bourne-type shells, not just Bash)

/etc/bashrc

These are systemwide functions and aliases for Bash

$HOME/.bash_profile

These are user-specific Bash environmental default settings, found in each user’s home directory (the local counterpart to /etc/profile)


$HOME/.bashrc

These are user-specific Bash init file, found in each user’s home directory (the local counterpart to /etc/bashrc). Only interactive shells and user scripts read this file.

Here are the contents of my startup files.

[root@shimla init.d]# cat ~/.bashrc
# .bashrc

# User specific aliases and functions with “start.sh” alias (“go”) added:

alias rm=’rm -i’
alias cp=’cp -i’
alias mv=’mv -i’
alias go=’./start.sh’

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@shimla init.d]#

[root@shimla init.d]# cat ~/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin:/usr/sbin:/sbin

export PATH
unset USERNAME
[root@shimla init.d]#

Linux logout files:

$HOME/.bash_logout
These are user-specific instruction file, found in each user’s home directory. Upon exit from a login (Bash) shell, the commands in this file execute.

Comments are closed.