misc-code3

=Misc. scripts, code excerpts, and scraps - Page 3=

Best Explanation of File Redirection
@http://www.linuxtutorialblog.com/post/tutorial-the-best-tips-tricks-for-bash

Bash Math
@http://hacktux.com/bash/math

Regex Help
Site 1 Site 2 Site 3

Validating an IP Address in a Bash Script using Regex
@http://www.linuxjournal.com/content/validating-ip-address-bash-script

Check if Variable is a Number
code format="bash" if [ $VARIABLE -eq $VARIABLE 2> /dev/null ]; then echo $VARIABLE is a number else echo $VARIABLE isn't a number fi

[ $NUM -eq $NUM 2> /dev/null ] && [ x$NUM != "x" ]
 * 1) The above matches against blank lines too
 * 2) So to exclude blank lines do the following

code

Sed - Using & as the Matched String
code format="bash" Sometimes you want to search for a pattern and add some characters, like parenthesis, around or near the pattern you found. It is easy to do this if you are looking for a particular string:

sed 's/abc/(abc)/' new

The solution requires the special character "&." It corresponds to the pattern found.

sed 's/[a-z]*/(&)/' new code

Use Sed to find Urls on a Page and Convert to Links
code format="bash" sed 's/http.*faq/&<\/a>/' /tmp/faq > faq sed 's/http.*.com/&<\/a>/' /tmp/faq > faq sed 's/@CORP.*VM/&<\/a>/' /tmp/faq3 > faq.html

code

Sed Help
@http://www.grymoire.com/Unix/Sed.html @http://sed.sourceforge.net/sed1line.txt @http://www.selectorweb.com/sed_tutorial.html

Run Command and Get Return Value from SSH
code format="bash" retval=`${SSH} ${REMOTE_USER}@${REMOTE_SERVER} "cd ${SERVER_DIR} > /dev/null 2>&1 ; echo \$? "` code

Delete Last Character from a String
code format="bash" sed 's/.$//' code

Display only last Character of a string
code format="bash" VAR="string" echo ${VAR: -1} code

Date Formatting
@http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/

Strip everything but numbers from a string
code format="bash" STR="123 hi, how are you 456" NEWSTR=$(echo "$STR" | sed "s/[^0-9]//g") echo $NEWSTR code

Get Model # from Solaris
code format="bash" prtdiag -v | head | grep "System Configuration:" | sed 's/.*[^0-9]\([M|T][0-9][0-9]*\).*/\1/g' sysdef -d | head -1 | sed 's/.*[^0-9]\([M|T|V|E][0-9][0-9-]*\).*/\1/g'

code

Keypress Without Hitting Enter
code format="bash"
 * 1) Read a keypress without hitting ENTER.

read -s -n1 -p "Hit a key " keypress echo; echo "Keypress was "\"$keypress\""."


 * 1) -s option means do not echo input.
 * 2) -n N option means accept only N characters of input.
 * 3) -p option means echo the following prompt before reading input.

code
 * 1) Using these options is tricky, since they need to be in the correct order.

Send Output to Screen and Log File
code format="bash" From a script: script.sh 2>&1 |tee out.log

From a function: create_a_zone 2>&1 | tee -a $LOGFILE code

Parsing a String - Grab from Character X to Y
code format="bash" substr( s, i, n )

Where:

s is the string you want to perform the substr operation on i is the first character that you want to extract n is the last character you want to match

So if you said substr( "hello", 2, 4 ) it would match the second, third and fourth character and return "ell". Let me explain the code I gave

Code:

$ echo "welcome" | awk '{ print substr( $0, 0, 4 ) }'

In the above example, the first argument to substr is $0 (which in this case is the text piped to it - "$0" is the entire record). The second argument is 0 (I could also have used 1) because I want to match from the start of the string. I want to grab the first four characters, so the third argument is 4.

Code:

$ echo "welcome" | awk '{ print substr( $0, length($0) - 3, length($0) ) }'

The above code also makes use length(string) command to return the number of characters in the string. So here, I'm saying "return the substring from the first character (which is the length of the string minus 3), up until the very end of the string".

To clarify this:

Code:

$ echo "welcome" | awk '{ print (length($0) - 3), length($0) }' 4 7

So in our substr, we grab from character 4 to character 7 inclusive, which are chars 4,5,6,7 - the last 4 chars. code

Printf with Colors
code format="bash"
 * 1) !/usr/bin/ksh

printf "%-15s %-20s %-20s\n" "Name" "Age" "Site" "" "---" "" printf "\033[34m" printf "%-15s" "Bob Actor" printf "\033[31m" printf "%-20s" "30" printf "\033[32m" printf "%-20s\n" "Orange County" printf "\033[mo" code sysdef -d | head -1 | sed 's/.*[^0-9]\([M|T|V|E][0-9][0-9-]*\).*/\1/g'