Archive

Posts Tagged ‘bash’

self defined timeout for telnet on Linux

December 26th, 2013 Comments off

telnet's default timeout value is relative high, so you may want to change timeout value to lower value such as 5 seconds. Here's the way that we can fulfill this:

#!/bin/bash

timeout()
{
waitfor=5
command=$*
$command &
commandpid=$!
( sleep $waitfor ; kill -9 $commandpid > /dev/null 2>&1 ) &
watchdog=$!
sleeppid=$PPID
wait $commandpid > /dev/null 2>&1
kill $sleeppid > /dev/null 2>&1
}

timeout telnet slcc29-scan1.us.oracle.com 1521 >> $output

Also, we can use expect and set timeout for expect. When telnet is integrated with expect, we can fulfill timeout for telnet through using expect's timeout value:

#!/usr/bin/expect

set timeout 30

send "<put telnet command here>\r"

Bash Shell Parameter Expansion examples

July 7th, 2012 Comments off

Here are some example/tutorial about bash Shell Parameter Expansion. More is here.

If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

ifnull="";
my_god="God exists";
my_null_message="yes, it's null";

echo ${ifnull:-will_print_this};


If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way.

echo ${ifnull:=will_assign_this}; 

 

If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.

ifnull="";

echo ${ifnull:?"Error - ifnull is null"};

If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

echo ${my_god:+will_print_this};

Expands to up to <length> characters of parameter starting at the character specified by <offset>. If length is omitted, expands to the substring of parameter starting at the character specified by offset. length and offset are arithmetic expressions (see Shell Arithmetic). This is referred to as Substring Expansion. length must evaluate to a number greater than or equal to zero. If offset evaluates to a number less than zero, the value is used as an offset from the end of the value of parameter.

Substring indexing is zero-based unless the positional parameters are used, in which case the indexing starts at 1 by default. If offset is 0, and the positional parameters are used, $@ is prefixed to the list.

echo ${my_god:0:3}; #will print "God"

echo ${my_god:4}; #will print "exists"

If parameter is ‘@’, the result is length positional parameters beginning at offset.

set -- 1 2 3 4 5 6 7 8 9 0 a b c d e f g h

echo ${@:7} #will print "7 8 9 0 a b c d e f g h"

If parameter is an indexed array name subscripted by ‘@’ or ‘*’, the result is the length members of the array beginning with ${parameter[offset]}. A negative offset is taken relative to one greater than the maximum index of the specified array. Substring expansion applied to an associative array produces undefined results.

Note that a negative offset must be separated from the colon by at least one space to avoid being confused with the ‘:-’ expansion.

array=(0 1 2 3 4 5 6 7 8 9 0 a b c d e f g h)

echo ${array[@]:7} #will print "7 8 9 0 a b c d e f g h"

echo ${array[@]: -7:2} #will print "b c"

 

The length in characters of the expanded value of parameter is substituted. If parameter is ‘*’ or ‘@’, the value substituted is the number of positional parameters. If parameter is an array name subscripted by ‘*’ or ‘@’, the value substituted is the number of elements in the array.

my_god="God exists";

echo "God exists has ${#my_god} characters"; #10

The word is expanded to produce a pattern just as in filename expansion (see Filename Expansion). If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ‘#’ case) or the longest matching pattern (the ‘##’ case) deleted. If parameter is ‘@’ or ‘*’, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with ‘@’ or ‘*’, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

echo ${my_god#God}; #will print "exists"

echo ${my_god##God*}; #will print NULL

The word is expanded to produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest matching pattern (the ‘%’ case) or the longest matching pattern (the ‘%%’ case) deleted. If parameter is ‘@’ or ‘*’, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with ‘@’ or ‘*’, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

echo ${my_god%*exists}; #will print "God"

echo ${my_god%%*exists}; #will print NULL

The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. If pattern begins with ‘/’, all matches of pattern are replaced with string. Normally only the first match is replaced. If pattern begins with ‘#’, it must match at the beginning of the expanded value of parameter. If pattern begins with ‘%’, it must match at the end of the expanded value of parameter. If string is null, matches of pattern are deleted and the / following pattern may be omitted. If parameter is ‘@’ or ‘*’, the substitution operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with ‘@’ or ‘*’, the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.

my_god="God exists, heaven exists"

echo ${my_god//exists/does not exist}; #will print "God does not exist, heaven does not exist"

echo ${my_god/#God exists/God does not exist}; #will print "God does not exist, heaven exists"

echo ${my_god/%exists/does not exist}; #will print "God exists, heaven does not exist"

echo ${my_god//exists/}; #will print "God , heaven"

 

Categories: IT Architecture, Programming, SHELL Tags: