Using # tcpdump -vvi
Wednesday, October 13, 2010
How to check Interface traffice on unix :
Using # tcpdump -vvi
Thursday, July 29, 2010
what is the meaning of " /dev/null 2>&1 "
The greater-than (>) in commands like these redirect the program’s output somewhere. In this case, something is being redirected into /dev/null, and something is being redirected into &1.
Standard in, out and error:
There are three standard sources of input and output for a program. Standard input usually comes from the keyboard if it’s an interactive program, or from another program if it’s processing the other program’s output. The program usually prints to standard output, and sometimes prints to standard error. These three file descriptors (you can think of them as “data pipes”) are often called STDIN, STDOUT, and STDERR.
Sometimes they’re not named, they’re numbered! The built-in numberings for them are 0, 1, and 2, in that order. By default, if you don’t name or number one explicitly, you’re talking about STDOUT.
That means file descriptor 0 or fd0 denotes STDIN or standard input and file descriptor 1 or fd1 denotes STDOUT or standard output and file descriptor 2 or fd2 denotes STDERR or standard error.
You can see the command above is redirecting standard output into /dev/null, which is a place you can dump anything you don’t want (often called the bit-bucket), then redirecting standard error into standard output (you have to put an & in front of the destination when you do this).
The short explanation, therefore, is “all output from this command should be shoved into a black hole.” That’s one good way to make a program be really quiet!
example :
/opt/oracle/admin/pgrxd/analyze.ksh >> /opt/oracle/admin/pgrxd/analyze.log 2>&1
Thursday, July 22, 2010
/etc/vx/reconfig.d/state.d/install-db
vxvm:vxconfigd: ERROR: enable failed: Error in disk group configuration copies
Rootdg disk group has no configuration copies; transactions are disabled.
vxvm:vxdctl: ERROR: enable failed: Error in disk group configuration copies
In Volume Manager 3.1.1 with patch02 or patch03, if /etc/vx/reconfig.d/state.d/install-db exists, the Volume Manager boot file, /etc/vx/volboot, will be re-initialized. If any simple disk is included in rootdg, Volume Manager can not be started properly even if the file install-db is removed. This problem only occurred in Volume Manager 3.1.1 with patch02 or patch03, and the Volume Manager 3.1.1 Japanese version.
NOTE: This problem does not occur in Volume Manager 3.1.1 without patches and Volume Manager 3.2.
After the /etc/vx/reconfig.d/state.d/install-db file is created, an error message will not be displayed during the boot process. But, after the machine boots up, running the vxdctl enable command to enable the vxconfigd daemon will generate an error message as shown below:
# vxdctl enable
vxvm:vxconfigd: ERROR: enable failed: Error in disk group configuration copies
Rootdg disk group has no configuration copies; transactions are disabled.
vxvm:vxdctl: ERROR: enable failed: Error in disk group configuration copies
If simple disks are used in Volume Manager, run the following command to re-add all simple disk entries to the volboot file in order to re-enable the vxconfigd daemon. If more than one simple disk is used in Volume Manager, repeat the command until all of the simple disk entries have been added.
# vxdctl add disk
If no simple disks are used in Volume Manager, then the above step is not necessary. If the prior Volume Manager host ID was different than the operating system host name, it must run the following command to change the Volume Manager host ID, which is saved in the volboot file, to get Volume Manager started properly.
# vxdctl hostid
Then vxconfigd can be re-enabled by executing the following commands:
# vxdctl -k stop
# vxconfigd -d
# vxdctl enable
To fix the problem permanently, apply the following solution after installing the patch, but before reboot, to avoid the problem. If the problem has already occurred, apply the solution after recovering the volboot file using the steps above.
Solution
1. Find the following lines in the startup script /etc/init.d/vxvm-reconfig in line 165 to 171:
165> if [ -f $mkdbfile ]; then
166> xcmd vxiod set 10
167> xcmd $VOLD -k -r reset -m disable
168> xcmd vxdctl init
169> xcmd vxdctl initdmp
170> xcmd vxdg init rootdg
171> fi
2. Find the following line in line 222:
222> [ -f $reconfigfile ] || exit 0
3. Move the lines in step 1 to appear below the line in step 2
4. The modified vxvm-reconfig file will appear as shown below:
214> [ -f $reconfigfile ] || exit 0
215>
216> if [ -f $mkdbfile ]; then
217> xcmd vxiod set 10
218> xcmd $VOLD -k -r reset -m disable
219> xcmd vxdctl init
220> xcmd vxdctl initdmp
221> xcmd vxdg init rootdg
222> fi
5. Reboot
Linux Variables & Iteration, control and if statements
Linux Shell ProgrammingThis section on shell programming, is a brief introduction to shell programming, and only talks about the bash shell. For more complete information, refer to "The CTDP Linux Programmer's Guide". Linux VariablesWhen variables are used they are referred to with the $ symbol in front of them. There are several useful variables available in the shell program. Here are a few:
The "shift" command can be used to shift command line arguments to the left, ie $1 becomes the value of $2, $3 shifts into $2, etc. The command, "shift 2" will shift 2 places meaning the new value of $1 will be the old value of $3 and so forth. Iteration, control and if statements | |
- if - Used to execute one or more statements on a condition. An example:
if [ ! -d /mnt ] # be sure the directory /mnt exists
then
mkdir /mnt
fi - case - Used to execute specific commands based on the value of a variable. An example:
case $NUM of
1)
echo The number is 1
;;
2)
echo The number is 2
;;
*)
echo The number is not 1 or 2
;;
esac - for - Used to loop for all cases of a condition. In the example below, it is used to copy all files found in /mnt/floppy to the /etc directory. The lines were numbered for reference with descriptions:
- The for loop statement will loop until all files have been found.
- A test to be sure the file is a normal file and not a directory.
- A comment line.
- This line extracts the name of the file from its full path pointed to by the variable $i and puts it in the variable $filename. The method used here is called parameter expansion and is documented in the bash man page. For more information on parameter expansion read the "Linux Programmer's Guide".
- This line sends a statement to the standard output, telling what file is being copied.
- This line performs the copy command using the -p option to preserve file attributes. Note: Much ability to perform script programming is couched in the ability to know and use the various commands, programs and tools available in Linux rather than a strict understanding of syntax. This is obvious to anyone who reads the system startup script files in /etc/rc.d and associated directories.
- This line ends the if statement.
- This line ends the for statement.
1. for i in /mnt/floppy/*; do
2. if [ -f $i ]; then
3. # if the file is there
4. filename=${i#/mnt/floppy/}
5. echo copying $i to /etc/$filename
6. cp -p $i /etc/$filename
7. fi
8. done - until - Cycles through a loop until some condition is met. The syntax for the command is shown below:
until [ expression ]
do
statements
done - while - Cycles through a loop while some condition is met. The below example will cycle through a loop forever:
while [ 1 ]
do
statement(s)
done
Tests
There is a function provided by bash called test which returns a true or false value depending on the result of the tested expression. Its syntax is:
test expression
It can also be implied as follows:
[ expression ]
The tests below are test conditions provided by the shell:
- -b file = True if the file exists and is block special file.
- -c file = True if the file exists and is character special file.
- -d file = True if the file exists and is a directory.
- -e file = True if the file exists.
- -f file = True if the file exists and is a regular file
- -g file = True if the file exists and the set-group-id bit is set.
- -k file = True if the files' "sticky" bit is set.
- -L file = True if the file exists and is a symbolic link.
- -p file = True if the file exists and is a named pipe.
- -r file = True if the file exists and is readable.
- -s file = True if the file exists and its size is greater than zero.
- -s file = True if the file exists and is a socket.
- -t fd = True if the file descriptor is opened on a terminal.
- -u file = True if the file exists and its set-user-id bit is set.
- -w file = True if the file exists and is writable.
- -x file = True if the file exists and is executable.
- -O file = True if the file exists and is owned by the effective user id.
- -G file = True if the file exists and is owned by the effective group id.
- file1 –nt file2 = True if file1 is newer, by modification date, than file2.
- file1 ot file2 = True if file1 is older than file2.
- file1 ef file2 = True if file1 and file2 have the same device and inode numbers.
- -z string = True if the length of the string is 0.
- -n string = True if the length of the string is non-zero.
- string1 = string2 = True if the strings are equal.
- string1 != string2 = True if the strings are not equal.
- !expr = True if the expr evaluates to false.
- expr1 –a expr2 = True if both expr1 and expr2 are true.
- expr1 –o expr2 = True is either expr1 or expr2 is true.
The syntax is :
arg1 OP arg2
where OP is one of –eq, -ne, -lt, -le, -gt, or –ge. Arg1 and arg2 may be positive or negative integers or the special expression "–l string" which evaluates to the length of string.
An example script
The file below is an example file which demonstrates some of the testing as talked above along with several looping and control statements.
#! /bin/bash
# Use the bash shell to run the script
# This is an example file to take entries from the user
# entries Version 1.0 May 22, 2000
DONE=no
ENTRIES="hello bye ls 1"
while [ $DONE = no ]
do
echo Valid entries are: $ENTRIES
read ENTRY # Read the variable ENTRY from the user
case $ENTRY in
1)
pwd
;;
hello)
echo How are you?
;;
bye)
echo exiting...
DONE=yes
;;
ls)
ls -al |more
;;
*)
echo $ENTRY is an unrecognized command.
;;
esac
done
gdb debugger commands
See the gdb(1) man page for more information on the gdb program. Below are listed some commands:
- file - Loads the exe file.
- kill - Terminates the program being debugged.
- list - List sections of the source code.
- next - Advances one line of source code in the current function and steps into other functions.
- run - Executes the program that is being debugged.
- quit - Ends gdb.
- watch - Enables you to tell the value of a variable when it changes.
- break - Set a break point.
- make - Remake the exe program without quitting gdb.
- shell - Run a UNIX shell command without leaving gdb.
While loop
Shell Script While Loop Examples
by Vivek Gite · 6 comments
Can you provide me a while loop control flow statement shell script syntax and example that allows code to be executed repeatedly based on a given boolean condition?
Each while loop consists of a set of commands and a condition. The general syntax as follows for bash while loop:
while [ condition ]
do
command1
command2
commandN
done
- The condition is evaluated, and if the condition is true, the command1,2…N is executed.
- This repeats until the condition becomes false.
- The condition can be integer ($i <>
ksh while loop syntax:
while [[ condition ]] ; do
command1
command1
commandN
done
csh while loop syntax:
while ( condition )
commands
end
BASH while Loop Example
#!/bin/bash
c=1
while [ $c -le 5 ]
do
echo "Welcone $c times"
(( c++ ))
done
KSH while loop Example
#!/bin/ksh
c=1
while [[ $c -le 5 ]]; do
echo "Welcome $c times"
(( c++ ))
done
CSH while loop Example
#!/bin/csh
c=1
while ( $c <= 5 )
echo "Welcome $c times"
@ c = $c + 1
end
Another example:
#!/bin/csh
set yname="foo"
while ( $yname != "" )
echo -n "Enter your name : "
set yname = $<
if ( $yname != "" ) then
echo "Hi, $yname"
endif
end
"for" loop syntax
for loop syntax
Numeric ranges for syntax is as follows:
for VARIABLE in 1 2 3 4 5 .. N
do
command1
command2
commandN
done
This type of for loop is characterized by counting. The range is specified by a beginning (#1) and ending number (#5). The for loop executes a sequence of commands for each member in a list of items. A representative example in BASH is as follows to display welcome message 5 times with for loop:
#!/bin/bash
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
Sometimes you may need to set a step value (allowing one to count by two's or to count backwards for instance). Latest bash version 3.0+ has inbuilt support for setting up ranges:
#!/bin/bash
for i in {1..5}
do
echo "Welcome $i times"
done
Bash v4.0+ has inbuilt support for setting up a step value using {START..END..INCREMENT} syntax:
#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
for i in {0..10..2}
do
echo "Welcome $i times"
done
Sample outputs:
Bash version 4.0.33(0)-release...
Welcome 0 times
Welcome 2 times
Welcome 4 times
Welcome 6 times
Welcome 8 times
Welcome 10 times
The seq command (outdated)
The seq command can be used as follows. A representative example in seq is as follows:
#!/bin/bash
for i in $(seq 1 2 20)
do
echo "Welcome $i times"
done
There is no good reason to use an external command such as seq to count and increment numbers in the for loop, hence it is recommend that you avoid using seq. The builtin command are fast.
Three-expression bash for loops syntax
This type of for loop share a common heritage with the C programming language. It is characterized by a three-parameter loop control expression; consisting of an initializer (EXP1), a loop-test or condition (EXP2), and a counting expression (EXP3).
for (( EXP1; EXP2; EXP3 ))
do
command1
command2
command3
done
A representative three-expression example in bash as follows:
#!/bin/bash
for (( c=1; c<=5; c++ ))
do
echo "Welcome $c times..."
done
Sample output:
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
How do I use for as infinite loops?
Infinite for loop can be created with empty expressions, such as:
#!/bin/bash
for (( ; ; ))
do
echo "infinite loops [ hit CTRL+C to stop]"
done
Conditional exit with break
You can do early exit with break statement inside the for loop. You can exit from within a FOR, WHILE or UNTIL loop using break. General break statement inside the for loop:
for I in 1 2 3 4 5
do
statements1 #Executed for all values of ''I'', up to a disaster-condition if any.
statements2
if (disaster-condition)
then
break #Abandon the loop.
fi
statements3 #While good and, no disaster-condition.
done
Following shell script will go though all files stored in /etc directory. The for loop will be abandon when /etc/resolv.conf file found.
#!/bin/bash
for file in /etc/*
do
if [ "${file}" == "/etc/resolv.conf" ]
then
countNameservers=$(grep -c nameserver /etc/resolv.conf)
echo "Total ${countNameservers} nameservers defined in ${file}"
break
fi
done
Early continuation with continue statement
To resume the next iteration of the enclosing FOR, WHILE or UNTIL loop use continue statement.
for I in 1 2 3 4 5
do
statements1 #Executed for all values of ''I'', up to a disaster-condition if any.
statements2
if (condition)
then
continue #Go to next iteration of I in the loop and skip statements3
fi
statements3
done
This script make backup of all file names specified on command line. If .bak file exists, it will skip the cp command.
#!/bin/bash
FILES="$@"
for f in $FILES
do
# if .bak backup file exists, read next file
if [ -f ${f}.bak ]
then
echo "Skiping $f file..."
continue # read next file and skip cp command
fi
# we are hear means no backup file exists, just use cp command to copy file
/bin/cp $f $f.bak
done