Linux Shell Script /bin/sh /bin/bash Command Cross-Reference
This is a small overview of common perl/php/C commands and their shell equivalent.
SPLIT a string with "cut"
Use "cut" to split a string with a certain character.
Parameters for "cut": -d = delimiter ; -f = column to return
#!/bin/sh
TMP="COLUMN1 COLUMN2 COLUMN3"
RESULT=`echo $TMP | cut -d \ -f 2`
echo $RESULT
RESULT=`echo $TMP | cut -d L -f 2`
echo $RESULT
(Note: the first "cut" splits the string at the spaces "\ " - a back-slash is used to indicate the space)
Search and replace with regular expression with "sed"
Use "sed" for searching for a string and replacing it.
#!/bin/sh
TMP="TEST=Captain"
RESULT=`echo $TMP | sed -e 's,TEST=\(.*\)$,\1,g'`
echo "$RESULT was here"
SUBSTRING / SUBSTR with "awk"
Use "awk" for processing a sub-string
#!/bin/sh
KERNELVERSION=`uname -r | awk '{print substr($1,1,3)}'`
if [ "$KERNELVERSION" = "2.6" ]
then
echo "COOL! You're running a 2.6 kernel"
else
echo "A 2.4 kernel, eh ? Try 2.6! Very stable!"
fi
A little unrelated to the scope of this page, but still very useful:
Checking text-files (config-files) for a certain type of parameter, content, line etc.
#!/bin/sh
if egrep -qi "^[[:space:]]*parameter.*123" test.conf
then
echo "OK"
fi
test.conf
parameter 123
Last-Modified: Sat, 04 Feb 2006 16:03:06 GMT