This was a fortune tumbled down by my /usr/bin/fortune at my bash prompt. I liked it very much
The highest good is like water.
Water give life to the ten thousand things and does not strive.
It flows in places men reject and so is like the Tao.
In dwelling, be close to the land.
In meditation, go deep in the heart.
In dealing with others, be gentle and kind.
In speech, be true.
In ruling, be just.
In daily life, be competent.
In action, be aware of the time and the season.
No fight: No blame.
Friday, March 6, 2009
Monday, January 5, 2009
Positional Paramters at bash
The positional parameters or command line parameters are those which are passed as arguments to the script
eg: #myscript.sh arg1 arg2 arg3 arg4
Here arg1, arg2, arg3, and arg4 are positional or command line parameters. Now lets look at how these command line parameters can be used in the script. Bash keeps some internal variables in which the positional parameters are saved. They are as below
$0 Filename of script
$1 - $n Positional parameters #1 - #n
$# Number of positional parameters
"$*" All the positional parameters
"$@" All the positional parameters (why two of them ???)
${#*} Number of command-line parameters passed to script
${#@} Number of command-line parameters passed to script
Now let us see some examples using the positional parameters.
Check the script below
#!/bin/bash
if [[ $# != 11 ]]
then
echo "Usage `basename $0` arg1 arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 arg10 arg11"
exit 1
fi
echo -e "first argument is $1"
echo -e "second argument is $2"
echo -e "Third arguement is $3"
echo -e "Fourth argument is $4"
echo -e "Fifth argument is $5"
echo -e "Sixth argument is $6"
echo -e "Seventh argument is $7"
echo -e "Eighth argument is $8"
echo -e "Nineth argument is $9"
echo -e "Tenth argument is $10"
echo -e "Eleventh argument is $11"
echo -e "The complete list of arguments are $*"
echo -e "The complete list of arguments are $@"
echo -e "The number of command line parameters are ${#*}"
echo -e "The number of command line parameters are ${#@}"
Executing the above scripts gives the following output.
[sijo@CyBerJiNX bash]$ ./posparams.sh 1 2 3 4 5 6 7 8 9 10 11
first argument is 1
second argument is 2
Third arguement is 3
Fourth argument is 4
Fifth argument is 5
Sixth argument is 6
Seventh argument is 7
Eighth argument is 8
Nineth argument is 9
Tenth argument is 10
Eleventh argument is 11
The complete list of arguments are 1 2 3 4 5 6 7 8 9 10 11
The complete list of arguments are 1 2 3 4 5 6 7 8 9 10 11
The number of command line parameters are 11
The number of command line parameters are 11
[sijo@CyBerJiNX bash]$
Now rerun script as below.
[sijo@CyBerJiNX bash]$ ./posparams.sh one two three four five six seven eight nine ten eleven
first argument is one
second argument is two
Third arguement is three
Fourth argument is four
Fifth argument is five
Sixth argument is six
Seventh argument is seven
Eighth argument is eight
Nineth argument is nine
Tenth argument is one0
Eleventh argument is one1
The complete list of arguments are one two three four five six seven eight nine ten eleven
The complete list of arguments are one two three four five six seven eight nine ten eleven
The number of command line parameters are 11
The number of command line parameters are 11
[sijo@CyBerJiNX bash]$
See the way in which the tenth and eleventh argument is printed. Is this a bug.. No, so what is wrong with the script. Let us recheck the lines which prints tenth and eleventh args.
echo -e "Tenth argument is $10"
echo -e "Eleventh argument is $11"
The bash prints them as $1 followed by a 0 and $1 followed by a 1.So how to print the arguments $10 and $11. Rewrite the lines as
echo -e "Tenth argument is ${10}"
echo -e "Eleventh argument is ${11}"
The curly braces {} acts as the delimiter for the variable name to be printed. This is the right way in which any variables in a shell script should be printed.
Now check the lines
echo -e "The complete list of arguments are $*"
echo -e "The complete list of arguments are $@"
Are these lines one and the same?... No, they are not
The first line $* will store the entire command line parameters as a single string where as the second one $@ stores the elements in an array.
}}}
eg: #myscript.sh arg1 arg2 arg3 arg4
Here arg1, arg2, arg3, and arg4 are positional or command line parameters. Now lets look at how these command line parameters can be used in the script. Bash keeps some internal variables in which the positional parameters are saved. They are as below
$0 Filename of script
$1 - $n Positional parameters #1 - #n
$# Number of positional parameters
"$*" All the positional parameters
"$@" All the positional parameters (why two of them ???)
${#*} Number of command-line parameters passed to script
${#@} Number of command-line parameters passed to script
Now let us see some examples using the positional parameters.
Check the script below
#!/bin/bash
if [[ $# != 11 ]]
then
echo "Usage `basename $0` arg1 arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 arg10 arg11"
exit 1
fi
echo -e "first argument is $1"
echo -e "second argument is $2"
echo -e "Third arguement is $3"
echo -e "Fourth argument is $4"
echo -e "Fifth argument is $5"
echo -e "Sixth argument is $6"
echo -e "Seventh argument is $7"
echo -e "Eighth argument is $8"
echo -e "Nineth argument is $9"
echo -e "Tenth argument is $10"
echo -e "Eleventh argument is $11"
echo -e "The complete list of arguments are $*"
echo -e "The complete list of arguments are $@"
echo -e "The number of command line parameters are ${#*}"
echo -e "The number of command line parameters are ${#@}"
Executing the above scripts gives the following output.
[sijo@CyBerJiNX bash]$ ./posparams.sh 1 2 3 4 5 6 7 8 9 10 11
first argument is 1
second argument is 2
Third arguement is 3
Fourth argument is 4
Fifth argument is 5
Sixth argument is 6
Seventh argument is 7
Eighth argument is 8
Nineth argument is 9
Tenth argument is 10
Eleventh argument is 11
The complete list of arguments are 1 2 3 4 5 6 7 8 9 10 11
The complete list of arguments are 1 2 3 4 5 6 7 8 9 10 11
The number of command line parameters are 11
The number of command line parameters are 11
[sijo@CyBerJiNX bash]$
Now rerun script as below.
[sijo@CyBerJiNX bash]$ ./posparams.sh one two three four five six seven eight nine ten eleven
first argument is one
second argument is two
Third arguement is three
Fourth argument is four
Fifth argument is five
Sixth argument is six
Seventh argument is seven
Eighth argument is eight
Nineth argument is nine
Tenth argument is one0
Eleventh argument is one1
The complete list of arguments are one two three four five six seven eight nine ten eleven
The complete list of arguments are one two three four five six seven eight nine ten eleven
The number of command line parameters are 11
The number of command line parameters are 11
[sijo@CyBerJiNX bash]$
See the way in which the tenth and eleventh argument is printed. Is this a bug.. No, so what is wrong with the script. Let us recheck the lines which prints tenth and eleventh args.
echo -e "Tenth argument is $10"
echo -e "Eleventh argument is $11"
The bash prints them as $1 followed by a 0 and $1 followed by a 1.So how to print the arguments $10 and $11. Rewrite the lines as
echo -e "Tenth argument is ${10}"
echo -e "Eleventh argument is ${11}"
The curly braces {} acts as the delimiter for the variable name to be printed. This is the right way in which any variables in a shell script should be printed.
Now check the lines
echo -e "The complete list of arguments are $*"
echo -e "The complete list of arguments are $@"
Are these lines one and the same?... No, they are not
The first line $* will store the entire command line parameters as a single string where as the second one $@ stores the elements in an array.
}}}
Wednesday, September 10, 2008
Compress and uncompress the files in Linux with tar
To compress folder myfolder to myfolder.tar.gz
tar czfv myfolder.tar.gz myfolder/
czfv = "compress gzip file archive with verbose output"
If you want bzip files, use ‘j’ instead of ‘z’.
tar cjfv myfolder.tar.gz myfolder
cjfv = "compress bzip file archive with verbose output"
Uncompress myfolder.tar.gz to folder myfolder/
tar -xzf Test.tar.gz
xzf = ‘extract gzipped file archive’
Again, if you want bzip files, use ‘j’ instead of ‘z’.
tar -xzf Test.tar.gz
xjf = ‘extract bzipped file archive’
tar czfv myfolder.tar.gz myfolder/
czfv = "compress gzip file archive with verbose output"
If you want bzip files, use ‘j’ instead of ‘z’.
tar cjfv myfolder.tar.gz myfolder
cjfv = "compress bzip file archive with verbose output"
Uncompress myfolder.tar.gz to folder myfolder/
tar -xzf Test.tar.gz
xzf = ‘extract gzipped file archive’
Again, if you want bzip files, use ‘j’ instead of ‘z’.
tar -xzf Test.tar.gz
xjf = ‘extract bzipped file archive’
Tuesday, August 19, 2008
Setting the inital file permissions using umask
umask command is used to set the default permission modes for newly created files in the current shell and its child processes.
umasks are calculated as the AND of the unary complement of the argument (NOT) and the full access mode.
The full access mode is 666 in the case of files, and 777 in the case of directories
A common umask value is 022 (masking out the write permission for the group and others), which ensures that new files are only writable for the owner (i.e. the user who created them). Another common value is 002, which leaves the write permission for the file's group enabled. This can be used for files in shared workspaces, where several users work with the same files.
Example usage of setting umask
Assuming the umask has the value 163, any new file will be created with the permissions 604 and any new directory will have permissions 614 because:
666 AND NOT(174) = 604
666=110 110 110
163 = 001 110 011
NOT(001 110 011) = (110 001 100)
(110 110 110) AND (110 001 100) = (110 000 100)
777 NOT (163) = 604
while
777 AND NOT(163) = 614
777 = 111 111 111
163 = 001 110 011
NOT(001 110 011) = (110 001 100)
(111 111 111) AND (110 001 100) = (110 001 100)
777 NOT (163) = 614
umasks are calculated as the AND of the unary complement of the argument (NOT) and the full access mode.
The full access mode is 666 in the case of files, and 777 in the case of directories
A common umask value is 022 (masking out the write permission for the group and others), which ensures that new files are only writable for the owner (i.e. the user who created them). Another common value is 002, which leaves the write permission for the file's group enabled. This can be used for files in shared workspaces, where several users work with the same files.
Example usage of setting umask
Assuming the umask has the value 163, any new file will be created with the permissions 604 and any new directory will have permissions 614 because:
666 AND NOT(174) = 604
666=110 110 110
163 = 001 110 011
NOT(001 110 011) = (110 001 100)
(110 110 110) AND (110 001 100) = (110 000 100)
777 NOT (163) = 604
while
777 AND NOT(163) = 614
777 = 111 111 111
163 = 001 110 011
NOT(001 110 011) = (110 001 100)
(111 111 111) AND (110 001 100) = (110 001 100)
777 NOT (163) = 614
Monday, August 11, 2008
Attaching serial console on a Linux box
Step # 1: Setup Serial redirection
By default, grub output does not appear on the remote console; it only appears on the local terminal. However, you can configure your grub.conf file to redirect all grub output to the remote console. Redirection will display the grub output on the remote console, but not on the local terminal. To redirect grub output, make the following changes to your /etc/grub.conf file:
# vi /boot/grub/grub.conf
Append the following lines in the file
serial --unit=1 --speed=19200 --word=8 --parity=no --stop=1
terminal --timeout=8 console serial
* The first line tells GRUB to use the first serial port at a baud rate of 19200
* The second line gives the user 9 seconds to decide where GRUB should output it's information.
* Please adjust port number and speed as per your setup.
Next make sure splashimage options is disabled as graphics can't be displayed across the serial port. Remove splashimage line or just comment it out by prefixing # symbol:
#splashimage=(hd0,0)/grub/splash.xpm.gz
Also please comment out the hiddenmenu option ( We had some issues at times when tried using this, so to be on safer side,disable it )
Step # 2: Enabling serial output from the Linux kernel
Find the kernel line (grub config file) which corresponds to your currently running kernel. Add the following at the end of that line - console=tty0 console=ttyS0,9600n8:
title Red Hat Enterprise Linux ES (2.6.9-42.0.10.ELsmp)
root (hd0,0)
kernel /vmlinuz-2.6.9-42.0.10.ELsmp ro root=LABEL=/ console=tty0 console=ttyS1,19200n8
initrd /initrd-2.6.9-42.0.10.ELsmp.img
On Red Hat EL nodes, please make the following changes
* Set SAFE=YES in /etc/sysconfig/kudzu
By default, grub output does not appear on the remote console; it only appears on the local terminal. However, you can configure your grub.conf file to redirect all grub output to the remote console. Redirection will display the grub output on the remote console, but not on the local terminal. To redirect grub output, make the following changes to your /etc/grub.conf file:
# vi /boot/grub/grub.conf
Append the following lines in the file
serial --unit=1 --speed=19200 --word=8 --parity=no --stop=1
terminal --timeout=8 console serial
* The first line tells GRUB to use the first serial port at a baud rate of 19200
* The second line gives the user 9 seconds to decide where GRUB should output it's information.
* Please adjust port number and speed as per your setup.
Next make sure splashimage options is disabled as graphics can't be displayed across the serial port. Remove splashimage line or just comment it out by prefixing # symbol:
#splashimage=(hd0,0)/grub/splash.xpm.gz
Also please comment out the hiddenmenu option ( We had some issues at times when tried using this, so to be on safer side,disable it )
Step # 2: Enabling serial output from the Linux kernel
Find the kernel line (grub config file) which corresponds to your currently running kernel. Add the following at the end of that line - console=tty0 console=ttyS0,9600n8:
title Red Hat Enterprise Linux ES (2.6.9-42.0.10.ELsmp)
root (hd0,0)
kernel /vmlinuz-2.6.9-42.0.10.ELsmp ro root=LABEL=/ console=tty0 console=ttyS1,19200n8
initrd /initrd-2.6.9-42.0.10.ELsmp.img
On Red Hat EL nodes, please make the following changes
* Set SAFE=YES in /etc/sysconfig/kudzu
Remote installation of OpenSuse-11 on an IBM PPC
1. Setup the DHCP/BOOTP/PXE/TFTP server to boot from network
3. Boot from network
4. The yaboot prompt will appear. Please enter
install vnc=1 ssh=1
to start the installation on vnc and to enable ssh connections.
5. You'll be asked to enter the vnc and ssh password. Please enter a password of your choice here.
Enter your VNC password>
Enter your temporary SSH password>
6. You'll be asked to choose the network interface you want to use.
Choose the network device.
1) eth1 : Broadcom NetXtreme BCM5704S Gigabit Ethern
2) eth0 : Broadcom NetXtreme BCM5704S Gigabit Ethern
Automatic configuration via DHCP?
1) Yes
2) No
7 Opensuse now starts the installer, Connect to the VNC server with a viewer of your choice
vncviewer:1
3. Boot from network
4. The yaboot prompt will appear. Please enter
install vnc=1 ssh=1
to start the installation on vnc and to enable ssh connections.
5. You'll be asked to enter the vnc and ssh password. Please enter a password of your choice here.
Enter your VNC password>
Enter your temporary SSH password>
6. You'll be asked to choose the network interface you want to use.
Choose the network device.
1) eth1 : Broadcom NetXtreme BCM5704S Gigabit Ethern
2) eth0 : Broadcom NetXtreme BCM5704S Gigabit Ethern
Automatic configuration via DHCP?
1) Yes
2) No
7 Opensuse now starts the installer, Connect to the VNC server with a viewer of your choice
vncviewer
Saturday, July 19, 2008
Special character ; at bashprompt
; is used as a command separator and to terminate a case statement at bash
#!/bin/bash
# This script is to explain the usage of the ; as a special character in bash.
# Sijo George
#--------------------------START-------------------------------
echo "Hello world,"; echo "How are you?"; # Execute two commands in a line using a ; delimiter
case $1 in
[a-z] ) echo " The input is an alphabet " ;; # Terminator in the case option
[0-9] ) echo " The input is a number" ;;
* ) echo " The input is neither an alpabet or a number ";;
esac
#!/bin/bash
# This script is to explain the usage of the ; as a special character in bash.
# Sijo George
#--------------------------START-------------------------------
echo "Hello world,"; echo "How are you?"; # Execute two commands in a line using a ; delimiter
case $1 in
[a-z] ) echo " The input is an alphabet " ;; # Terminator in the case option
[0-9] ) echo " The input is a number" ;;
* ) echo " The input is neither an alpabet or a number ";;
esac
Labels:
bash,
Linux,
scripting,
special characters at bash prompt
Subscribe to:
Posts (Atom)